Add signaldctl group accept

This commit is contained in:
Finn 2021-02-02 02:37:56 -08:00
parent 4d82a2093d
commit fb3261f811
2 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,95 @@
// 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 accept
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
AcceptGroupInvitationCmd = &cobra.Command{
Use: "accept",
Short: "accept an invitation to join 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.AcceptInvitationRequest{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:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "Title", "Members"})
for _, group := range responses {
t.AppendRow(table.Row{group.ID, group.Title, len(group.Members)})
}
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
t.SetStyle(table.StyleLight)
t.Render()
}
default:
log.Fatal("Unsupported output format")
}
},
}
)
func init() {
AcceptGroupInvitationCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use")
}

View file

@ -18,6 +18,7 @@ package group
import (
"github.com/spf13/cobra"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/accept"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/list"
"gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/show"
)
@ -28,6 +29,7 @@ var GroupCmd = &cobra.Command{
}
func init() {
GroupCmd.AddCommand(accept.AcceptGroupInvitationCmd)
GroupCmd.AddCommand(list.ListGroupCmd)
GroupCmd.AddCommand(show.ShowGroupCmd)
}