-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_teams.go
196 lines (171 loc) · 4.36 KB
/
command_teams.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
package main
import (
"fmt"
"strconv"
"github.com/1and1/soma/internal/adm"
"github.com/1and1/soma/internal/cmpl"
"github.com/1and1/soma/lib/proto"
"github.com/codegangsta/cli"
)
func registerTeams(app cli.App) *cli.App {
app.Commands = append(app.Commands,
[]cli.Command{
// teams
{
Name: "teams",
Usage: "SUBCOMMANDS for teams",
Subcommands: []cli.Command{
{
Name: "add",
Usage: "Register a new team",
Action: runtime(cmdTeamAdd),
BashComplete: cmpl.TeamCreate,
},
{
Name: "remove",
Usage: "Delete an existing team",
Action: runtime(cmdTeamDel),
},
{
Name: "rename",
Usage: "Rename an existing team",
Action: runtime(cmdTeamRename),
BashComplete: cmpl.To,
},
{
Name: "migrate",
Usage: "Migrate users between teams",
Action: runtime(cmdTeamMigrate),
},
{
Name: "list",
Usage: "List all teams",
Action: runtime(cmdTeamList),
},
{
Name: "synclist",
Usage: "Export a list of all teams suitable for sync",
Action: runtime(cmdTeamSync),
},
{
Name: "show",
Usage: "Show information about a team",
Action: runtime(cmdTeamShow),
},
{
Name: "update",
Usage: "Update team information",
Action: runtime(cmdTeamUpdate),
BashComplete: cmpl.TeamUpdate,
},
},
}, // end teams
}...,
)
return &app
}
func cmdTeamAdd(c *cli.Context) error {
utl.ValidateCliMinArgumentCount(c, 3)
switch utl.GetCliArgumentCount(c) {
case 3, 5:
break // nop
default:
utl.Abort("Syntax error, unexpected argument count")
}
multi := []string{}
required := []string{"ldap"}
unique := []string{"ldap", "system"}
opts := utl.ParseVariadicArguments(
multi,
unique,
required,
c.Args().Tail())
req := proto.Request{}
req.Team = &proto.Team{}
req.Team.Name = c.Args().First()
req.Team.LdapId = opts["ldap"][0]
if len(opts["system"]) > 0 {
bl, err := strconv.ParseBool(opts["system"][0])
if err != nil {
utl.Abort("Argument to system parameter must be boolean")
}
req.Team.IsSystem = bl
}
resp := utl.PostRequestWithBody(Client, req, "/teams/")
fmt.Println(resp)
return nil
}
func cmdTeamUpdate(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 5)
multi := []string{}
unique := []string{`name`, `ldap`, `system`}
required := []string{`name`, `ldap`}
opts := utl.ParseVariadicArguments(multi, unique, required, c.Args().Tail())
teamid := utl.TryGetTeamByUUIDOrName(Client, c.Args().First())
req := proto.NewTeamRequest()
req.Team.Name = opts[`name`][0]
req.Team.LdapId = opts[`ldap`][0]
if len(opts[`system`]) > 0 {
req.Team.IsSystem = utl.GetValidatedBool(opts[`system`][0])
}
path := fmt.Sprintf("/teams/%s", teamid)
resp := utl.PutRequestWithBody(Client, req, path)
fmt.Println(resp)
return nil
}
func cmdTeamDel(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
id := utl.TryGetTeamByUUIDOrName(Client, c.Args().First())
path := fmt.Sprintf("/teams/%s", id)
resp := utl.DeleteRequest(Client, path)
fmt.Println(resp)
return nil
}
func cmdTeamRename(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 3)
key := []string{"to"}
opts := utl.ParseVariadicArguments(key, key, key, c.Args().Tail())
id := utl.TryGetTeamByUUIDOrName(Client, c.Args().First())
path := fmt.Sprintf("/teams/%s", id)
req := proto.Request{}
req.Team = &proto.Team{}
req.Team.Name = opts["to"][0]
resp := utl.PatchRequestWithBody(Client, req, path)
fmt.Println(resp)
return nil
}
func cmdTeamMigrate(c *cli.Context) error {
// XXX
utl.Abort("Not implemented")
return nil
}
func cmdTeamList(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
resp, err := adm.GetReq(`/teams/`)
if err != nil {
return err
}
fmt.Println(resp)
return nil
}
func cmdTeamSync(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
resp, err := adm.GetReq(`/sync/teams/`)
if err != nil {
return err
}
fmt.Println(resp)
return nil
}
func cmdTeamShow(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
id := utl.TryGetTeamByUUIDOrName(Client, c.Args().First())
path := fmt.Sprintf("/teams/%s", id)
resp, err := adm.GetReq(path)
if err != nil {
return err
}
fmt.Println(resp)
return nil
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix