-
Notifications
You must be signed in to change notification settings - Fork 4
/
update_member.go
56 lines (42 loc) · 1.32 KB
/
update_member.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package members
import (
"fmt"
"github.com/spf13/cobra"
"github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
"github.com/calyptia/cli/config"
)
func NewCmdUpdateMember(config *config.Config) *cobra.Command {
completer := &completer.Completer{Config: config}
var permissions []string
cmd := &cobra.Command{
Use: "member MEMBER-ID",
Short: "Update a member permissions given its membership ID",
Args: cobra.ExactArgs(1),
ValidArgsFunction: completer.CompleteMembers,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
memberID := args[0]
in := types.UpdateMember{
MemberID: memberID,
}
if cmd.Flags().Changed("permissions") {
in.Permissions = &permissions
}
// If the user passed "all" as the only permission,
// we pass an empty slice to the API to grant all permissions.
if len(permissions) == 1 && permissions[0] == "all" {
in.Permissions = &[]string{}
}
err := config.Cloud.UpdateMember(ctx, in)
if err != nil {
return fmt.Errorf("update member: %w", err)
}
return nil
},
}
fs := cmd.Flags()
fs.StringSliceVar(&permissions, "permissions", nil, "Permissions to grant to the member")
_ = cmd.RegisterFlagCompletionFunc("permissions", completer.CompletePermissions)
return cmd
}