This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
cli.go
175 lines (152 loc) · 4.65 KB
/
cli.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package user
import (
"context"
"fmt"
otfapi "github.com/leg100/otf/internal/api"
"github.com/leg100/otf/internal/team"
"github.com/spf13/cobra"
)
type userCLI struct {
client userCLIClient
}
type userCLIClient interface {
Create(ctx context.Context, username string, opts ...NewUserOption) (*User, error)
Delete(ctx context.Context, username string) error
}
func NewUserCommand(apiClient *otfapi.Client) *cobra.Command {
cli := &userCLI{}
cmd := &cobra.Command{
Use: "users",
Short: "User account management",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Parent().PersistentPreRunE(cmd.Parent(), args); err != nil {
return err
}
cli.client = &client{Client: apiClient}
return nil
},
}
cmd.AddCommand(cli.userNewCommand())
cmd.AddCommand(cli.userDeleteCommand())
return cmd
}
func (a *userCLI) userNewCommand() *cobra.Command {
return &cobra.Command{
Use: "new [username]",
Short: "Create a new user account",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
user, err := a.client.Create(cmd.Context(), args[0])
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Successfully created user %s\n", user.Username)
return nil
},
}
}
func (a *userCLI) userDeleteCommand() *cobra.Command {
return &cobra.Command{
Use: "delete [username]",
Short: "Delete a user account",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := a.client.Delete(cmd.Context(), args[0]); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Successfully deleted user %s\n", args[0])
return nil
},
}
}
type membershipCLI struct {
client membershipCLIClient
teams teamsCLIClient
}
type membershipCLIClient interface {
AddTeamMembership(ctx context.Context, teamID string, usernames []string) error
RemoveTeamMembership(ctx context.Context, teamID string, usernames []string) error
}
type teamsCLIClient interface {
Get(ctx context.Context, organization, name string) (*team.Team, error)
}
func NewTeamMembershipCommand(apiclient *otfapi.Client) *cobra.Command {
cli := &membershipCLI{}
cmd := &cobra.Command{
Use: "team-membership",
Short: "Team membership management",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Parent().PersistentPreRunE(cmd.Parent(), args); err != nil {
return err
}
cli.client = &client{Client: apiclient}
cli.teams = &team.Client{Client: apiclient}
return nil
},
}
cmd.AddCommand(cli.addTeamMembershipCommand())
cmd.AddCommand(cli.deleteTeamMembershipCommand())
return cmd
}
func (a *membershipCLI) addTeamMembershipCommand() *cobra.Command {
var (
organization string
name string // team name
)
cmd := &cobra.Command{
Use: "add-users [username|...]",
Short: "Add users to team",
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
team, err := a.teams.Get(cmd.Context(), organization, name)
if err != nil {
return err
}
if err := a.client.AddTeamMembership(cmd.Context(), team.ID, args); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Successfully added %s to %s\n", args, name)
return nil
},
}
cmd.Flags().StringVar(&organization, "organization", "", "OTF organization in which to perform action")
cmd.MarkFlagRequired("organization")
cmd.Flags().StringVar(&name, "team", "", "Team in which to perform action")
cmd.MarkFlagRequired("team")
return cmd
}
func (a *membershipCLI) deleteTeamMembershipCommand() *cobra.Command {
var (
organization string
name string // team name
)
cmd := &cobra.Command{
Use: "del-users [username|...]",
Short: "Delete users from team",
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
team, err := a.teams.Get(cmd.Context(), organization, name)
if err != nil {
return err
}
if err := a.client.RemoveTeamMembership(cmd.Context(), team.ID, args); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Successfully removed %s from %s\n", args, name)
return nil
},
}
cmd.Flags().StringVar(&organization, "organization", "", "OTF organization in which to perform action")
cmd.MarkFlagRequired("organization")
cmd.Flags().StringVar(&name, "team", "", "Team in which to perform action")
cmd.MarkFlagRequired("team")
return cmd
}