clean up some UX and improve validation

This commit is contained in:
Finn 2021-02-03 01:11:32 -08:00
parent 4fb2cd3147
commit f4487704cd
10 changed files with 56 additions and 68 deletions

View file

@ -26,23 +26,23 @@ import (
) )
var ( var (
phoneNumber string voice bool
voice bool
RegisterAccountCmd = &cobra.Command{ RegisterAccountCmd = &cobra.Command{
Use: "register", Use: "register [phone number]",
Short: "begin the process of creating a new account", Short: "begin the process of creating a new account",
PreRun: func(_ *cobra.Command, _ []string) { PreRun: func(cmd *cobra.Command, args []string) {
if phoneNumber == "" { if len(args) != 1 {
log.Fatal("--phone-number required") common.Must(cmd.Help())
log.Fatal("must specify phone number")
} }
}, },
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, args []string) {
requestID := signald.GenerateID() requestID := signald.GenerateID()
err := common.Signald.RawRequest(v0.LegacyRequest{ err := common.Signald.RawRequest(v0.LegacyRequest{
Type: "register", Type: "register",
ID: requestID, ID: requestID,
Username: phoneNumber, Username: args[0],
Voice: voice, Voice: voice,
}) })
if err != nil { if err != nil {
@ -52,7 +52,7 @@ var (
go common.Signald.Listen(c) go common.Signald.Listen(c)
response := signald.GetLegacyResponse(c, requestID) response := signald.GetLegacyResponse(c, requestID)
if response.Type == "verification_required" { 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], "<code>")
} else { } else {
log.Fatalf("unexpected response from signald when requesting verification code: %+v", response) log.Fatalf("unexpected response from signald when requesting verification code: %+v", response)
} }
@ -61,6 +61,5 @@ var (
) )
func init() { 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)") RegisterAccountCmd.Flags().BoolVarP(&voice, "voice", "V", false, "request verification code be sent via an automated voice call (code is sent via SMS by default)")
} }

View file

@ -26,27 +26,22 @@ import (
) )
var ( var (
phoneNumber string
code string
VerifyAccountCmd = &cobra.Command{ VerifyAccountCmd = &cobra.Command{
Use: "verify", Use: "verify [phone number] [code]",
Short: "verify an account and complete the registration process", Short: "verify an account and complete the registration process",
PreRun: func(_ *cobra.Command, _ []string) { PreRun: func(cmd *cobra.Command, args []string) {
if phoneNumber == "" { if len(args) != 2 {
log.Fatal("--phone-number required") common.Must(cmd.Help())
} log.Fatal("must specify phone number and code")
if code == "" {
log.Fatal("--code required")
} }
}, },
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, args []string) {
requestID := signald.GenerateID() requestID := signald.GenerateID()
err := common.Signald.RawRequest(v0.LegacyRequest{ err := common.Signald.RawRequest(v0.LegacyRequest{
Type: "verify", Type: "verify",
ID: requestID, ID: requestID,
Username: phoneNumber, Username: args[0],
Code: code, Code: args[1],
}) })
if err != nil { if err != nil {
log.Fatal("error sending request to signald: ", err) 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")
}

View file

@ -21,15 +21,18 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/cmd/signaldctl/config" "gitlab.com/signald/signald-go/cmd/signaldctl/config"
) )
var ( var (
SetConfigCmd = &cobra.Command{ SetConfigCmd = &cobra.Command{
Use: "set", Use: "set <key> <value>",
Short: "send a message", Short: "send a message",
PreRun: func(_ *cobra.Command, args []string) { Annotations: map[string]string{common.AnnotationNoSocketConnection: "true"},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) != 2 { if len(args) != 2 {
common.Must(cmd.Help())
log.Fatal("unexpected number of arguments") log.Fatal("unexpected number of arguments")
} }
}, },

View file

@ -30,7 +30,7 @@ var (
device int64 device int64
RemoveDeviceCmd = &cobra.Command{ RemoveDeviceCmd = &cobra.Command{
Use: "remove", Use: "remove <device id>",
Short: "remove a linked device", Short: "remove a linked device",
PreRun: func(_ *cobra.Command, args []string) { PreRun: func(_ *cobra.Command, args []string) {
if account == "" { if account == "" {

View file

@ -32,9 +32,9 @@ import (
var ( var (
account string account string
AcceptGroupInvitationCmd = &cobra.Command{ AcceptGroupInvitationCmd = &cobra.Command{
Use: "accept", Use: "accept <group id> [<group id>...]",
Short: "accept an invitation to join a group", Short: "accept an invitation to join a group",
PreRun: func(_ *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
@ -42,7 +42,8 @@ var (
log.Fatal("No account specified. Please specify with --account or set a default") log.Fatal("No account specified. Please specify with --account or set a default")
} }
if len(args) == 0 { 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) { Run: func(_ *cobra.Command, args []string) {

View file

@ -32,9 +32,9 @@ import (
var ( var (
account string account string
ShowGroupCmd = &cobra.Command{ ShowGroupCmd = &cobra.Command{
Use: "show", Use: "show <group id> [<group id>]",
Short: "show details about a group", Short: "show details about a group",
PreRun: func(_ *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
@ -42,7 +42,8 @@ var (
log.Fatal("No account specified. Please specify with --account or set a default") log.Fatal("No account specified. Please specify with --account or set a default")
} }
if len(args) == 0 { 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) { Run: func(_ *cobra.Command, args []string) {

View file

@ -41,7 +41,7 @@ var (
ReactMessageCmd = &cobra.Command{ ReactMessageCmd = &cobra.Command{
Use: "react", Use: "react",
Short: "react to a message", Short: "react to a message",
PreRun: func(_ *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
@ -49,6 +49,7 @@ var (
log.Fatal("No account specified. Please specify with --account or set a default") log.Fatal("No account specified. Please specify with --account or set a default")
} }
if len(args) != 1 { if len(args) != 1 {
common.Must(cmd.Help())
log.Fatal("must provide exactly one reaction") log.Fatal("must provide exactly one reaction")
} }
}, },

View file

@ -30,9 +30,9 @@ var (
account string account string
ReadMessageCmd = &cobra.Command{ ReadMessageCmd = &cobra.Command{
Use: "mark-read", Use: "mark-read <author> <timestamp>",
Short: "mark a message as read", Short: "mark a message as read",
PreRun: func(_ *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
@ -40,7 +40,8 @@ var (
log.Fatal("No account specified. Please specify with --account or set a default") log.Fatal("No account specified. Please specify with --account or set a default")
} }
if len(args) < 2 { if len(args) < 2 {
log.Fatal("Usage: signaldctl message mark-read <author> <timestamp>") common.Must(cmd.Help())
log.Fatal("must specify author and timestamp")
} }
}, },
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {

View file

@ -33,38 +33,34 @@ import (
var ( var (
account string account string
to string
message string
SendMessageCmd = &cobra.Command{ SendMessageCmd = &cobra.Command{
Use: "send", Use: "send <group id> | <phone number> <message>",
Short: "send a message", Short: "send a message",
PreRun: func(_ *cobra.Command, _ []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
log.Fatal("No account specified. Please specify with --account or set a default") log.Fatal("No account specified. Please specify with --account or set a default")
} }
if to == "" { if len(args) < 2 {
log.Fatal("--to required") common.Must(cmd.Help())
} log.Fatal("must specify both destination (either group id or phone number) and message")
if message == "" {
log.Fatal("--message required")
} }
}, },
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, args []string) {
go common.Signald.Listen(nil) go common.Signald.Listen(nil)
req := v1.SendRequest{ req := v1.SendRequest{
Username: account, Username: account,
MessageBody: message, MessageBody: strings.Join(args[1:], " "),
} }
if strings.HasPrefix(to, "+") { if strings.HasPrefix(args[0], "+") {
req.RecipientAddress = &v1.JsonAddress{Number: to} req.RecipientAddress = &v1.JsonAddress{Number: args[0]}
} else { } else {
req.RecipientGroupID = to req.RecipientGroupID = args[0]
} }
resp, err := req.Submit(common.Signald) resp, err := req.Submit(common.Signald)
@ -124,6 +120,4 @@ var (
func init() { func init() {
SendMessageCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") 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")
} }

View file

@ -35,20 +35,18 @@ var versionCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
go common.Signald.Listen(nil) go common.Signald.Listen(nil)
r := v1.VersionRequest{} r := v1.VersionRequest{}
response, err := r.Submit(common.Signald) signaldVersion, err := r.Submit(common.Signald)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
output := []v1.JsonVersionMessage{ signaldctlVersion := v1.JsonVersionMessage{
response, Name: common.Name,
{ Branch: common.Branch,
Name: common.Name, Commit: common.Commit,
Branch: common.Branch, Version: common.Version,
Commit: common.Commit,
Version: common.Version,
},
} }
output := []v1.JsonVersionMessage{signaldVersion, signaldctlVersion}
switch common.OutputFormat { switch common.OutputFormat {
case common.OutputFormatJSON: case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(output) err := json.NewEncoder(os.Stdout).Encode(output)
@ -65,7 +63,7 @@ var versionCmd = &cobra.Command{
t.SetOutputMirror(os.Stdout) t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Name", "Version", "Branch", "Commit"}) t.AppendHeader(table.Row{"Name", "Version", "Branch", "Commit"})
t.AppendRow(table.Row{common.Name, common.Version, common.Branch, common.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 { if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV() t.RenderCSV()
} else { } else {