forked from PagerDuty/go-pagerduty
/
standard_list.go
78 lines (66 loc) · 1.56 KB
/
standard_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"fmt"
"strings"
"github.com/erwan690/go-pagerduty"
"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type StandardList struct {
Meta
}
func (c *StandardList) Help() string {
helpText := `
pd standard list List all of the existing standards
Options:
-active
-resource-type Filter by resource type. Allowed values: technical_service
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}
func (c *StandardList) Synopsis() string {
return "List all of the existing standards"
}
func StandardListCommand() (cli.Command, error) {
return &StandardList{}, nil
}
func (c *StandardList) Run(args []string) int {
var active bool
var resourceType string
flags := c.Meta.FlagSet("standard list")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.BoolVar(&active, "active", false, "")
flags.StringVar(&resourceType, "resource-type", "", "Filter by resource type. Allowed values: technical_service")
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.ListStandardsOptions{
Active: active,
ResourceType: resourceType,
}
if res, err := client.ListStandards(context.Background(), opts); err != nil {
log.Error(err)
return -1
} else {
for i, r := range res.Standards {
if i > 0 {
fmt.Println("---")
}
data, err := yaml.Marshal(r)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
}
}
return 0
}