From 2fa9670efec9762d9553eef78f7fc870c92499d4 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 1 Sep 2021 20:24:56 -0700 Subject: [PATCH] improve support for handling UUIDs --- .../cmd/group/addmember/add-member.go | 5 +++- .../cmd/group/create/create-group.go | 5 +++- .../cmd/group/removemember/remove-member.go | 5 +++- .../group/update/role/update-group-role.go | 6 ++++- cmd/signaldctl/cmd/key/list/list-keys.go | 5 +++- cmd/signaldctl/cmd/key/qr/qr-key.go | 6 ++++- cmd/signaldctl/cmd/key/trust/trust-key.go | 6 ++++- cmd/signaldctl/cmd/key/trust_all/trust-all.go | 6 ++++- .../cmd/message/react/react-to-message.go | 5 +++- .../cmd/message/send/send-message.go | 22 ++++++++++------- cmd/signaldctl/common/signald.go | 24 ++++++++++++++++--- go.mod | 1 + go.sum | 2 ++ 13 files changed, 77 insertions(+), 21 deletions(-) diff --git a/cmd/signaldctl/cmd/group/addmember/add-member.go b/cmd/signaldctl/cmd/group/addmember/add-member.go index 73464bc..54bc20f 100644 --- a/cmd/signaldctl/cmd/group/addmember/add-member.go +++ b/cmd/signaldctl/cmd/group/addmember/add-member.go @@ -52,7 +52,10 @@ var ( } group = args[0] for _, member := range args[1:] { - address := common.StringToAddress(member) + address, err := common.StringToAddress(member) + if err != nil { + log.Fatal(err) + } members = append(members, &address) } }, diff --git a/cmd/signaldctl/cmd/group/create/create-group.go b/cmd/signaldctl/cmd/group/create/create-group.go index 8fdefb5..ca47d72 100644 --- a/cmd/signaldctl/cmd/group/create/create-group.go +++ b/cmd/signaldctl/cmd/group/create/create-group.go @@ -58,7 +58,10 @@ var ( } for _, member := range args[1:] { - address := common.StringToAddress(member) + address, err := common.StringToAddress(member) + if err != nil { + log.Fatal(err) + } req.Members = append(req.Members, &address) } diff --git a/cmd/signaldctl/cmd/group/removemember/remove-member.go b/cmd/signaldctl/cmd/group/removemember/remove-member.go index 49a2a6c..436496c 100644 --- a/cmd/signaldctl/cmd/group/removemember/remove-member.go +++ b/cmd/signaldctl/cmd/group/removemember/remove-member.go @@ -52,7 +52,10 @@ var ( } group = args[0] for _, member := range args[1:] { - address := common.StringToAddress(member) + address, err := common.StringToAddress(member) + if err != nil { + log.Fatal(err) + } members = append(members, &address) } }, diff --git a/cmd/signaldctl/cmd/group/update/role/update-group-role.go b/cmd/signaldctl/cmd/group/update/role/update-group-role.go index 8568b0d..5c69ab9 100644 --- a/cmd/signaldctl/cmd/group/update/role/update-group-role.go +++ b/cmd/signaldctl/cmd/group/update/role/update-group-role.go @@ -52,7 +52,11 @@ var ( log.Fatal("not enough arguments provided") } group = args[0] - address = common.StringToAddress(args[1]) + var err error + address, err = common.StringToAddress(args[1]) + if err != nil { + log.Fatal(err) + } switch strings.ToLower(args[2]) { case "default": role = "DEFAULT" diff --git a/cmd/signaldctl/cmd/key/list/list-keys.go b/cmd/signaldctl/cmd/key/list/list-keys.go index b20ca26..20ee54d 100644 --- a/cmd/signaldctl/cmd/key/list/list-keys.go +++ b/cmd/signaldctl/cmd/key/list/list-keys.go @@ -44,7 +44,10 @@ var ( log.Fatal("No account specified. Please specify with --account or set a default") } if len(args) >= 1 { - a := common.StringToAddress(args[0]) + a, err := common.StringToAddress(args[0]) + if err != nil { + log.Fatal(err) + } address = &a } }, diff --git a/cmd/signaldctl/cmd/key/qr/qr-key.go b/cmd/signaldctl/cmd/key/qr/qr-key.go index 205db36..8ad927d 100644 --- a/cmd/signaldctl/cmd/key/qr/qr-key.go +++ b/cmd/signaldctl/cmd/key/qr/qr-key.go @@ -50,7 +50,11 @@ var ( common.Must(cmd.Help()) log.Fatal("please specify an address") } - address = common.StringToAddress(args[0]) + var err error + address, err = common.StringToAddress(args[0]) + if err != nil { + log.Fatal(err) + } if len(args) > 1 { safetyNumber = args[1] } diff --git a/cmd/signaldctl/cmd/key/trust/trust-key.go b/cmd/signaldctl/cmd/key/trust/trust-key.go index 6dd0195..da2224c 100644 --- a/cmd/signaldctl/cmd/key/trust/trust-key.go +++ b/cmd/signaldctl/cmd/key/trust/trust-key.go @@ -46,7 +46,11 @@ var ( common.Must(cmd.Help()) log.Fatal("please specify an address and a safety number") } - address = common.StringToAddress(args[0]) + var err error + address, err = common.StringToAddress(args[0]) + if err != nil { + log.Fatal(err) + } safetyNumber = args[1] if len(args) > 2 { level = strings.ToUpper(args[2]) diff --git a/cmd/signaldctl/cmd/key/trust_all/trust-all.go b/cmd/signaldctl/cmd/key/trust_all/trust-all.go index 3eb10a2..e474b2c 100644 --- a/cmd/signaldctl/cmd/key/trust_all/trust-all.go +++ b/cmd/signaldctl/cmd/key/trust_all/trust-all.go @@ -44,7 +44,11 @@ var ( log.Fatal("No account specified. Please specify with --account or set a default") } for _, address := range args { - addresses = append(addresses, common.StringToAddress(address)) + address, err := common.StringToAddress(address) + if err != nil { + log.Fatal(err) + } + addresses = append(addresses, address) } }, Run: func(_ *cobra.Command, _ []string) { diff --git a/cmd/signaldctl/cmd/message/react/react-to-message.go b/cmd/signaldctl/cmd/message/react/react-to-message.go index be66ebc..d91c2f3 100644 --- a/cmd/signaldctl/cmd/message/react/react-to-message.go +++ b/cmd/signaldctl/cmd/message/react/react-to-message.go @@ -53,8 +53,11 @@ var ( if len(args) != 3 { common.Must(cmd.Help()) } - author = common.StringToAddress(args[0]) var err error + author, err = common.StringToAddress(args[0]) + if err != nil { + log.Fatal(err) + } timestamp, err = strconv.ParseInt(args[1], 10, 64) if err != nil { log.Fatal("Unable to parse timestamp", args[1], ":", err.Error()) diff --git a/cmd/signaldctl/cmd/message/send/send-message.go b/cmd/signaldctl/cmd/message/send/send-message.go index b0cd39d..f8bf02c 100644 --- a/cmd/signaldctl/cmd/message/send/send-message.go +++ b/cmd/signaldctl/cmd/message/send/send-message.go @@ -35,6 +35,8 @@ import ( var ( account string + toAddress *v1.JsonAddress + toGroup string attachments []string SendMessageCmd = &cobra.Command{ @@ -52,20 +54,22 @@ var ( common.Must(cmd.Help()) log.Fatal("must specify both destination (either group id or phone number) and message") } + to, err := common.StringToAddress(args[0]) + if err != nil { + toGroup = args[0] + } else { + toAddress = &to + } }, Run: func(_ *cobra.Command, args []string) { go common.Signald.Listen(nil) req := v1.SendRequest{ - Username: account, - MessageBody: strings.Join(args[1:], " "), - Attachments: []*v0.JsonAttachment{}, - } - - if strings.HasPrefix(args[0], "+") { - req.RecipientAddress = &v1.JsonAddress{Number: args[0]} - } else { - req.RecipientGroupID = args[0] + Username: account, + MessageBody: strings.Join(args[1:], " "), + Attachments: []*v0.JsonAttachment{}, + RecipientAddress: toAddress, + RecipientGroupID: toGroup, } for _, attachment := range attachments { diff --git a/cmd/signaldctl/common/signald.go b/cmd/signaldctl/common/signald.go index 8ffc145..3db14e8 100644 --- a/cmd/signaldctl/common/signald.go +++ b/cmd/signaldctl/common/signald.go @@ -1,9 +1,11 @@ package common import ( + "fmt" "log" "strings" + "github.com/google/uuid" "github.com/jedib0t/go-pretty/v6/table" "gitlab.com/signald/signald-go/signald" @@ -30,6 +32,18 @@ var ( OutputFormat string ) +type InvalidAddressError struct { + invalidAddress string +} + +func NewInvalidAddress(i string) InvalidAddressError { + return InvalidAddressError{invalidAddress: i} +} + +func (i InvalidAddressError) Error() string { + return fmt.Sprintf("invalid address: %s", i.invalidAddress) +} + func Must(err error) { if err != nil { log.Fatal(err) @@ -40,9 +54,13 @@ func StylizeTable(t table.Writer) { t.SetStyle(table.StyleLight) } -func StringToAddress(address string) v1.JsonAddress { +func StringToAddress(address string) (v1.JsonAddress, error) { if strings.HasPrefix(address, "+") { - return v1.JsonAddress{Number: address} + return v1.JsonAddress{Number: address}, nil } - return v1.JsonAddress{UUID: address} + + if _, err := uuid.Parse(address); err == nil { + return v1.JsonAddress{UUID: address}, nil + } + return v1.JsonAddress{}, NewInvalidAddress(address) } diff --git a/go.mod b/go.mod index 6cc870b..a08f00b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module gitlab.com/signald/signald-go go 1.14 require ( + github.com/google/uuid v1.3.0 github.com/jedib0t/go-pretty/v6 v6.1.0 github.com/kr/text v0.2.0 // indirect github.com/mattn/go-runewidth v0.0.10 // indirect diff --git a/go.sum b/go.sum index b61d002..d22c029 100644 --- a/go.sum +++ b/go.sum @@ -65,6 +65,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=