-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuserlist.go
More file actions
208 lines (176 loc) · 4.32 KB
/
userlist.go
File metadata and controls
208 lines (176 loc) · 4.32 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
package cli
import (
"context"
"fmt"
"time"
"github.com/jedib0t/go-pretty/v6/table"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)
func (r *RootCmd) userList() *serpent.Command {
formatter := cliui.NewOutputFormatter(
cliui.TableFormat([]codersdk.User{}, []string{"username", "email", "created at", "status"}),
cliui.JSONFormat(),
)
var githubUserID int64
cmd := &serpent.Command{
Use: "list",
Short: "Prints the list of users.",
Aliases: []string{"ls"},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
),
Options: serpent.OptionSet{
{
Name: "github-user-id",
Description: "Filter users by their GitHub user ID.",
Default: "",
Flag: "github-user-id",
Required: false,
Value: serpent.Int64Of(&githubUserID),
},
},
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
req := codersdk.UsersRequest{}
if githubUserID != 0 {
req.Search = fmt.Sprintf("github_com_user_id:%d", githubUserID)
}
res, err := client.Users(inv.Context(), req)
if err != nil {
return err
}
out, err := formatter.Format(inv.Context(), res.Users)
if err != nil {
return err
}
if out == "" {
cliui.Infof(inv.Stderr, "No users found.")
return nil
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
func (r *RootCmd) userSingle() *serpent.Command {
formatter := cliui.NewOutputFormatter(
&userShowFormat{},
cliui.JSONFormat(),
)
cmd := &serpent.Command{
Use: "show <username|user_id|'me'>",
Short: "Show a single user. Use 'me' to indicate the currently authenticated user.",
Long: FormatExamples(
Example{
Command: "coder users show me",
},
),
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
user, err := client.User(inv.Context(), inv.Args[0])
if err != nil {
return err
}
orgNames := make([]string, len(user.OrganizationIDs))
for i, orgID := range user.OrganizationIDs {
org, err := client.Organization(inv.Context(), orgID)
if err != nil {
return xerrors.Errorf("get organization %q: %w", orgID.String(), err)
}
orgNames[i] = org.Name
}
out, err := formatter.Format(inv.Context(), userWithOrgNames{
User: user,
OrganizationNames: orgNames,
})
if err != nil {
return err
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
type userWithOrgNames struct {
codersdk.User
OrganizationNames []string `json:"organization_names"`
}
type userShowFormat struct{}
var _ cliui.OutputFormat = &userShowFormat{}
// ID implements OutputFormat.
func (*userShowFormat) ID() string {
return "table"
}
// AttachOptions implements OutputFormat.
func (*userShowFormat) AttachOptions(_ *serpent.OptionSet) {}
// Format implements OutputFormat.
func (*userShowFormat) Format(_ context.Context, out interface{}) (string, error) {
user, ok := out.(userWithOrgNames)
if !ok {
return "", xerrors.Errorf("expected type %T, got %T", user, out)
}
tw := cliui.Table()
addRow := func(name string, value interface{}) {
key := ""
if name != "" {
key = name + ":"
}
tw.AppendRow(table.Row{
key, value,
})
}
// Add rows for each of the user's fields.
addRow("ID", user.ID.String())
addRow("Username", user.Username)
addRow("Full name", user.Name)
addRow("Email", user.Email)
addRow("Status", user.Status)
addRow("Created At", user.CreatedAt.Format(time.Stamp))
addRow("", "")
firstRole := true
for _, role := range user.Roles {
if role.DisplayName == "" {
// Skip roles with no display name.
continue
}
key := ""
if firstRole {
key = "Roles"
firstRole = false
}
addRow(key, role.DisplayName)
}
if firstRole {
addRow("Roles", "(none)")
}
addRow("", "")
firstOrg := true
for _, orgName := range user.OrganizationNames {
key := ""
if firstOrg {
key = "Organizations"
firstOrg = false
}
addRow(key, orgName)
}
if firstOrg {
addRow("Organizations", "(none)")
}
return tw.Render(), nil
}