Add several group update commands

fixes #5
This commit is contained in:
Finn 2021-03-03 18:31:12 -08:00
parent 83cf9efc94
commit 90334fb82d
10 changed files with 910 additions and 49 deletions

View file

@ -0,0 +1,109 @@
// 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 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 <group id> [<new member number or UUID> [<new member number or UUID>...]]",
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")
}

View file

@ -0,0 +1,110 @@
// 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 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 <group id> [<member number or UUID> [<member number or UUID>...]]",
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")
}

View file

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

View file

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

View file

@ -0,0 +1,115 @@
// 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 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 <group> <attributes|link|members> <any|admin|default|member|unknown|unsatisfiable>",
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")
}

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 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 <group> <avatar path>",
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")
}

View file

@ -0,0 +1,154 @@
// 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 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 <group> <address> <default|admin>",
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")
}

View file

@ -0,0 +1,38 @@
// 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 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)
}

View file

@ -0,0 +1,111 @@
// 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 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 <group> <time>",
Short: "change a group's timer",
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]
d, err := time.ParseDuration(args[1])
if err != nil {
log.Fatal("unparsable time: see https://golang.org/pkg/time/#ParseDuration for format details")
}
expiration = int32(d.Seconds())
},
Run: func(_ *cobra.Command, _ []string) {
go common.Signald.Listen(nil)
req := v1.UpdateGroupRequest{
Account: account,
GroupID: group,
UpdateTimer: expiration,
}
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() {
UpdateGroupTimerCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use")
}

View file

@ -0,0 +1,105 @@
// 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 title
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"
v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
)
var (
account string
group string
title string
UpdateGroupTitleCmd = &cobra.Command{
Use: "title <group> <new title>",
Short: "change a group's title",
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]
title = args[1]
},
Run: func(_ *cobra.Command, _ []string) {
go common.Signald.Listen(nil)
req := v1.UpdateGroupRequest{
Account: account,
GroupID: group,
Title: title,
}
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() {
UpdateGroupTitleCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use")
}