From 4e1603db2e4cfa72e6e2625e7ca34e2c76045e0e Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 5 May 2021 19:51:53 -0700 Subject: [PATCH] signaldctl: Use v1 request type for register so errors (especially captcha errors) are shown to the user Probably shouldn't have closed #10 before this --- .../cmd/account/register/register-account.go | 50 +++++++++++-------- go.sum | 1 + 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/cmd/signaldctl/cmd/account/register/register-account.go b/cmd/signaldctl/cmd/account/register/register-account.go index b2c92c0..95ca2a9 100644 --- a/cmd/signaldctl/cmd/account/register/register-account.go +++ b/cmd/signaldctl/cmd/account/register/register-account.go @@ -16,13 +16,16 @@ package register import ( + "encoding/json" + "fmt" "log" + "os" "github.com/spf13/cobra" + "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 ( @@ -39,27 +42,32 @@ var ( } }, Run: func(_ *cobra.Command, args []string) { - requestID := signald.GenerateID() - err := common.Signald.RawRequest(v0.LegacyRequest{ - Type: "register", - ID: requestID, - Username: args[0], - Voice: voice, - Captcha: captcha, - }) - if err != nil { - log.Fatal("error sending request to signald: ", err) + go common.Signald.Listen(nil) + req := v1.RegisterRequest{ + Account: args[0], + Voice: voice, + Captcha: captcha, } - c := make(chan v0.LegacyResponse) - go common.Signald.Listen(c) - response := signald.GetLegacyResponse(c, requestID) - switch response.Type { - case "verification_required": - log.Println("verification code requested. submit with: signaldctl account verify", args[0], "") - case "captcha_required": - log.Println("captcha required. Please provide a captcha token with the --captcha argument (see https://gitlab.com/signald/signald/-/wikis/Captchas)") + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal("error: ", err) + } + + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatDefault: + fmt.Println("submit verification code with signaldctl account verify ", resp.AccountId, "000000") default: - log.Fatalf("unexpected response from signald when requesting verification code: %+v", response) + log.Fatal("Unsupported output format") } }, } diff --git a/go.sum b/go.sum index 22b84e4..b5b15ab 100644 --- a/go.sum +++ b/go.sum @@ -76,6 +76,7 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=