forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_user.go
105 lines (93 loc) · 2.25 KB
/
get_user.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
package cmd
import (
"io"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/spf13/cobra"
)
// GetUserOptions containers the CLI options
type GetUserOptions struct {
GetOptions
Pending bool
}
var (
getUserLong = templates.LongDesc(`
Display the Users
`)
getUserExample = templates.Examples(`
# List the users
jx get user
`)
)
// NewCmdGetUser creates the new command for: jx get env
func NewCmdGetUser(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetUserOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "users",
Short: "Display the User or Users the current user is a member of",
Aliases: []string{"user"},
Long: getUserLong,
Example: getUserExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Pending, "pending", "p", false, "Display only pending Users which are not yet provisioned yet")
options.addGetFlags(cmd)
return cmd
}
// Run implements this command
func (o *GetUserOptions) Run() error {
err := o.registerUserCRD()
if err != nil {
return err
}
err = o.registerEnvironmentRoleBindingCRD()
if err != nil {
return err
}
jxClient, ns, err := o.JXClientAndAdminNamespace()
if err != nil {
return err
}
users, names, err := kube.GetUsers(jxClient, ns)
if err != nil {
return err
}
if len(names) == 0 {
log.Info(`
There are no Users yet. Try create one via: jx create user
`)
return nil
}
table := o.CreateTable()
table.AddRow("LOGIN", "NAME", "EMAIL", "URL", "ROLES")
for _, name := range names {
user := users[name]
if user != nil {
spec := &user.Spec
userKind := user.SubjectKind()
userName := user.Name
roleNames, err := kube.GetUserRoles(jxClient, ns, userKind, userName)
if err != nil {
log.Warnf("Failed to find User roles in namespace %s for User %s kind %s: %s\n", ns, userName, userKind, err)
}
table.AddRow(userName, spec.Name, spec.Email, spec.URL, strings.Join(roleNames, ", "))
}
}
table.Render()
return nil
}