From 5a24d20d26a43fd44d9533dc900e587f7e51d6be Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 30 Jan 2021 14:32:43 -0800 Subject: [PATCH] Update list-groups to use v1 request --- cmd/signaldctl/cmd/list-groups.go | 31 ++++++++++--------------------- signald/signald.go | 6 ++++++ 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/cmd/signaldctl/cmd/list-groups.go b/cmd/signaldctl/cmd/list-groups.go index 6c53bb9..35a99a5 100644 --- a/cmd/signaldctl/cmd/list-groups.go +++ b/cmd/signaldctl/cmd/list-groups.go @@ -16,13 +16,13 @@ package cmd import ( - "fmt" + "encoding/json" "log" - "math/rand" + "os" "github.com/spf13/cobra" - "gitlab.com/signald/signald-go/signald/client-protocol/v0" + "gitlab.com/signald/signald-go/signald/client-protocol/v1" ) // listGroupsCmd represents the listGroups command @@ -31,26 +31,15 @@ var listGroupsCmd = &cobra.Command{ Short: "list of all the groups that the user is in.", Long: `Prints a list of all groups the user is in to stdout.`, Run: func(cmd *cobra.Command, args []string) { - requestID := fmt.Sprint("signaldctl-", rand.Intn(1000)) - err := s.RawRequest(v0.LegacyRequest{ - Type: "list_groups", - Username: username, - ID: requestID, - }) + go s.Listen(nil) + req := v1.ListGroupsRequest{Account: username} + resp, err := req.Submit(s) if err != nil { - log.Fatal("error sending request: ", err) + log.Fatal(err, "error communicating with signald") } - - c := make(chan v0.LegacyResponse) - go s.Listen(c) - for { - message := <-c - if message.ID == requestID { - for _, group := range message.Data.Groups { - fmt.Println(group.Name) - } - break - } + err = json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") } }, } diff --git a/signald/signald.go b/signald/signald.go index 4e1fa5d..ba19a48 100644 --- a/signald/signald.go +++ b/signald/signald.go @@ -21,10 +21,12 @@ import ( "fmt" "io" "log" + "math/rand" "net" "os" "strconv" "strings" + "time" "gitlab.com/signald/signald-go/signald/client-protocol/v0" ) @@ -33,6 +35,10 @@ var ( debugSignaldIO, _ = strconv.ParseBool(os.Getenv("DEBUG_SIGNALD_IO")) ) +func init() { + rand.Seed(time.Now().UTC().UnixNano()) +} + // Signald is a connection to a signald instance. type Signald struct { socket net.Conn