From 2b94a65ca09cf8b9bb936bfb7f1cc3f50fe35023 Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 2 Feb 2021 01:18:37 -0800 Subject: [PATCH] get a specific group --- .../cmd/account/link/link-account.go | 1 + .../cmd/account/list/list-accounts.go | 1 + cmd/signaldctl/cmd/group/list/list-group.go | 2 +- cmd/signaldctl/cmd/group/root.go | 2 + cmd/signaldctl/cmd/group/show/show-group.go | 127 ++++++++++++++++++ .../cmd/message/send/send-message.go | 1 + cmd/signaldctl/cmd/version.go | 3 +- cmd/signaldctl/config/config.go | 4 +- 8 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 cmd/signaldctl/cmd/group/show/show-group.go diff --git a/cmd/signaldctl/cmd/account/link/link-account.go b/cmd/signaldctl/cmd/account/link/link-account.go index a819030..992fd88 100644 --- a/cmd/signaldctl/cmd/account/link/link-account.go +++ b/cmd/signaldctl/cmd/account/link/link-account.go @@ -68,6 +68,7 @@ var ( if common.OutputFormat == common.OutputFormatCSV { t.RenderCSV() } else { + t.SetStyle(table.StyleLight) t.Render() } case common.OutputFormatQR, common.OutputFormatDefault: diff --git a/cmd/signaldctl/cmd/account/list/list-accounts.go b/cmd/signaldctl/cmd/account/list/list-accounts.go index 01d86c7..6ec1112 100644 --- a/cmd/signaldctl/cmd/account/list/list-accounts.go +++ b/cmd/signaldctl/cmd/account/list/list-accounts.go @@ -78,6 +78,7 @@ var ( if common.OutputFormat == common.OutputFormatCSV { t.RenderCSV() } else { + t.SetStyle(table.StyleLight) t.Render() } default: diff --git a/cmd/signaldctl/cmd/group/list/list-group.go b/cmd/signaldctl/cmd/group/list/list-group.go index 695eaec..9f406e9 100644 --- a/cmd/signaldctl/cmd/group/list/list-group.go +++ b/cmd/signaldctl/cmd/group/list/list-group.go @@ -77,6 +77,7 @@ var ( if common.OutputFormat == common.OutputFormatCSV { t.RenderCSV() } else { + t.SetStyle(table.StyleLight) t.Render() } default: @@ -88,5 +89,4 @@ var ( func init() { ListGroupCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") - common.Must(ListGroupCmd.MarkFlagRequired("account")) } diff --git a/cmd/signaldctl/cmd/group/root.go b/cmd/signaldctl/cmd/group/root.go index b1c760c..bba3ff5 100644 --- a/cmd/signaldctl/cmd/group/root.go +++ b/cmd/signaldctl/cmd/group/root.go @@ -19,6 +19,7 @@ import ( "github.com/spf13/cobra" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/list" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/show" ) var GroupCmd = &cobra.Command{ @@ -28,4 +29,5 @@ var GroupCmd = &cobra.Command{ func init() { GroupCmd.AddCommand(list.ListGroupCmd) + GroupCmd.AddCommand(show.ShowGroupCmd) } diff --git a/cmd/signaldctl/cmd/group/show/show-group.go b/cmd/signaldctl/cmd/group/show/show-group.go new file mode 100644 index 0000000..125742a --- /dev/null +++ b/cmd/signaldctl/cmd/group/show/show-group.go @@ -0,0 +1,127 @@ +// Copyright © 2021 Finn Herzfeld +// +// 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 . + +package show + +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/cmd/signaldctl/config" + "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + ShowGroupCmd = &cobra.Command{ + Use: "show", + Short: "show details about a group", + PreRun: func(_ *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) == 0 { + log.Fatal("at least one group ID required") + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + responses := []v1.JsonGroupV2Info{} + for _, group := range args { + req := v1.GetGroupRequest{Account: account, GroupID: group} + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal(err, "error communicating with signald") + } + responses = append(responses, resp) + } + + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(responses) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(responses) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: + rowConfigAutoMerge := table.RowConfig{AutoMerge: true} + for _, resp := range responses { + t := table.NewWriter() + t.AppendHeader(table.Row{"Title", resp.Title}, rowConfigAutoMerge) + t.AppendRow(table.Row{"ID", resp.ID}, rowConfigAutoMerge) + t.AppendRow(table.Row{"Revision", resp.Revision}, rowConfigAutoMerge) + if resp.Timer > 0 { + t.AppendRow(table.Row{"Timer", resp.Timer}, rowConfigAutoMerge) + } + if resp.InviteLink != "" { + t.AppendRow(table.Row{"Invite Link", resp.InviteLink}, rowConfigAutoMerge) + } + + for i, member := range resp.Members { + first := "" + if i == 0 { + first = "Members" + } + t.AppendRow(table.Row{first, member.UUID}, rowConfigAutoMerge) + } + + for i, member := range resp.PendingMembers { + first := "" + if i == 0 { + first = "Pending Members" + } + t.AppendRow(table.Row{first, member.Number}, rowConfigAutoMerge) + } + + for i, member := range resp.RequestingMembers { + first := "" + if i == 0 { + first = "Requesting Members" + } + t.AppendRow(table.Row{first, member.Number}, rowConfigAutoMerge) + } + + t.SetOutputMirror(os.Stdout) + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + t.SetStyle(table.StyleLight) + t.Render() + } + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + ShowGroupCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") +} diff --git a/cmd/signaldctl/cmd/message/send/send-message.go b/cmd/signaldctl/cmd/message/send/send-message.go index 32c36e5..de8b6b8 100644 --- a/cmd/signaldctl/cmd/message/send/send-message.go +++ b/cmd/signaldctl/cmd/message/send/send-message.go @@ -112,6 +112,7 @@ var ( if common.OutputFormat == common.OutputFormatCSV { t.RenderCSV() } else { + t.SetStyle(table.StyleLight) t.Render() } default: diff --git a/cmd/signaldctl/cmd/version.go b/cmd/signaldctl/cmd/version.go index c3dd7c0..f29608d 100644 --- a/cmd/signaldctl/cmd/version.go +++ b/cmd/signaldctl/cmd/version.go @@ -31,8 +31,7 @@ import ( // versionCmd represents the version command var versionCmd = &cobra.Command{ Use: "version", - Short: "print the signald version", - Long: `print the signald version`, + Short: "print the current signald and signaldctl version", Run: func(cmd *cobra.Command, args []string) { go common.Signald.Listen(nil) r := v1.VersionRequest{} diff --git a/cmd/signaldctl/config/config.go b/cmd/signaldctl/config/config.go index b4866f7..494d0e9 100644 --- a/cmd/signaldctl/config/config.go +++ b/cmd/signaldctl/config/config.go @@ -25,7 +25,9 @@ import ( type Configuration struct { // DefaultAccount will be used if --account is not provided DefaultAccount string - SocketPath string + + // SocketPath is the path to signald.sock + SocketPath string } var Path string