From 90334fb82d12b29fb5bbfb646b3352211bbb2d64 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 3 Mar 2021 18:31:12 -0800 Subject: [PATCH] Add several group update commands fixes #5 --- .../cmd/group/addmember/add-member.go | 109 +++++++++++++ .../cmd/group/removemember/remove-member.go | 110 +++++++++++++ cmd/signaldctl/cmd/group/root.go | 6 + cmd/signaldctl/cmd/group/show/show-group.go | 123 ++++++++------ .../accesscontrol/update-group-access.go | 115 +++++++++++++ .../update/avatar/update-group-avatar.go | 88 ++++++++++ .../group/update/role/update-group-role.go | 154 ++++++++++++++++++ cmd/signaldctl/cmd/group/update/root.go | 38 +++++ .../group/update/timer/update-group-timer.go | 111 +++++++++++++ .../group/update/title/update-group-title.go | 105 ++++++++++++ 10 files changed, 910 insertions(+), 49 deletions(-) create mode 100644 cmd/signaldctl/cmd/group/addmember/add-member.go create mode 100644 cmd/signaldctl/cmd/group/removemember/remove-member.go create mode 100644 cmd/signaldctl/cmd/group/update/accesscontrol/update-group-access.go create mode 100644 cmd/signaldctl/cmd/group/update/avatar/update-group-avatar.go create mode 100644 cmd/signaldctl/cmd/group/update/role/update-group-role.go create mode 100644 cmd/signaldctl/cmd/group/update/root.go create mode 100644 cmd/signaldctl/cmd/group/update/timer/update-group-timer.go create mode 100644 cmd/signaldctl/cmd/group/update/title/update-group-title.go diff --git a/cmd/signaldctl/cmd/group/addmember/add-member.go b/cmd/signaldctl/cmd/group/addmember/add-member.go new file mode 100644 index 0000000..73464bc --- /dev/null +++ b/cmd/signaldctl/cmd/group/addmember/add-member.go @@ -0,0 +1,109 @@ +// 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 addmember + +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 + group string + members []*v1.JsonAddress + + AddGroupMembersCmd = &cobra.Command{ + Use: "add-member [ [...]]", + Aliases: []string{"add-members"}, + Short: "add a member to a group", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) < 2 { + common.Must(cmd.Help()) + log.Fatal("not enough arguments provided") + } + group = args[0] + for _, member := range args[1:] { + address := common.StringToAddress(member) + members = append(members, &address) + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + req := v1.UpdateGroupRequest{ + Account: account, + GroupID: group, + AddMembers: members, + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal(err, "error communicating with signald") + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + 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"}) + if resp.V2 != nil { + t.AppendRow(table.Row{resp.V2.ID, resp.V2.Title, len(resp.V2.Members)}) + } + + if resp.V1 != nil { + t.AppendRow(table.Row{resp.V1.GroupId, resp.V1.Name, len(resp.V1.Members)}) + } + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + common.StylizeTable(t) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + AddGroupMembersCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") +} diff --git a/cmd/signaldctl/cmd/group/removemember/remove-member.go b/cmd/signaldctl/cmd/group/removemember/remove-member.go new file mode 100644 index 0000000..49a2a6c --- /dev/null +++ b/cmd/signaldctl/cmd/group/removemember/remove-member.go @@ -0,0 +1,110 @@ +// 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 removemember + +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 + group string + members []*v1.JsonAddress + + RemoveMemberCmd = &cobra.Command{ + Use: "remove-member [ [...]]", + Aliases: []string{"remove-members"}, + Short: "remove a member from a group", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) < 2 { + common.Must(cmd.Help()) + log.Fatal("not enough arguments provided") + } + group = args[0] + for _, member := range args[1:] { + address := common.StringToAddress(member) + members = append(members, &address) + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + req := v1.UpdateGroupRequest{ + Account: account, + GroupID: group, + RemoveMembers: members, + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal(err, "error communicating with signald") + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + 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"}) + if resp.V2 != nil { + t.AppendRow(table.Row{resp.V2.ID, resp.V2.Title, len(resp.V2.Members)}) + } + + if resp.V1 != nil { + t.AppendRow(table.Row{resp.V1.GroupId, resp.V1.Name, len(resp.V1.Members)}) + } + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + common.StylizeTable(t) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + + }, + } +) + +func init() { + RemoveMemberCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") +} diff --git a/cmd/signaldctl/cmd/group/root.go b/cmd/signaldctl/cmd/group/root.go index adc4ce5..7e23c97 100644 --- a/cmd/signaldctl/cmd/group/root.go +++ b/cmd/signaldctl/cmd/group/root.go @@ -19,10 +19,13 @@ import ( "github.com/spf13/cobra" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/accept" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/addmember" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/create" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/leave" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/list" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/removemember" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/show" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/update" ) var GroupCmd = &cobra.Command{ @@ -32,8 +35,11 @@ var GroupCmd = &cobra.Command{ func init() { GroupCmd.AddCommand(accept.AcceptGroupInvitationCmd) + GroupCmd.AddCommand(addmember.AddGroupMembersCmd) GroupCmd.AddCommand(create.CreateGroupCmd) GroupCmd.AddCommand(leave.LeaveGroupCmd) GroupCmd.AddCommand(list.ListGroupCmd) + GroupCmd.AddCommand(removemember.RemoveMemberCmd) GroupCmd.AddCommand(show.ShowGroupCmd) + GroupCmd.AddCommand(update.UpdateGroupCmd) } diff --git a/cmd/signaldctl/cmd/group/show/show-group.go b/cmd/signaldctl/cmd/group/show/show-group.go index f7dad3d..2e1b274 100644 --- a/cmd/signaldctl/cmd/group/show/show-group.go +++ b/cmd/signaldctl/cmd/group/show/show-group.go @@ -17,10 +17,12 @@ package show import ( "encoding/json" + "fmt" "log" "os" "github.com/jedib0t/go-pretty/v6/table" + "github.com/jedib0t/go-pretty/v6/text" "github.com/spf13/cobra" "gopkg.in/yaml.v2" @@ -49,73 +51,96 @@ var ( }, Run: func(_ *cobra.Command, args []string) { go common.Signald.Listen(nil) - responses := []v1.JsonGroupV2Info{} + groups := []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) + groups = append(groups, 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") - } + common.Must(json.NewEncoder(os.Stdout).Encode(groups)) 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 { + common.Must(yaml.NewEncoder(os.Stdout).Encode(groups)) + case common.OutputFormatDefault: + for i, group := range groups { + if i > 0 { + fmt.Println() + } + leftColumnWidth := 12 + fmt.Println(text.Pad("Title", leftColumnWidth, ' '), group.Title) + fmt.Println(text.Pad("ID", leftColumnWidth, ' '), text.Pad(group.ID, 10, ' ')) + fmt.Println(text.Pad("Revision", leftColumnWidth, ' '), group.Revision) + if group.Timer > 0 { + fmt.Println(text.Pad("Timer", leftColumnWidth, ' '), text.NewUnixTimeTransformer("", nil)(group.Timer)) + } + if group.InviteLink != "" { + fmt.Println(text.Pad("Invite Link", leftColumnWidth, ' '), group.InviteLink) + } + fmt.Println("Members") + + membere164s := make(map[string]string) + for _, member := range group.Members { + if member.Number == "" { + continue + } + membere164s[member.UUID] = member.Number + } + 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) + t.AppendHeader(table.Row{"Number", "UUID", "Role"}) + for _, member := range group.MemberDetail { + t.AppendRow(table.Row{membere164s[member.UUID], member.UUID, member.Role}) } - 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) + common.StylizeTable(t) + t.Render() - if common.OutputFormat == common.OutputFormatCSV { - t.RenderCSV() - } else { + if len(group.PendingMembers) > 0 { + fmt.Println("Pending Members") + + pendinge164s := make(map[string]string) + for _, member := range group.PendingMembers { + if member.Number == "" { + continue + } + pendinge164s[member.UUID] = member.Number + } + + t := table.NewWriter() + t.AppendHeader(table.Row{"Number", "UUID", "Role"}) + for _, member := range group.PendingMemberDetail { + t.AppendRow(table.Row{pendinge164s[member.UUID], member.UUID, member.Role}) + } + t.SetOutputMirror(os.Stdout) common.StylizeTable(t) t.Render() } + + if len(group.RequestingMembers) > 0 { + fmt.Println("Requesting Members") + + t := table.NewWriter() + t.AppendHeader(table.Row{"Number", "UUID"}) + for _, member := range group.RequestingMembers { + t.AppendRow(table.Row{member.Number, member.UUID}) + } + t.SetOutputMirror(os.Stdout) + common.StylizeTable(t) + t.Render() + } + + fmt.Println("Access Control") + t = table.NewWriter() + t.AppendHeader(table.Row{"Property", "Required Permission"}) + t.AppendRows([]table.Row{{"Attributes", group.AccessControl.Attributes}, {"Link", group.AccessControl.Link}, {"Members", group.AccessControl.Members}}) + t.SetOutputMirror(os.Stdout) + common.StylizeTable(t) + t.Render() } default: log.Fatal("Unsupported output format") diff --git a/cmd/signaldctl/cmd/group/update/accesscontrol/update-group-access.go b/cmd/signaldctl/cmd/group/update/accesscontrol/update-group-access.go new file mode 100644 index 0000000..9a2c1cc --- /dev/null +++ b/cmd/signaldctl/cmd/group/update/accesscontrol/update-group-access.go @@ -0,0 +1,115 @@ +// 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 accesscontrol + +import ( + "encoding/json" + "log" + "os" + "strings" + + "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" + v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + group string + accesscontrol v1.GroupAccessControl + UpdateGroupAccessControlCmd = &cobra.Command{ + Use: "access-control ", + Aliases: []string{"access-controls"}, + Short: "change a group's access control", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) != 3 { + common.Must(cmd.Help()) + log.Fatal("not enough arguments provided") + } + group = args[0] + role := strings.ToUpper(args[2]) + if role == "ADMIN" { + role = "ADMINISTRATOR" + } + switch args[1] { + case "attributes": + accesscontrol = v1.GroupAccessControl{Attributes: role} + case "link": + accesscontrol = v1.GroupAccessControl{Link: role} + case "members": + accesscontrol = v1.GroupAccessControl{Members: role} + default: + common.Must(cmd.Help()) + log.Fatal("please specify attribute, link or member") + } + }, + Run: func(_ *cobra.Command, _ []string) { + go common.Signald.Listen(nil) + + req := v1.UpdateGroupRequest{ + Account: account, + GroupID: group, + UpdateAccessControl: &accesscontrol, + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal(err, "error communicating with signald") + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: + t := table.NewWriter() + t.AppendHeader(table.Row{"Property", "Required Permission"}) + t.AppendRows([]table.Row{{"Attributes", resp.V2.AccessControl.Attributes}, {"Link", resp.V2.AccessControl.Link}, {"Members", resp.V2.AccessControl.Members}}) + t.SetOutputMirror(os.Stdout) + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + common.StylizeTable(t) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + UpdateGroupAccessControlCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") +} diff --git a/cmd/signaldctl/cmd/group/update/avatar/update-group-avatar.go b/cmd/signaldctl/cmd/group/update/avatar/update-group-avatar.go new file mode 100644 index 0000000..513cb90 --- /dev/null +++ b/cmd/signaldctl/cmd/group/update/avatar/update-group-avatar.go @@ -0,0 +1,88 @@ +// 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 avatar + +import ( + "encoding/json" + "log" + "os" + + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/cmd/signaldctl/config" + v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + group string + avatar string + UpdateGroupAvatarCmd = &cobra.Command{ + Use: "avatar ", + Short: "change a group's avatar", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) != 2 { + common.Must(cmd.Help()) + log.Fatal("not enough arguments provided") + } + group = args[0] + avatar = args[1] + }, + Run: func(_ *cobra.Command, _ []string) { + go common.Signald.Listen(nil) + + req := v1.UpdateGroupRequest{ + Account: account, + GroupID: group, + Avatar: avatar, + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal(err, "error communicating with signald") + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatTable, common.OutputFormatDefault: + return + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + UpdateGroupAvatarCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") +} diff --git a/cmd/signaldctl/cmd/group/update/role/update-group-role.go b/cmd/signaldctl/cmd/group/update/role/update-group-role.go new file mode 100644 index 0000000..8568b0d --- /dev/null +++ b/cmd/signaldctl/cmd/group/update/role/update-group-role.go @@ -0,0 +1,154 @@ +// 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 role + +import ( + "encoding/json" + "fmt" + "log" + "os" + "strings" + + "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" + v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + group string + address v1.JsonAddress + role string + UpdateGroupRoleCmd = &cobra.Command{ + Use: "role
", + Short: "change a group member's role", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) != 3 { + common.Must(cmd.Help()) + log.Fatal("not enough arguments provided") + } + group = args[0] + address = common.StringToAddress(args[1]) + switch strings.ToLower(args[2]) { + case "default": + role = "DEFAULT" + case "admin": + role = "ADMINISTRATOR" + default: + common.Must(cmd.Help()) + log.Fatal("invalid role specified, please choose 'default' or 'admin'") + } + }, + Run: func(_ *cobra.Command, _ []string) { + go common.Signald.Listen(nil) + if address.UUID == "" { + req := v1.ResolveAddressRequest{ + Account: account, + Partial: &address, + } + var err error + address, err = req.Submit(common.Signald) + if err != nil { + log.Fatal("Failed to resolve user address: ", err) + } + if address.UUID == "" { + log.Fatal("Failed to resolve user's UUID") + } + } + + req := v1.UpdateGroupRequest{ + Account: account, + GroupID: group, + UpdateRole: &v1.GroupMember{ + Role: role, + UUID: address.UUID, + }, + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal(err, "error communicating with signald") + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: + membere164s := make(map[string]string) + for _, member := range resp.V2.Members { + if member.Number == "" { + continue + } + membere164s[member.UUID] = member.Number + } + + t := table.NewWriter() + t.AppendHeader(table.Row{"Number", "UUID", "Role"}) + for _, member := range resp.V2.MemberDetail { + t.AppendRow(table.Row{membere164s[member.UUID], member.UUID, member.Role}) + } + t.SetOutputMirror(os.Stdout) + common.StylizeTable(t) + t.Render() + + if len(resp.V2.PendingMembers) > 0 { + fmt.Println("Pending Members") + + pendinge164s := make(map[string]string) + for _, member := range resp.V2.PendingMembers { + if member.Number == "" { + continue + } + pendinge164s[member.UUID] = member.Number + } + + t := table.NewWriter() + t.AppendHeader(table.Row{"Number", "UUID", "Role"}) + for _, member := range resp.V2.PendingMemberDetail { + t.AppendRow(table.Row{pendinge164s[member.UUID], member.UUID, member.Role}) + } + t.SetOutputMirror(os.Stdout) + common.StylizeTable(t) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + UpdateGroupRoleCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") +} diff --git a/cmd/signaldctl/cmd/group/update/root.go b/cmd/signaldctl/cmd/group/update/root.go new file mode 100644 index 0000000..1db5424 --- /dev/null +++ b/cmd/signaldctl/cmd/group/update/root.go @@ -0,0 +1,38 @@ +// 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 update + +import ( + "github.com/spf13/cobra" + + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/update/accesscontrol" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/update/avatar" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/update/role" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/update/timer" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/update/title" +) + +var ( + UpdateGroupCmd = &cobra.Command{Use: "update"} +) + +func init() { + UpdateGroupCmd.AddCommand(accesscontrol.UpdateGroupAccessControlCmd) + UpdateGroupCmd.AddCommand(avatar.UpdateGroupAvatarCmd) + UpdateGroupCmd.AddCommand(role.UpdateGroupRoleCmd) + UpdateGroupCmd.AddCommand(timer.UpdateGroupTimerCmd) + UpdateGroupCmd.AddCommand(title.UpdateGroupTitleCmd) +} diff --git a/cmd/signaldctl/cmd/group/update/timer/update-group-timer.go b/cmd/signaldctl/cmd/group/update/timer/update-group-timer.go new file mode 100644 index 0000000..a7a9f17 --- /dev/null +++ b/cmd/signaldctl/cmd/group/update/timer/update-group-timer.go @@ -0,0 +1,111 @@ +// 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 timer + +import ( + "encoding/json" + "log" + "os" + "time" + + "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" + v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + group string + expiration int32 + UpdateGroupTimerCmd = &cobra.Command{ + Use: "timer