Add flag to register accounts on the testing server

also move account list to v1
This commit is contained in:
Finn 2021-07-09 00:04:46 -07:00
parent fdcaabfba4
commit eae4dbc944
3 changed files with 48 additions and 54 deletions

View file

@ -17,6 +17,7 @@ package link
import (
"encoding/json"
"fmt"
"log"
"os"
@ -27,36 +28,35 @@ import (
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/signald"
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
)
var (
noWait bool
testing bool
deviceName string
LinkAccountCmd = &cobra.Command{
Use: "link",
Short: "create a local account by linking to an existing Signal account",
Run: func(_ *cobra.Command, _ []string) {
requestID := signald.GenerateID()
err := common.Signald.RawRequest(v0.LegacyRequest{Type: "link", ID: requestID, Version: "v0"})
if err != nil {
log.Fatal("error sending request: ", err)
}
c := make(chan v0.LegacyResponse)
go common.Signald.Listen(c)
uri := signald.GetLegacyResponse(c, requestID)
if uri.Type != "linking_uri" {
log.Fatalf("unexpected response from signald when requesting link: %+v", uri)
go common.Signald.Listen(nil)
req := v1.GenerateLinkingURIRequest{}
if testing {
req.Server = signald.StagingServerUUID
}
response, err := req.Submit(common.Signald)
if err != nil {
log.Fatal(err)
}
switch common.OutputFormat {
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(uri.Data.URI)
err := json.NewEncoder(os.Stdout).Encode(response.Uri)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(uri.Data.URI)
err := yaml.NewEncoder(os.Stdout).Encode(response.Uri)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
@ -64,7 +64,7 @@ var (
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"URI"})
t.AppendRow(table.Row{uri.Data.URI})
t.AppendRow(table.Row{response.Uri})
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
@ -72,29 +72,31 @@ var (
t.Render()
}
case common.OutputFormatQR, common.OutputFormatDefault:
qrterminal.Generate(uri.Data.URI, qrterminal.M, os.Stdout)
qrterminal.Generate(response.Uri, qrterminal.M, os.Stdout)
default:
log.Fatal("unsupported output format")
}
if noWait {
return
finishReq := v1.FinishLinkRequest{
DeviceName: deviceName,
SessionId: response.SessionId,
}
finish := signald.GetLegacyResponse(c, requestID)
if finish.Type == "linking_successful" {
log.Println("linking successful")
return
}
if finish.Type == "linking_error" {
log.Fatal("error from signald:", finish.Data.Message)
} else {
log.Fatal("unexpected message from signald:", finish.Data.Message)
_, err = finishReq.Submit(common.Signald)
if err != nil {
log.Fatal(err)
}
log.Println("linking successful")
},
}
)
func init() {
LinkAccountCmd.Flags().BoolVar(&noWait, "no-wait", false, "return after the linking URI is printed to stdout. By default, signaldctl will wait until the server acknowledges that the linking has completed before exiting")
name := "signald"
if hostname, err := os.Hostname(); err == nil {
name = fmt.Sprintf("signald on %s", hostname)
}
LinkAccountCmd.Flags().BoolVarP(&testing, "testing", "t", false, "use the Signal testing server")
LinkAccountCmd.Flags().StringVarP(&deviceName, "device-name", "n", name, "the name of this device. shown to other devices on the signal account")
}

View file

@ -25,8 +25,7 @@ import (
"gopkg.in/yaml.v2"
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/signald"
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
)
var (
@ -34,46 +33,33 @@ var (
Use: "list",
Short: "return a list of accounts",
Run: func(_ *cobra.Command, _ []string) {
requestID := signald.GenerateID()
err := common.Signald.RawRequest(v0.LegacyRequest{
Type: "list_accounts",
ID: requestID,
Version: "v0",
})
go common.Signald.Listen(nil)
req := v1.ListAccountsRequest{}
accounts, err := req.Submit(common.Signald)
if err != nil {
log.Fatal("error sending request: ", err)
}
c := make(chan v0.LegacyResponse)
go common.Signald.Listen(c)
var accounts []v0.Account
for {
message := <-c
if message.ID == requestID {
accounts = message.Data.Accounts
break
}
log.Fatal(err)
}
switch common.OutputFormat {
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(accounts)
err := json.NewEncoder(os.Stdout).Encode(accounts.Accounts)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(accounts)
err := yaml.NewEncoder(os.Stdout).Encode(accounts.Accounts)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Phone Number", "UUID", "Device ID", "Subscribed"})
t.AppendHeader(table.Row{"Phone Number", "UUID", "Device ID"})
for _, account := range accounts {
t.AppendRow(table.Row{account.Username, account.UUID, account.DeviceID, account.Subscribed})
for _, account := range accounts.Accounts {
t.AppendRow(table.Row{account.Address.Number, account.Address.UUID, account.DeviceId})
}
if common.OutputFormat == common.OutputFormatCSV {

View file

@ -25,12 +25,14 @@ import (
"gopkg.in/yaml.v2"
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/signald"
v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
)
var (
voice bool
captcha string
testing bool
RegisterAccountCmd = &cobra.Command{
Use: "register [phone number]",
@ -48,6 +50,9 @@ var (
Voice: voice,
Captcha: captcha,
}
if testing {
req.Server = signald.StagingServerUUID
}
resp, err := req.Submit(common.Signald)
if err != nil {
log.Fatal(err)
@ -76,4 +81,5 @@ var (
func init() {
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().StringVarP(&captcha, "captcha", "c", "", "a captcha token may be required to register, see https://gitlab.com/signald/signald/-/wikis/Captchas for how to get one")
RegisterAccountCmd.Flags().BoolVarP(&testing, "testing", "t", false, "use the Signal testing server")
}