/
user_create.go
113 lines (103 loc) · 2.44 KB
/
user_create.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
package commands
import (
"fmt"
"os"
"strings"
"golang.org/x/net/context"
pb "github.com/jaypipes/procession/proto"
"github.com/spf13/cobra"
)
var (
userCreateDisplayName string
userCreateEmail string
userCreateRoles string
)
var userCreateCommand = &cobra.Command{
Use: "create",
Short: "Creates a new user",
Run: userCreate,
}
func setupUserCreateFlags() {
userCreateCommand.Flags().StringVarP(
&userCreateDisplayName,
"display-name", "n",
"",
"Display name for the user.",
)
userCreateCommand.Flags().StringVarP(
&userCreateEmail,
"email", "e",
"",
"Email for the user.",
)
userCreateCommand.Flags().StringVarP(
&userCreateRoles,
"roles", "",
"",
"Comma-separated list of roles to add the user to."+
rolesHelpExtended,
)
}
func init() {
setupUserCreateFlags()
}
func userCreate(cmd *cobra.Command, args []string) {
checkAuthUser(cmd)
conn := connect()
defer conn.Close()
client := pb.NewIAMClient(conn)
req := &pb.UserSetRequest{
Session: &pb.Session{User: authUser},
Changed: &pb.UserSetFields{},
}
if cmd.Flags().Changed("display-name") {
req.Changed.DisplayName = &pb.StringValue{
Value: userCreateDisplayName,
}
} else {
fmt.Println("Specify a display name using --display-name=<NAME>.")
cmd.Usage()
os.Exit(1)
}
if cmd.Flags().Changed("email") {
req.Changed.Email = &pb.StringValue{
Value: userCreateEmail,
}
} else {
fmt.Println("Specify an email using --email=<EMAIL>.")
cmd.Usage()
os.Exit(1)
}
if cmd.Flags().Changed("roles") {
req.Changed.Roles = strings.Split(userCreateRoles, ",")
}
resp, err := client.UserSet(context.Background(), req)
exitIfError(err)
user := resp.User
if quiet {
fmt.Println(user.Uuid)
} else {
fmt.Printf("Successfully created user with UUID %s\n", user.Uuid)
}
if verbose {
fmt.Printf("UUID: %s\n", user.Uuid)
fmt.Printf("Display name: %s\n", user.DisplayName)
fmt.Printf("Email: %s\n", user.Email)
fmt.Printf("Slug: %s\n", user.Slug)
if len(user.Roles) == 0 {
fmt.Println("Roles: None")
} else {
roleStrs := make([]string, len(user.Roles))
for x, role := range user.Roles {
orgStr := ""
if role.Organization != nil {
orgStr = fmt.Sprintf(" (%s)", role.Organization.DisplayName)
}
tmp := fmt.Sprintf("%s%s", role.DisplayName, orgStr)
roleStrs[x] = tmp
}
roleStr := strings.Join(roleStrs, ", ")
fmt.Printf("Roles: %s\n", roleStr)
}
}
}