2021-01-31 03:14:33 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2021-09-02 03:24:56 +00:00
|
|
|
"fmt"
|
2021-01-31 03:14:33 +00:00
|
|
|
"log"
|
2021-02-18 08:50:47 +00:00
|
|
|
"strings"
|
2021-01-31 03:14:33 +00:00
|
|
|
|
2021-09-02 03:24:56 +00:00
|
|
|
"github.com/google/uuid"
|
2021-02-06 09:13:55 +00:00
|
|
|
"github.com/jedib0t/go-pretty/v6/table"
|
|
|
|
|
2021-01-31 03:14:33 +00:00
|
|
|
"gitlab.com/signald/signald-go/signald"
|
2021-02-18 08:50:47 +00:00
|
|
|
"gitlab.com/signald/signald-go/signald/client-protocol/v1"
|
2021-01-31 03:14:33 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 21:50:44 +00:00
|
|
|
const (
|
2021-02-03 01:13:31 +00:00
|
|
|
OutputFormatDefault = "default"
|
|
|
|
OutputFormatCSV = "csv"
|
|
|
|
OutputFormatTable = "table"
|
|
|
|
OutputFormatJSON = "json"
|
|
|
|
OutputFormatYAML = "yaml"
|
|
|
|
OutputFormatQR = "qr"
|
|
|
|
OutputFormatMarkdown = "md"
|
|
|
|
OutputFormatMan = "man"
|
2021-07-23 23:54:53 +00:00
|
|
|
OutputFormatQuiet = "quiet"
|
2021-02-02 21:50:44 +00:00
|
|
|
|
|
|
|
AnnotationNoSocketConnection = "no-socket"
|
|
|
|
)
|
|
|
|
|
2021-01-31 03:14:33 +00:00
|
|
|
var (
|
|
|
|
Signald *signald.Signald
|
|
|
|
|
|
|
|
OutputFormat string
|
|
|
|
)
|
|
|
|
|
2021-09-02 03:24:56 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-01-31 03:14:33 +00:00
|
|
|
func Must(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-02-06 09:13:55 +00:00
|
|
|
|
|
|
|
func StylizeTable(t table.Writer) {
|
|
|
|
t.SetStyle(table.StyleLight)
|
|
|
|
}
|
2021-02-18 08:50:47 +00:00
|
|
|
|
2021-09-02 03:24:56 +00:00
|
|
|
func StringToAddress(address string) (v1.JsonAddress, error) {
|
2021-02-18 08:50:47 +00:00
|
|
|
if strings.HasPrefix(address, "+") {
|
2021-09-02 03:24:56 +00:00
|
|
|
return v1.JsonAddress{Number: address}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := uuid.Parse(address); err == nil {
|
|
|
|
return v1.JsonAddress{UUID: address}, nil
|
2021-02-18 08:50:47 +00:00
|
|
|
}
|
2021-09-02 03:24:56 +00:00
|
|
|
return v1.JsonAddress{}, NewInvalidAddress(address)
|
2021-02-18 08:50:47 +00:00
|
|
|
}
|
2021-10-17 22:01:20 +00:00
|
|
|
|
|
|
|
func StringToAddressOrGroup(identifier string) (*v1.JsonAddress, string, error) {
|
|
|
|
// if it starts with a +, assume it's an e164
|
|
|
|
if strings.HasPrefix(identifier, "+") {
|
|
|
|
return &v1.JsonAddress{Number: identifier}, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it parses as a UUID, assume it's a UUID
|
|
|
|
if _, err := uuid.Parse(identifier); err == nil {
|
|
|
|
return &v1.JsonAddress{UUID: identifier}, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it doesn't start with a + and doesn't parse as a UUID, assume it's a group
|
|
|
|
return nil, identifier, nil
|
|
|
|
}
|