-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd_group.go
266 lines (247 loc) · 6.97 KB
/
cmd_group.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"errors"
"fmt"
"strings"
"github.com/urfave/cli"
"golang.org/x/net/context"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)
func cmdGroupList(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory Client %v", err)
}
call := srv.Groups.List().
Customer(myAccoutsCustomerID).
MaxResults(int64(ifZero(c.Int("limit"), 100))).
OrderBy("email")
if domain := c.GlobalString("domain"); len(domain) > 0 {
call = call.Domain(domain)
}
r, err := call.Do()
if err != nil {
return fmt.Errorf("unable to retrieve groups in domain: %v", err)
}
if optionJSON(c, r.Groups) {
return nil
}
for _, g := range r.Groups {
// email is default
fmt.Println(g.Email)
}
return nil
}
func cmdGroupMembers(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory Client %v", err)
}
groupKey := c.Args().Get(0)
if len(groupKey) == 0 {
return fmt.Errorf("missing group email in command")
}
if strings.Index(groupKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
groupKey = fmt.Sprintf("%s@%s", groupKey, domain)
}
r, err := srv.Members.List(groupKey).Do()
if err != nil {
return fmt.Errorf("unable to retrieve members of group [%s] : %v", groupKey, err)
}
if optionJSON(c, r.Members) {
return nil
}
for _, g := range r.Members {
// email is default
fmt.Println(g.Email)
}
return nil
}
func cmdGroupInfo(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %w", err)
}
groupKey := c.Args().Get(0)
if len(groupKey) == 0 {
return fmt.Errorf("missing group email in command")
}
if strings.Index(groupKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
groupKey = fmt.Sprintf("%s@%s", groupKey, domain)
}
r, err := srv.Groups.Get(groupKey).Do()
if err != nil {
return fmt.Errorf("unable to retrieve group [%s] because: %v (%T)", groupKey, err, err)
}
if optionJSON(c, r) {
return nil
}
fmt.Printf("%s (%s) [members=%d]\n", r.Email, r.Name, r.DirectMembersCount)
return nil
}
func cmdGroupDelete(c *cli.Context) error {
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(sharedAuthClient(c)))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %w (%T)", err, err)
}
// group argument
groupKey := c.Args().Get(0)
if len(groupKey) == 0 {
return fmt.Errorf("missing group email in command")
}
if strings.Index(groupKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
groupKey = fmt.Sprintf("%s@%s", groupKey, domain)
}
// prompt
if !promptForYes(c, fmt.Sprintf("Are you sure to delete group [%s] (y/N)? ", groupKey)) {
return errors.New("group delete aborted")
}
// doit
err = srv.Groups.Delete(groupKey).Do()
if err != nil {
return fmt.Errorf("unable to delete group [%s] because: %w (%T)", groupKey, err, err)
}
fmt.Printf("deleted group [%s]\n", groupKey)
return nil
}
func cmdGroupAddMembers(c *cli.Context) error {
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(sharedAuthClient(c)))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %w (%T)", err, err)
}
// group argument
groupKey := c.Args().Get(0)
if len(groupKey) == 0 {
return fmt.Errorf("missing group email in command")
}
if strings.Index(groupKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
groupKey = fmt.Sprintf("%s@%s", groupKey, domain)
}
// users argument
userArgument := c.Args().Get(1)
if len(userArgument) == 0 {
return fmt.Errorf("missing (space separated) user(s) email in command")
}
userKeys := []string{}
for _, each := range c.Args()[1:] {
userKey := each
if strings.Index(each, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
userKey = fmt.Sprintf("%s@%s", each, domain)
}
userKeys = append(userKeys, userKey)
}
// prompt
if !promptForYes(c, fmt.Sprintf("Are you sure to add member(s) %v to group [%s] (y/N)? ", userKeys, groupKey)) {
return errors.New("group add aborted")
}
for _, each := range userKeys {
// doit
member := &admin.Member{
Email: each,
}
_, err = srv.Members.Insert(groupKey, member).Do()
if err != nil {
// TODO: show different log when *googleapi.Error=&{409 Member already exists.
fmt.Printf("unable to add member [%s] to group [%s] because: %v (%T)\n", each, groupKey, err, err)
continue
}
fmt.Printf("added member [%s] to group [%s]\n", each, groupKey)
}
return nil
}
func cmdGroupRemoveMember(c *cli.Context) error {
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(sharedAuthClient(c)))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %w (%T)", err, err)
}
// group argument
groupKey := c.Args().Get(0)
if len(groupKey) == 0 {
return fmt.Errorf("missing group email in command")
}
if strings.Index(groupKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
groupKey = fmt.Sprintf("%s@%s", groupKey, domain)
}
// member argument
userKey := c.Args().Get(1)
if len(userKey) == 0 {
return fmt.Errorf("missing user email in command")
}
if strings.Index(userKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
userKey = fmt.Sprintf("%s@%s", userKey, domain)
}
// prompt
if !promptForYes(c, fmt.Sprintf("Are you sure to remove member [%s] from group [%s] (y/N)? ", userKey, groupKey)) {
return errors.New("group remove aborted")
}
// doit
err = srv.Members.Delete(groupKey, userKey).Do()
if err != nil {
return fmt.Errorf("unable to remove member [%s] from group [%s] because: %w (%T)", userKey, groupKey, err, err)
}
fmt.Printf("removed member [%s] from group [%s]\n", userKey, groupKey)
return nil
}
func cmdGroupCreate(c *cli.Context) error {
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(sharedAuthClient(c)))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %w (%T)", err, err)
}
// group argument
groupKey := c.Args().Get(0)
if len(groupKey) == 0 {
return fmt.Errorf("missing group email in command")
}
if strings.Index(groupKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
groupKey = fmt.Sprintf("%s@%s", groupKey, domain)
}
// doit
grp := &admin.Group{
Email: groupKey,
}
r, err := srv.Groups.Insert(grp).Do()
if err != nil {
return fmt.Errorf("unable to create group [%s] because: %w (%T)", groupKey, err, err)
}
if optionJSON(c, r) {
return nil
}
fmt.Printf("created group [%s]\n", groupKey)
return nil
}