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
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)
}
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
log.Fatal(err, "error communicating with signald")
}
err = json.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
},
}

View file

@ -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