From 7c5f87bd66a4f395eb2423e369d61160e5a43814 Mon Sep 17 00:00:00 2001 From: Rosco Nap Date: Tue, 4 Dec 2018 15:10:41 +0100 Subject: [PATCH] Added list and resend options --- README.md | 4 +-- cmd/component.go | 37 +++++++++++++++++++++ cmd/subscriber.go | 84 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 cmd/component.go diff --git a/README.md b/README.md index 422a6e7..a72af32 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # statuspage-cli -Statuspage.io CLI tool +Statuspage.io CLI tool ## Synopsis -This commandline tool interacts with the statuspage.io API. +This commandline tool interacts with the statuspage.io API. *note: Beware, this is work in progress and without warranty! It might be broken.* diff --git a/cmd/component.go b/cmd/component.go new file mode 100644 index 0000000..613711a --- /dev/null +++ b/cmd/component.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var componentCmd = &cobra.Command{ + Use: "component", + Short: "Manipulate statuspage components", +} + +var componentListCmd = &cobra.Command{ + Use: "list", + Example: "statuspage component list", + Short: "list component", + Long: `Lists all components`, + Run: func(cmd *cobra.Command, args []string) { + components, err := app.Client.GetAllComponents() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + for _, component := range components { + fmt.Println(&component) + } + + }, +} + +func init() { + RootCmd.AddCommand(componentCmd) + componentCmd.AddCommand(componentListCmd) +} diff --git a/cmd/subscriber.go b/cmd/subscriber.go index 7ffa46c..41d5835 100644 --- a/cmd/subscriber.go +++ b/cmd/subscriber.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "github.com/cloudrkt/statuspage-cli/email" + "git.proserve.nl/statuspage/email" "github.com/spf13/cobra" ) @@ -13,14 +13,49 @@ var subscriberCmd = &cobra.Command{ Short: "Manipulate subscribers", } +var subscriberListCmd = &cobra.Command{ + Use: "list", + Example: "statuspage subscriber list [type]", + Short: "list subscribers", + Long: `Lists subscribers`, + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + + listAllSubscribers := func() { + subscribers, err := app.Client.GetAllSubscribers() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + for _, subscriber := range subscribers { + fmt.Println(&subscriber) + } + } + + if len(args) < 1 { + listAllSubscribers() + } else { + switch args[0] { + case "sms": // TODO: Implement SMS subscriber list + fmt.Println("not implemented yet") + case "webhook": // TODO: Impelement webhook subscriber list + fmt.Println("not implemented yet") + default: + listAllSubscribers() + } + } + }, +} + var subscriberCreateCmd = &cobra.Command{ Use: "create", Example: "statuspage subscriber create [email@example.org]", Short: "create a subscriber", - Long: `Create a subscriber through email adres. The subsciber *needs* to + Long: `Create a subscriber through email adres. The subsciber *needs* to confirm the email from statuspage to receive notifications. The subscriber is then added to all the components by default.`, - Args: cobra.ExactArgs(1), + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { create := args[0] @@ -40,8 +75,7 @@ var subscriberCreateCmd = &cobra.Command{ fmt.Println(err) os.Exit(1) } - - fmt.Printf("Succesfully added: %v\n", create) + fmt.Printf("succesfully added: %v\n", create) }, } @@ -71,7 +105,7 @@ var subscriberDeleteCmd = &cobra.Command{ os.Exit(1) } - fmt.Printf("Succesfully deleted: %v\n", *subscriber.Email) + fmt.Printf("succesfully deleted: %v\n", *subscriber.Email) }, } @@ -95,7 +129,41 @@ var subscriberSearchCmd = &cobra.Command{ os.Exit(1) } - fmt.Println(subscriber.String()) + fmt.Printf(subscriber.String()) + }, +} + +var subscriberResendCmd = &cobra.Command{ + Use: "resend", + Example: "statuspage subscriber resend [email@example.org]", + Short: "Resend conformation email", + Long: "Resend the conformation email to a subscriber", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + address := args[0] + + if err := email.ValidateFormat(address); err != nil { + fmt.Println(err) + os.Exit(1) + } + + subscriber, err := app.Client.SearchEmailSubscriber(address) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if app.Debug { + fmt.Println("Found: ", subscriber) + } + + _, err = app.Client.ResendConformationEmail(subscriber) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Printf("succesfully resend conformation email to: %v\n", *subscriber.Email) }, } @@ -103,5 +171,7 @@ func init() { RootCmd.AddCommand(subscriberCmd) subscriberCmd.AddCommand(subscriberCreateCmd) subscriberCmd.AddCommand(subscriberDeleteCmd) + subscriberCmd.AddCommand(subscriberListCmd) subscriberCmd.AddCommand(subscriberSearchCmd) + subscriberCmd.AddCommand(subscriberResendCmd) }