Add signaldctl get accounts

This commit is contained in:
Finn 2021-01-30 21:36:35 -08:00
parent 8d4b247698
commit a44db53f64
10 changed files with 148 additions and 147 deletions

View file

@ -0,0 +1,88 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package account
import (
"encoding/json"
"log"
"os"
"github.com/jedib0t/go-pretty/v6/table"
"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"
)
var (
GetAccountCmd = &cobra.Command{
Use: "account",
Aliases: []string{"accounts"},
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,
})
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
}
}
switch common.OutputFormat {
case "json":
err := json.NewEncoder(os.Stdout).Encode(accounts)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
break
case "yaml":
err := yaml.NewEncoder(os.Stdout).Encode(accounts)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
break
case "table":
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Phone Number", "UUID", "Device ID", "Subscribed"})
for _, account := range accounts {
t.AppendRow(table.Row{account.Username, account.UUID, account.DeviceID, account.Subscribed})
}
t.Render()
break
default:
log.Fatal("Unsupported output format")
}
},
}
)

View file

@ -13,7 +13,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package get
package group
import (
"encoding/json"
@ -31,7 +31,7 @@ import (
var (
account string
groupID string
getGroupCmd = &cobra.Command{
GetGroupCmd = &cobra.Command{
Use: "group",
Aliases: []string{"groups"},
Short: "return a list of Signal groups",
@ -102,9 +102,7 @@ var (
)
func init() {
GetCmd.AddCommand(getGroupCmd)
getGroupCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use")
getGroupCmd.Flags().StringVarP(&groupID, "group-id", "g", "", "if set, fetches the latest state of a group from the server (local state is used for legacy groups). If unset, return all groups")
common.Must(getGroupCmd.MarkFlagRequired("account"))
GetGroupCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use")
GetGroupCmd.Flags().StringVarP(&groupID, "group-id", "g", "", "if set, fetches the latest state of a group from the server (local state is used for legacy groups). If unset, return all groups")
common.Must(GetGroupCmd.MarkFlagRequired("account"))
}

View file

@ -17,6 +17,14 @@ package get
import (
"github.com/spf13/cobra"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/get/account"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/get/group"
)
var GetCmd = &cobra.Command{Use: "get"}
func init() {
GetCmd.AddCommand(group.GetGroupCmd)
GetCmd.AddCommand(account.GetAccountCmd)
}

View file

@ -1,60 +0,0 @@
// Copyright © 2018 Finn Herzfeld <finn@janky.solutions>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"log"
"math/rand"
"github.com/spf13/cobra"
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
)
// listAccountsCmd represents the listAccounts command
var listAccountsCmd = &cobra.Command{
Use: "list-accounts",
Short: "list of all the accounts registered to this signald instance.",
Long: `Prints a list of all users to stdout.`,
Run: func(cmd *cobra.Command, args []string) {
requestID := fmt.Sprint("signaldctl-", rand.Intn(1000))
err := common.Signald.RawRequest(v0.LegacyRequest{
Type: "list_accounts",
ID: requestID,
})
if err != nil {
log.Fatal("error sending request: ", err)
}
c := make(chan v0.LegacyResponse)
go common.Signald.Listen(c)
for {
message := <-c
if message.ID == requestID {
for _, account := range message.Data.Accounts {
fmt.Println(account.Username)
}
break
}
}
},
}
func init() {
RootCmd.AddCommand(listAccountsCmd)
}

View file

@ -38,6 +38,7 @@ type Account struct {
Registered bool `json:",omitempty"`
HasKeys bool `json:"has_keys,omitempty"`
Subscribed bool
UUID string `json:",omitempty"`
}
// DataMessage is the main component of incoming text messages

View file

@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"gitlab.com/signald/signald-go/signald"
)
@ -16,7 +15,7 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr
r.Version = "v1"
r.Type = "accept_invitation"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -49,7 +48,7 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG
r.Version = "v1"
r.Type = "approve_membership"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -82,7 +81,7 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf
r.Version = "v1"
r.Type = "get_group"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -115,7 +114,7 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked
r.Version = "v1"
r.Type = "get_linked_devices"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -148,7 +147,7 @@ func (r *GetProfileRequest) Submit(conn *signald.Signald) (response Profile, err
r.Version = "v1"
r.Type = "get_profile"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -181,7 +180,7 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin
r.Version = "v1"
r.Type = "join_group"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -213,7 +212,7 @@ func (r *ListContactsRequest) Submit(conn *signald.Signald) (response ProfileLis
r.Version = "v1"
r.Type = "list_contacts"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -245,7 +244,7 @@ func (r *ListGroupsRequest) Submit(conn *signald.Signald) (response GroupList, e
r.Version = "v1"
r.Type = "list_groups"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -277,7 +276,7 @@ func (r *MarkReadRequest) Submit(conn *signald.Signald) error {
r.Version = "v1"
r.Type = "mark_read"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
return conn.RawRequest(r)
@ -288,7 +287,7 @@ func (r *ProtocolRequest) Submit(conn *signald.Signald) error {
r.Version = "v1"
r.Type = "protocol"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
return conn.RawRequest(r)
@ -300,7 +299,7 @@ func (r *ReactRequest) Submit(conn *signald.Signald) (response SendResponse, err
r.Version = "v1"
r.Type = "react"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -333,7 +332,7 @@ func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) error {
r.Version = "v1"
r.Type = "remove_linked_device"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
return conn.RawRequest(r)
@ -345,7 +344,7 @@ func (r *ResolveAddressRequest) Submit(conn *signald.Signald) (response JsonAddr
r.Version = "v1"
r.Type = "resolve_address"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -377,7 +376,7 @@ func (r *SendRequest) Submit(conn *signald.Signald) (response SendResponse, err
r.Version = "v1"
r.Type = "send"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -409,7 +408,7 @@ func (r *SetProfile) Submit(conn *signald.Signald) error {
r.Version = "v1"
r.Type = "set_profile"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
return conn.RawRequest(r)
@ -421,7 +420,7 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo,
r.Version = "v1"
r.Type = "update_group"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -453,7 +452,7 @@ func (r *VersionRequest) Submit(conn *signald.Signald) (response JsonVersionMess
r.Version = "v1"
r.Type = "version"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -480,15 +479,3 @@ func (r *VersionRequest) Submit(conn *signald.Signald) (response JsonVersionMess
return response, nil
}
const idsize = 10
var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func generateID() string {
id := make([]rune, idsize)
for i := range id {
id[i] = charset[rand.Intn(len(charset))]
}
return string(id)
}

View file

@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"gitlab.com/signald/signald-go/signald"
)
@ -16,7 +15,7 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr
r.Version = "v1alpha1"
r.Type = "accept_invitation"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -49,7 +48,7 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG
r.Version = "v1alpha1"
r.Type = "approve_membership"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -82,7 +81,7 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf
r.Version = "v1alpha1"
r.Type = "get_group"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -115,7 +114,7 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked
r.Version = "v1alpha1"
r.Type = "get_linked_devices"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -148,7 +147,7 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin
r.Version = "v1alpha1"
r.Type = "join_group"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -180,7 +179,7 @@ func (r *ProtocolRequest) Submit(conn *signald.Signald) error {
r.Version = "v1alpha1"
r.Type = "protocol"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
return conn.RawRequest(r)
@ -192,7 +191,7 @@ func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) error {
r.Version = "v1alpha1"
r.Type = "remove_linked_device"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
return conn.RawRequest(r)
@ -204,7 +203,7 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2
r.Version = "v1alpha1"
r.Type = "update_group"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -231,15 +230,3 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2
return response, nil
}
const idsize = 10
var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func generateID() string {
id := make([]rune, idsize)
for i := range id {
id[i] = charset[rand.Intn(len(charset))]
}
return string(id)
}

View file

@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"gitlab.com/signald/signald-go/signald"
)
@ -16,7 +15,7 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo,
r.Version = "v1alpha2"
r.Type = "update_group"
if r.ID == "" {
r.ID = generateID()
r.ID = signald.GenerateID()
}
err = conn.RawRequest(r)
@ -43,15 +42,3 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo,
return response, nil
}
const idsize = 10
var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func generateID() string {
id := make([]rune, idsize)
for i := range id {
id[i] = charset[rand.Intn(len(charset))]
}
return string(id)
}

18
signald/utils.go Normal file
View file

@ -0,0 +1,18 @@
package signald
import (
"math/rand"
)
const idsize = 10
var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
// GenerateID is a helper function to generate random request IDs.
func GenerateID() string {
id := make([]rune, idsize)
for i := range id {
id[i] = charset[rand.Intn(len(charset))]
}
return string(id)
}

View file

@ -6,7 +6,6 @@ import ({{if .Responses}}
"encoding/json"
"fmt"
"log"{{end}}
"math/rand"
"gitlab.com/signald/signald-go/signald"
)
@ -22,7 +21,7 @@ func {{$action.FnName}}(conn *signald.Signald) ({{if ne $action.Response ""}}res
r := Request{Version: "{{.Version}}"}
{{end}} r.Type = "{{$type}}"
if(r.ID == "") {
r.ID = generateID()
r.ID = signald.GenerateID()
}
{{if ne $action.Response ""}}
@ -53,15 +52,3 @@ func {{$action.FnName}}(conn *signald.Signald) ({{if ne $action.Response ""}}res
{{end}}
}
{{end}}
const idsize = 10
var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func generateID() string {
id := make([]rune, idsize)
for i := range id {
id[i]= charset[rand.Intn(len(charset))]
}
return string(id)
}