From 976744198ec5783132d5d5fccb0feb608dbc80ed Mon Sep 17 00:00:00 2001 From: Finn Date: Sun, 28 Feb 2021 13:25:34 -0800 Subject: [PATCH] Improve error handling in signaldctl --- cmd/signaldctl/cmd/device/list/list.go | 3 ++- cmd/signaldctl/cmd/device/remove/remove.go | 14 ++++++++++++-- .../cmd/group/accept/accept-invitation-to-group.go | 1 + cmd/signaldctl/cmd/group/create/create-group.go | 1 + cmd/signaldctl/cmd/group/leave/leave-group.go | 1 + cmd/signaldctl/cmd/group/list/list-group.go | 3 ++- cmd/signaldctl/cmd/group/show/show-group.go | 1 + .../cmd/message/react/react-to-message.go | 1 + .../cmd/message/read/mark-message-as-read.go | 1 + cmd/signaldctl/cmd/message/send/send-message.go | 3 ++- 10 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/signaldctl/cmd/device/list/list.go b/cmd/signaldctl/cmd/device/list/list.go index be433e9..6305eae 100644 --- a/cmd/signaldctl/cmd/device/list/list.go +++ b/cmd/signaldctl/cmd/device/list/list.go @@ -35,11 +35,12 @@ var ( ListDeviceCmd = &cobra.Command{ Use: "list", Short: "list all devices linked to the account", - PreRun: func(_ *cobra.Command, args []string) { + PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } }, diff --git a/cmd/signaldctl/cmd/device/remove/remove.go b/cmd/signaldctl/cmd/device/remove/remove.go index d8c69f6..ec0bd51 100644 --- a/cmd/signaldctl/cmd/device/remove/remove.go +++ b/cmd/signaldctl/cmd/device/remove/remove.go @@ -17,6 +17,7 @@ package remove import ( "log" + "strconv" "github.com/spf13/cobra" @@ -32,13 +33,23 @@ var ( RemoveDeviceCmd = &cobra.Command{ Use: "remove ", Short: "remove a linked device", - PreRun: func(_ *cobra.Command, args []string) { + PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } + if len(args) != 1 { + common.Must(cmd.Help()) + log.Fatal("must specify a device ID") + } + d, err := strconv.ParseInt(args[0], 10, 64) + if err != nil { + log.Fatal("unable to parse number: " + args[0]) + } + device = d }, Run: func(_ *cobra.Command, args []string) { go common.Signald.Listen(nil) @@ -56,6 +67,5 @@ var ( func init() { RemoveDeviceCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") - RemoveDeviceCmd.Flags().Int64VarP(&device, "device", "d", 0, "device ID to remove") common.Must(RemoveDeviceCmd.MarkFlagRequired("device")) } diff --git a/cmd/signaldctl/cmd/group/accept/accept-invitation-to-group.go b/cmd/signaldctl/cmd/group/accept/accept-invitation-to-group.go index 3ba6265..832ef27 100644 --- a/cmd/signaldctl/cmd/group/accept/accept-invitation-to-group.go +++ b/cmd/signaldctl/cmd/group/accept/accept-invitation-to-group.go @@ -39,6 +39,7 @@ var ( account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) == 0 { diff --git a/cmd/signaldctl/cmd/group/create/create-group.go b/cmd/signaldctl/cmd/group/create/create-group.go index 011cb92..8fdefb5 100644 --- a/cmd/signaldctl/cmd/group/create/create-group.go +++ b/cmd/signaldctl/cmd/group/create/create-group.go @@ -42,6 +42,7 @@ var ( account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) == 0 { diff --git a/cmd/signaldctl/cmd/group/leave/leave-group.go b/cmd/signaldctl/cmd/group/leave/leave-group.go index a3c2ef6..7bde547 100644 --- a/cmd/signaldctl/cmd/group/leave/leave-group.go +++ b/cmd/signaldctl/cmd/group/leave/leave-group.go @@ -39,6 +39,7 @@ var ( account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) == 0 { diff --git a/cmd/signaldctl/cmd/group/list/list-group.go b/cmd/signaldctl/cmd/group/list/list-group.go index b1ac079..b69ce21 100644 --- a/cmd/signaldctl/cmd/group/list/list-group.go +++ b/cmd/signaldctl/cmd/group/list/list-group.go @@ -34,11 +34,12 @@ var ( ListGroupCmd = &cobra.Command{ Use: "list", Short: "return a list of Signal groups", - PreRun: func(_ *cobra.Command, _ []string) { + PreRun: func(cmd *cobra.Command, _ []string) { if account == "" { account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } }, diff --git a/cmd/signaldctl/cmd/group/show/show-group.go b/cmd/signaldctl/cmd/group/show/show-group.go index 47f2415..f7dad3d 100644 --- a/cmd/signaldctl/cmd/group/show/show-group.go +++ b/cmd/signaldctl/cmd/group/show/show-group.go @@ -39,6 +39,7 @@ var ( account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) == 0 { diff --git a/cmd/signaldctl/cmd/message/react/react-to-message.go b/cmd/signaldctl/cmd/message/react/react-to-message.go index c795da1..be66ebc 100644 --- a/cmd/signaldctl/cmd/message/react/react-to-message.go +++ b/cmd/signaldctl/cmd/message/react/react-to-message.go @@ -47,6 +47,7 @@ var ( account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) != 3 { diff --git a/cmd/signaldctl/cmd/message/read/mark-message-as-read.go b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go index f3ac826..95b0500 100644 --- a/cmd/signaldctl/cmd/message/read/mark-message-as-read.go +++ b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go @@ -37,6 +37,7 @@ var ( account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) < 2 { diff --git a/cmd/signaldctl/cmd/message/send/send-message.go b/cmd/signaldctl/cmd/message/send/send-message.go index d274bda..2f9724a 100644 --- a/cmd/signaldctl/cmd/message/send/send-message.go +++ b/cmd/signaldctl/cmd/message/send/send-message.go @@ -35,13 +35,14 @@ var ( account string SendMessageCmd = &cobra.Command{ - Use: "send | ", + Use: "send ", Short: "send a message", PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } if account == "" { + common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) < 2 {