/
add-group.go
66 lines (55 loc) · 1.41 KB
/
add-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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package admincmd
import (
"github.com/juju/cmd"
"golang.org/x/net/context"
"gopkg.in/errgo.v1"
"gopkg.in/juju/idmclient.v1/params"
)
type addGroupCommand struct {
userCommand
groups []string
}
func newAddGroupCommand() cmd.Command {
return &addGroupCommand{}
}
var addGroupDoc = `
The add-group command adds the specified user to the specified group,
or groups.
To add the group-1 and group-2 groups to the user bob:
user-admin add-group -u bob group-1 group-2
To add the group-1 and group-2 groups to the user with the email
address bob@example.com:
user-admin add-group -e bob@example.com group-1 group-2
`
func (c *addGroupCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "add-group",
Args: "[group...]",
Purpose: "add a user to groups",
Doc: addGroupDoc,
}
}
func (c *addGroupCommand) Init(args []string) error {
c.groups = args
return errgo.Mask(c.userCommand.Init(nil))
}
func (c *addGroupCommand) Run(ctxt *cmd.Context) error {
ctx := context.Background()
username, err := c.lookupUser(ctxt)
if err != nil {
return errgo.Mask(err)
}
client, err := c.Client(ctxt)
if err != nil {
return errgo.Mask(err)
}
err = client.ModifyUserGroups(ctx, ¶ms.ModifyUserGroupsRequest{
Username: username,
Groups: params.ModifyGroups{
Add: c.groups,
},
})
return errgo.Mask(err)
}