From f4487704cd4b07394231818de1ea765f002e1c15 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 3 Feb 2021 01:11:32 -0800 Subject: [PATCH] clean up some UX and improve validation --- .../cmd/account/register/register-account.go | 19 +++++++------- .../cmd/account/verify/verify-account.go | 26 ++++++------------- cmd/signaldctl/cmd/config/set/set-config.go | 9 ++++--- cmd/signaldctl/cmd/device/remove/remove.go | 2 +- .../accept/accept-invitation-to-group.go | 7 ++--- cmd/signaldctl/cmd/group/show/show-group.go | 7 ++--- .../cmd/message/react/react-to-message.go | 3 ++- .../cmd/message/read/mark-message-as-read.go | 7 ++--- .../cmd/message/send/send-message.go | 26 +++++++------------ cmd/signaldctl/cmd/version.go | 18 ++++++------- 10 files changed, 56 insertions(+), 68 deletions(-) diff --git a/cmd/signaldctl/cmd/account/register/register-account.go b/cmd/signaldctl/cmd/account/register/register-account.go index 534f9a7..f97504d 100644 --- a/cmd/signaldctl/cmd/account/register/register-account.go +++ b/cmd/signaldctl/cmd/account/register/register-account.go @@ -26,23 +26,23 @@ import ( ) var ( - phoneNumber string - voice bool + voice bool RegisterAccountCmd = &cobra.Command{ - Use: "register", + Use: "register [phone number]", Short: "begin the process of creating a new account", - PreRun: func(_ *cobra.Command, _ []string) { - if phoneNumber == "" { - log.Fatal("--phone-number required") + PreRun: func(cmd *cobra.Command, args []string) { + if len(args) != 1 { + common.Must(cmd.Help()) + log.Fatal("must specify phone number") } }, - Run: func(_ *cobra.Command, _ []string) { + Run: func(_ *cobra.Command, args []string) { requestID := signald.GenerateID() err := common.Signald.RawRequest(v0.LegacyRequest{ Type: "register", ID: requestID, - Username: phoneNumber, + Username: args[0], Voice: voice, }) if err != nil { @@ -52,7 +52,7 @@ var ( go common.Signald.Listen(c) response := signald.GetLegacyResponse(c, requestID) if response.Type == "verification_required" { - log.Println("verification code requested. submit with: signaldctl account verify --phone-number", phoneNumber, "--code XXX-XXX") + log.Println("verification code requested. submit with: signaldctl account verify", args[0], "") } else { log.Fatalf("unexpected response from signald when requesting verification code: %+v", response) } @@ -61,6 +61,5 @@ var ( ) func init() { - RegisterAccountCmd.Flags().StringVarP(&phoneNumber, "number", "n", "", "phone number being registered") RegisterAccountCmd.Flags().BoolVarP(&voice, "voice", "V", false, "request verification code be sent via an automated voice call (code is sent via SMS by default)") } diff --git a/cmd/signaldctl/cmd/account/verify/verify-account.go b/cmd/signaldctl/cmd/account/verify/verify-account.go index 2b032bb..151804a 100644 --- a/cmd/signaldctl/cmd/account/verify/verify-account.go +++ b/cmd/signaldctl/cmd/account/verify/verify-account.go @@ -26,27 +26,22 @@ import ( ) var ( - phoneNumber string - code string - VerifyAccountCmd = &cobra.Command{ - Use: "verify", + Use: "verify [phone number] [code]", Short: "verify an account and complete the registration process", - PreRun: func(_ *cobra.Command, _ []string) { - if phoneNumber == "" { - log.Fatal("--phone-number required") - } - if code == "" { - log.Fatal("--code required") + PreRun: func(cmd *cobra.Command, args []string) { + if len(args) != 2 { + common.Must(cmd.Help()) + log.Fatal("must specify phone number and code") } }, - Run: func(_ *cobra.Command, _ []string) { + Run: func(_ *cobra.Command, args []string) { requestID := signald.GenerateID() err := common.Signald.RawRequest(v0.LegacyRequest{ Type: "verify", ID: requestID, - Username: phoneNumber, - Code: code, + Username: args[0], + Code: args[1], }) if err != nil { log.Fatal("error sending request to signald: ", err) @@ -62,8 +57,3 @@ var ( }, } ) - -func init() { - VerifyAccountCmd.Flags().StringVarP(&phoneNumber, "phone-number", "n", "", "phone number being verified") - VerifyAccountCmd.Flags().StringVarP(&code, "code", "c", "", "verification code to submit") -} diff --git a/cmd/signaldctl/cmd/config/set/set-config.go b/cmd/signaldctl/cmd/config/set/set-config.go index ee3e587..34aeba5 100644 --- a/cmd/signaldctl/cmd/config/set/set-config.go +++ b/cmd/signaldctl/cmd/config/set/set-config.go @@ -21,15 +21,18 @@ import ( "github.com/spf13/cobra" + "gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/config" ) var ( SetConfigCmd = &cobra.Command{ - Use: "set", - Short: "send a message", - PreRun: func(_ *cobra.Command, args []string) { + Use: "set ", + Short: "send a message", + Annotations: map[string]string{common.AnnotationNoSocketConnection: "true"}, + PreRun: func(cmd *cobra.Command, args []string) { if len(args) != 2 { + common.Must(cmd.Help()) log.Fatal("unexpected number of arguments") } }, diff --git a/cmd/signaldctl/cmd/device/remove/remove.go b/cmd/signaldctl/cmd/device/remove/remove.go index 4b158ad..d8c69f6 100644 --- a/cmd/signaldctl/cmd/device/remove/remove.go +++ b/cmd/signaldctl/cmd/device/remove/remove.go @@ -30,7 +30,7 @@ var ( device int64 RemoveDeviceCmd = &cobra.Command{ - Use: "remove", + Use: "remove ", Short: "remove a linked device", PreRun: func(_ *cobra.Command, args []string) { if account == "" { 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 4dea28a..db73494 100644 --- a/cmd/signaldctl/cmd/group/accept/accept-invitation-to-group.go +++ b/cmd/signaldctl/cmd/group/accept/accept-invitation-to-group.go @@ -32,9 +32,9 @@ import ( var ( account string AcceptGroupInvitationCmd = &cobra.Command{ - Use: "accept", + Use: "accept [...]", Short: "accept an invitation to join a group", - PreRun: func(_ *cobra.Command, args []string) { + PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } @@ -42,7 +42,8 @@ var ( log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) == 0 { - log.Fatal("at least one group ID required") + common.Must(cmd.Help()) + log.Fatal("must specify at least one group id") } }, Run: func(_ *cobra.Command, args []string) { diff --git a/cmd/signaldctl/cmd/group/show/show-group.go b/cmd/signaldctl/cmd/group/show/show-group.go index 125742a..44daa65 100644 --- a/cmd/signaldctl/cmd/group/show/show-group.go +++ b/cmd/signaldctl/cmd/group/show/show-group.go @@ -32,9 +32,9 @@ import ( var ( account string ShowGroupCmd = &cobra.Command{ - Use: "show", + Use: "show []", Short: "show details about a group", - PreRun: func(_ *cobra.Command, args []string) { + PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } @@ -42,7 +42,8 @@ var ( log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) == 0 { - log.Fatal("at least one group ID required") + common.Must(cmd.Help()) + log.Fatal("must specify at least one group id") } }, Run: func(_ *cobra.Command, args []string) { diff --git a/cmd/signaldctl/cmd/message/react/react-to-message.go b/cmd/signaldctl/cmd/message/react/react-to-message.go index 393c62b..5db9085 100644 --- a/cmd/signaldctl/cmd/message/react/react-to-message.go +++ b/cmd/signaldctl/cmd/message/react/react-to-message.go @@ -41,7 +41,7 @@ var ( ReactMessageCmd = &cobra.Command{ Use: "react", Short: "react to a message", - PreRun: func(_ *cobra.Command, args []string) { + PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } @@ -49,6 +49,7 @@ var ( log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) != 1 { + common.Must(cmd.Help()) log.Fatal("must provide exactly one reaction") } }, 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 fe1c36d..f18670c 100644 --- a/cmd/signaldctl/cmd/message/read/mark-message-as-read.go +++ b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go @@ -30,9 +30,9 @@ var ( account string ReadMessageCmd = &cobra.Command{ - Use: "mark-read", + Use: "mark-read ", Short: "mark a message as read", - PreRun: func(_ *cobra.Command, args []string) { + PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } @@ -40,7 +40,8 @@ var ( log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) < 2 { - log.Fatal("Usage: signaldctl message mark-read ") + common.Must(cmd.Help()) + log.Fatal("must specify author and timestamp") } }, Run: func(_ *cobra.Command, args []string) { diff --git a/cmd/signaldctl/cmd/message/send/send-message.go b/cmd/signaldctl/cmd/message/send/send-message.go index de8b6b8..dd8d5cb 100644 --- a/cmd/signaldctl/cmd/message/send/send-message.go +++ b/cmd/signaldctl/cmd/message/send/send-message.go @@ -33,38 +33,34 @@ import ( var ( account string - to string - message string SendMessageCmd = &cobra.Command{ - Use: "send", + Use: "send | ", Short: "send a message", - PreRun: func(_ *cobra.Command, _ []string) { + PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount } if account == "" { log.Fatal("No account specified. Please specify with --account or set a default") } - if to == "" { - log.Fatal("--to required") - } - if message == "" { - log.Fatal("--message required") + if len(args) < 2 { + common.Must(cmd.Help()) + log.Fatal("must specify both destination (either group id or phone number) and message") } }, - Run: func(_ *cobra.Command, _ []string) { + Run: func(_ *cobra.Command, args []string) { go common.Signald.Listen(nil) req := v1.SendRequest{ Username: account, - MessageBody: message, + MessageBody: strings.Join(args[1:], " "), } - if strings.HasPrefix(to, "+") { - req.RecipientAddress = &v1.JsonAddress{Number: to} + if strings.HasPrefix(args[0], "+") { + req.RecipientAddress = &v1.JsonAddress{Number: args[0]} } else { - req.RecipientGroupID = to + req.RecipientGroupID = args[0] } resp, err := req.Submit(common.Signald) @@ -124,6 +120,4 @@ var ( func init() { SendMessageCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") - SendMessageCmd.Flags().StringVarP(&to, "to", "t", "", "phone number or group ID to send the message to") - SendMessageCmd.Flags().StringVarP(&message, "message", "m", "", "the body of the message to send") } diff --git a/cmd/signaldctl/cmd/version.go b/cmd/signaldctl/cmd/version.go index f29608d..9510c43 100644 --- a/cmd/signaldctl/cmd/version.go +++ b/cmd/signaldctl/cmd/version.go @@ -35,20 +35,18 @@ var versionCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { go common.Signald.Listen(nil) r := v1.VersionRequest{} - response, err := r.Submit(common.Signald) + signaldVersion, err := r.Submit(common.Signald) if err != nil { log.Fatal(err) } - output := []v1.JsonVersionMessage{ - response, - { - Name: common.Name, - Branch: common.Branch, - Commit: common.Commit, - Version: common.Version, - }, + signaldctlVersion := v1.JsonVersionMessage{ + Name: common.Name, + Branch: common.Branch, + Commit: common.Commit, + Version: common.Version, } + output := []v1.JsonVersionMessage{signaldVersion, signaldctlVersion} switch common.OutputFormat { case common.OutputFormatJSON: err := json.NewEncoder(os.Stdout).Encode(output) @@ -65,7 +63,7 @@ var versionCmd = &cobra.Command{ t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{"Name", "Version", "Branch", "Commit"}) t.AppendRow(table.Row{common.Name, common.Version, common.Branch, common.Commit}) - t.AppendRow(table.Row{response.Name, response.Version, response.Branch, response.Commit}) + t.AppendRow(table.Row{signaldVersion.Name, signaldVersion.Version, signaldVersion.Branch, signaldVersion.Commit}) if common.OutputFormat == common.OutputFormatCSV { t.RenderCSV() } else {