improve support for handling UUIDs

This commit is contained in:
Finn 2021-09-01 20:24:56 -07:00
parent 096152394a
commit 2fa9670efe
13 changed files with 77 additions and 21 deletions

View file

@ -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)
}
},

View file

@ -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)
}

View file

@ -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)
}
},

View file

@ -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"

View file

@ -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
}
},

View file

@ -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]
}

View file

@ -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])

View file

@ -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) {

View file

@ -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())

View file

@ -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 {

View file

@ -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)
}

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=