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

(feat)implement addon update/delete #4

Merged
merged 1 commit into from
Apr 2, 2016
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
51 changes: 51 additions & 0 deletions command/addon_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/cli"
"strings"
)

type AddonDelete struct {
Meta
}

func (c *AddonDelete) Help() string {
helpText := `
pd addon delete <ID> Delete details of an addon
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *AddonDelete) Synopsis() string {
return "Delete details of an addon"
}

func AddonDeleteCommand() (cli.Command, error) {
return &AddonDelete{}, nil
}

func (c *AddonDelete) Run(args []string) int {
flags := c.Meta.FlagSet("addon delete")
flags.Usage = func() { fmt.Println(c.Help()) }
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()
if len(flags.Args()) != 1 {
log.Error("Please specify addon id")
return -1
}
err := client.DeleteAddon(flags.Arg(0))
if err != nil {
log.Error(err)
return -1
}
return 0
}
74 changes: 74 additions & 0 deletions command/addon_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"encoding/json"
"fmt"
"github.com/PagerDuty/go-pagerduty"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/cli"
"strings"
)

type AddonUpdate struct {
Meta
}

func (c *AddonUpdate) Help() string {
helpText := `
pd addon update <ID> Update details of an addon
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

func (c *AddonUpdate) Synopsis() string {
return "Update details of an addon"
}

func AddonUpdateCommand() (cli.Command, error) {
return &AddonUpdate{}, nil
}

func (c *AddonUpdate) Run(args []string) int {
flags := c.Meta.FlagSet("addon update")
flags.Usage = func() { fmt.Println(c.Help()) }
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()
if len(flags.Args()) != 1 {
log.Error("Please specify addon id")
return -1
}
a, err := client.GetAddon(flags.Arg(0))
if err != nil {
log.Error(err)
return -1
}

oldData, err := json.MarshalIndent(a, "", " ")
if err != nil {
log.Error(err)
return -1
}

newData, err := TextEditor(oldData)
if err != nil {
log.Error(err)
return -1
}
var newAddon pagerduty.Addon
if err := json.Unmarshal(newData, &newAddon); err != nil {
log.Error(err)
return -1
}
if err := client.UpdateAddon(newAddon.ID, newAddon); err != nil {
log.Error(err)
return -1
}
return 0
}
2 changes: 2 additions & 0 deletions command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func loadCommands() map[string]cli.CommandFactory {
"addon list": AddonListCommand,
"addon install": AddonInstallCommand,
"addon show": AddonShowCommand,
"addon delete": AddonDeleteCommand,
"addon update": AddonUpdateCommand,

"escalation-policy list": EscalationPolicyListCommand,
"escalation-policy create": EscalationPolicyCreateCommand,
Expand Down