Update list-groups to use v1 request

This commit is contained in:
Finn 2021-01-30 14:32:43 -08:00
parent 65270b95e6
commit 5a24d20d26
2 changed files with 16 additions and 21 deletions

View file

@ -16,13 +16,13 @@
package cmd package cmd
import ( import (
"fmt" "encoding/json"
"log" "log"
"math/rand" "os"
"github.com/spf13/cobra" "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 // listGroupsCmd represents the listGroups command
@ -31,26 +31,15 @@ var listGroupsCmd = &cobra.Command{
Short: "list of all the groups that the user is in.", Short: "list of all the groups that the user is in.",
Long: `Prints a list of all groups the user is in to stdout.`, Long: `Prints a list of all groups the user is in to stdout.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
requestID := fmt.Sprint("signaldctl-", rand.Intn(1000)) go s.Listen(nil)
err := s.RawRequest(v0.LegacyRequest{ req := v1.ListGroupsRequest{Account: username}
Type: "list_groups", resp, err := req.Submit(s)
Username: username,
ID: requestID,
})
if err != nil { if err != nil {
log.Fatal("error sending request: ", err) log.Fatal(err, "error communicating with signald")
} }
err = json.NewEncoder(os.Stdout).Encode(resp)
c := make(chan v0.LegacyResponse) if err != nil {
go s.Listen(c) log.Fatal(err, "error encoding response to stdout")
for {
message := <-c
if message.ID == requestID {
for _, group := range message.Data.Groups {
fmt.Println(group.Name)
}
break
}
} }
}, },
} }

View file

@ -21,10 +21,12 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"math/rand"
"net" "net"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time"
"gitlab.com/signald/signald-go/signald/client-protocol/v0" "gitlab.com/signald/signald-go/signald/client-protocol/v0"
) )
@ -33,6 +35,10 @@ var (
debugSignaldIO, _ = strconv.ParseBool(os.Getenv("DEBUG_SIGNALD_IO")) debugSignaldIO, _ = strconv.ParseBool(os.Getenv("DEBUG_SIGNALD_IO"))
) )
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
// Signald is a connection to a signald instance. // Signald is a connection to a signald instance.
type Signald struct { type Signald struct {
socket net.Conn socket net.Conn