forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
94 lines (72 loc) · 1.89 KB
/
new.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
package groups
import (
"errors"
"fmt"
"io"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
userapi "github.com/openshift/origin/pkg/user/api"
)
const (
NewGroupRecommendedName = "new"
newLong = `
Create a new group.
This command will create a new group with an optional list of users.`
newExample = ` # Add a group with no users
%[1]s my-group
# Add a group with two users
%[1]s my-group user1 user2`
)
type NewGroupOptions struct {
GroupClient client.GroupInterface
Group string
Users []string
}
func NewCmdNewGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &NewGroupOptions{}
cmd := &cobra.Command{
Use: name + " GROUP [USER ...]",
Short: "Create a new group",
Long: newLong,
Example: fmt.Sprintf(newExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
kcmdutil.CheckErr(options.AddGroup())
},
}
return cmd
}
func (o *NewGroupOptions) Complete(f *clientcmd.Factory, args []string) error {
if len(args) < 1 {
return errors.New("You must specify at least one argument: GROUP [USER ...]")
}
o.Group = args[0]
if len(args) > 1 {
o.Users = append(o.Users, args[1:]...)
}
osClient, _, err := f.Clients()
if err != nil {
return err
}
o.GroupClient = osClient.Groups()
return nil
}
func (o *NewGroupOptions) AddGroup() error {
group := &userapi.Group{}
group.Name = o.Group
usedNames := sets.String{}
for _, user := range o.Users {
if usedNames.Has(user) {
continue
}
usedNames.Insert(user)
group.Users = append(group.Users, user)
}
_, err := o.GroupClient.Create(group)
return err
}