diff --git a/command/oncall_list.go b/command/oncall_list.go index 8be2dd54..2e3025e8 100644 --- a/command/oncall_list.go +++ b/command/oncall_list.go @@ -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) { @@ -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) } @@ -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 } diff --git a/on_call.go b/on_call.go index 66dd4152..b05f8862 100644 --- a/on_call.go +++ b/on_call.go @@ -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.