Skip to content

Commit

Permalink
Support the /oncalls endpoint in the CLI (#24)
Browse files Browse the repository at this point in the history
* preliminary support for oncall list

* fmt.Println already inserts blanks between operands

* the implementation of this print is inconsistent (i, i+1) and superfluous. print valid yaml instead

* support all args for oncall_list

* add meta help string for oncall_list args
  • Loading branch information
afirth authored and ranjib committed Sep 5, 2016
1 parent 7868ce7 commit 2e74576
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
62 changes: 60 additions & 2 deletions command/oncall_list.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package main

import (
"github.com/mitchellh/cli"
"fmt"
"strings"

"github.com/PagerDuty/go-pagerduty"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/cli"
"gopkg.in/yaml.v2"
)

type OncallList struct {
Meta
}

func OncallListCommand() (cli.Command, error) {
Expand All @@ -14,7 +20,9 @@ func OncallListCommand() (cli.Command, error) {

func (c *OncallList) Help() string {
helpText := `
`
pd oncall list List on-calls
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

Expand All @@ -23,5 +31,55 @@ func (c *OncallList) Synopsis() string {
}

func (c *OncallList) Run(args []string) int {
var escalationPolicyIDs []string
var includes []string
var scheduleIDs []string
var timeZone string
var userIDs []string
var until string
var since string
var earliest bool

flags := c.Meta.FlagSet("on-call list")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.Var((*ArrayFlags)(&includes), "include", "Additional details to include (can be specified multiple times)")
flags.Var((*ArrayFlags)(&scheduleIDs), "schedule-id", "Only show for schedule ID (can be specified multiple times)")
flags.Var((*ArrayFlags)(&userIDs), "user-id", "Only show for user ID (can be specified multiple times)")
flags.Var((*ArrayFlags)(&escalationPolicyIDs), "escalationPolicy-id", "Only show for escalationPolicy ID (can be specified multiple times)")
flags.StringVar(&timeZone, "time-zone", "", "Time Zone")
flags.StringVar(&until, "until", "", "End of the time range over which you want to search")
flags.StringVar(&since, "since", "", "Start of the time range over which you want to search")
flags.BoolVar(&earliest, "earliest", false, "Return only the earliest on-call for each combination of escalation policy, escalation level, and user")

if err := flags.Parse(args); err != nil {
log.Error(err)
return -1
}
if err := c.Meta.Setup(); err != nil {
log.Error(err)
return -1
}
client := c.Meta.Client()
opts := pagerduty.ListOnCallOptions{
UserIDs: userIDs,
Includes: includes,
TimeZone: timeZone,
EscalationPolicyIDs: escalationPolicyIDs,
ScheduleIDs: scheduleIDs,
Until: until,
Since: since,
Earliest: earliest,
}
if oncs, err := client.ListOnCalls(opts); err != nil {
log.Error(err)
return -1
} else {
data, err := yaml.Marshal(oncs.OnCalls)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
}
return 0
}
6 changes: 3 additions & 3 deletions on_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type ListOnCallOptions struct {
UserIDs []string `url:"user_ids,omitempty,brackets"`
EscalationPolicyIDs []string `url:"escalation_policy_ids,omitempty,brackets"`
ScheduleIDs []string `url:"schedule_ids,omitempty,brackets"`
Since string `json:"since,omitempty"`
Until string `json:"until,omitempty"`
Earliest bool `json:"earliest,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Earliest bool `url:"earliest,omitempty"`
}

// ListOnCalls list the on-call entries during a given time range.
Expand Down

0 comments on commit 2e74576

Please sign in to comment.