Improve error handling in signaldctl

This commit is contained in:
Finn 2021-02-28 13:25:34 -08:00
parent 60778761c2
commit 976744198e
10 changed files with 24 additions and 5 deletions

View file

@ -35,11 +35,12 @@ var (
ListDeviceCmd = &cobra.Command{ ListDeviceCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "list all devices linked to the account", Short: "list all devices linked to the account",
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
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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")
} }
}, },

View file

@ -17,6 +17,7 @@ package remove
import ( import (
"log" "log"
"strconv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -32,13 +33,23 @@ var (
RemoveDeviceCmd = &cobra.Command{ RemoveDeviceCmd = &cobra.Command{
Use: "remove <device id>", Use: "remove <device id>",
Short: "remove a linked device", Short: "remove a linked device",
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
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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 {
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) { Run: func(_ *cobra.Command, args []string) {
go common.Signald.Listen(nil) go common.Signald.Listen(nil)
@ -56,6 +67,5 @@ var (
func init() { func init() {
RemoveDeviceCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") 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")) common.Must(RemoveDeviceCmd.MarkFlagRequired("device"))
} }

View file

@ -39,6 +39,7 @@ var (
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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 {

View file

@ -42,6 +42,7 @@ var (
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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 {

View file

@ -39,6 +39,7 @@ var (
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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 {

View file

@ -34,11 +34,12 @@ var (
ListGroupCmd = &cobra.Command{ ListGroupCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "return a list of Signal groups", Short: "return a list of Signal groups",
PreRun: func(_ *cobra.Command, _ []string) { PreRun: func(cmd *cobra.Command, _ []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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")
} }
}, },

View file

@ -39,6 +39,7 @@ var (
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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 {

View file

@ -47,6 +47,7 @@ var (
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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) != 3 { if len(args) != 3 {

View file

@ -37,6 +37,7 @@ var (
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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 {

View file

@ -35,13 +35,14 @@ var (
account string account string
SendMessageCmd = &cobra.Command{ SendMessageCmd = &cobra.Command{
Use: "send <group id> | <phone number> <message>", Use: "send <group id | phone number> <message>",
Short: "send a message", Short: "send a message",
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
} }
if account == "" { if account == "" {
common.Must(cmd.Help())
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 {