Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor CLI #73

Merged
merged 2 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func loadCommands() map[string]cli.CommandFactory {
"user notification-rule delete": UserNotificationRuleDeleteCommand,
"user notification-rule show": UserNotificationRuleShowCommand,
"user notification-rule update": UserNotificationRuleUpdateCommand,

"vendor list": VendorListCommand,
"vendor show": VendorShowCommand,
}
}

Expand Down
71 changes: 71 additions & 0 deletions command/vendor_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

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

type VendorList struct {
Meta
}

func VendorListCommand() (cli.Command, error) {
return &VendorList{}, nil
}

func (c *VendorList) Help() string {
helpText := `
pd vendor list List vendors

Options:

-query Filter vendors with certain name

`
return strings.TrimSpace(helpText)
}

func (c *VendorList) Synopsis() string {
return "List vendors within PagerDuty, optionally filtered by a search query"
}

func (c *VendorList) Run(args []string) int {
var query string

flags := c.Meta.FlagSet("user list")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&query, "query", "", "Show vendors whose names contain the query")

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.ListVendorOptions{
Query: query,
}
if resp, err := client.ListVendors(opts); err != nil {
log.Error(err)
return -1
} else {
for i, p := range resp.Vendors {
fmt.Println("Entry: ", i)
data, err := yaml.Marshal(p)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
}
}

return 0
}
63 changes: 63 additions & 0 deletions command/vendor_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/cli"
"gopkg.in/yaml.v2"
"strings"
)

type VendorShow struct {
Meta
}

func VendorShowCommand() (cli.Command, error) {
return &VendorShow{}, nil
}

func (c *VendorShow) Help() string {
helpText := `
vendor show Show details of an individual vendor

Options:
-id Vendor ID
`
return strings.TrimSpace(helpText)
}

func (c *VendorShow) Synopsis() string { return "Get details about a vendor" }

func (c *VendorShow) Run(args []string) int {
log.Println(args)
var id string
flags := c.Meta.FlagSet("vendor show")
flags.Usage = func() { fmt.Println(c.Help()) }
flags.StringVar(&id, "id", "", "Vendor ID")
if err := flags.Parse(args); err != nil {
log.Errorln(err)
return -1
}
if err := c.Meta.Setup(); err != nil {
log.Error(err)
return -1
}

if id == "" {
log.Error("You must specify the vendor id using -id flag")
return -1
}
client := c.Meta.Client()
result, err := client.GetVendor(id)
if err != nil {
log.Error(err)
return -1
}
data, err := yaml.Marshal(result)
if err != nil {
log.Error(err)
return -1
}
fmt.Println(string(data))
return 0
}