forked from juju/juju
/
list.go
243 lines (212 loc) · 5.94 KB
/
list.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for infos.
package user
import (
"io"
"github.com/juju/ansiterm"
"github.com/juju/clock"
"github.com/juju/cmd"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api/usermanager"
"github.com/juju/juju/apiserver/params"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/cmd/output"
)
var usageListUsersSummary = `
Lists Juju users allowed to connect to a controller or model.`[1:]
var usageListUsersDetails = `
When used without a model name argument, users relevant to a controller are printed.
When used with a model name, users relevant to the specified model are printed.
Examples:
Print the users relevant to the current controller:
juju users
Print the users relevant to the controller "another":
juju users -c another
Print the users relevant to the model "mymodel":
juju users mymodel
See also:
add-user
register
show-user
disable-user
enable-user`[1:]
func NewListCommand() cmd.Command {
return modelcmd.WrapController(&listCommand{
infoCommandBase: infoCommandBase{
clock: clock.WallClock,
},
})
}
// listCommand shows all the users in the Juju server.
type listCommand struct {
infoCommandBase
modelUserAPI modelUsersAPI
All bool
modelName string
currentUser string
}
// ModelUsersAPI defines the methods on the client API that the
// users command calls.
type modelUsersAPI interface {
Close() error
ModelUserInfo() ([]params.ModelUserInfo, error)
}
func (c *listCommand) getModelAPI() (modelUsersAPI, error) {
if c.modelUserAPI != nil {
return c.modelUserAPI, nil
}
conn, err := c.NewModelAPIRoot(c.modelName)
if err != nil {
return nil, errors.Trace(err)
}
return conn.Client(), nil
}
// Info implements Command.Info.
func (c *listCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "users",
Purpose: usageListUsersSummary,
Doc: usageListUsersDetails,
Aliases: []string{"list-users"},
})
}
// SetFlags implements Command.SetFlags.
func (c *listCommand) SetFlags(f *gnuflag.FlagSet) {
c.infoCommandBase.SetFlags(f)
f.BoolVar(&c.All, "all", false, "Include disabled users")
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
"tabular": c.formatTabular,
})
}
// Init implements Command.Init.
func (c *listCommand) Init(args []string) (err error) {
c.modelName, err = cmd.ZeroOrOneArgs(args)
if err != nil {
return err
}
return err
}
// Run implements Command.Run.
func (c *listCommand) Run(ctx *cmd.Context) (err error) {
if c.out.Name() == "tabular" {
// Only the tabular outputters need to know the current user,
// but both of them do, so do it in one place.
accountDetails, err := c.CurrentAccountDetails()
if err != nil {
return err
}
c.currentUser = names.NewUserTag(accountDetails.User).Id()
}
if c.modelName == "" {
return c.controllerUsers(ctx)
}
return c.modelUsers(ctx)
}
func (c *listCommand) modelUsers(ctx *cmd.Context) error {
client, err := c.getModelAPI()
if err != nil {
return err
}
defer client.Close()
result, err := client.ModelUserInfo()
if err != nil {
return err
}
if len(result) == 0 {
ctx.Infof("No users to display.")
return nil
}
return c.out.Write(ctx, common.ModelUserInfoFromParams(result, c.clock.Now()))
}
func (c *listCommand) controllerUsers(ctx *cmd.Context) error {
// Note: the InfoCommandBase and the UserInfo struct are defined
// in info.go.
client, err := c.getUserInfoAPI()
if err != nil {
return err
}
defer client.Close()
result, err := client.UserInfo(nil, usermanager.IncludeDisabled(c.All))
if err != nil {
return err
}
if len(result) == 0 {
ctx.Infof("No users to display.")
return nil
}
return c.out.Write(ctx, c.apiUsersToUserInfoSlice(result))
}
func (c *listCommand) formatTabular(writer io.Writer, value interface{}) error {
if c.modelName == "" {
return c.formatControllerUsers(writer, value)
}
return c.formatModelUsers(writer, value)
}
func (c *listCommand) isLoggedInUser(username string) bool {
tag := names.NewUserTag(username)
return tag.Id() == c.currentUser
}
func (c *listCommand) formatModelUsers(writer io.Writer, value interface{}) error {
users, ok := value.(map[string]common.ModelUserInfo)
if !ok {
return errors.Errorf("expected value of type %T, got %T", users, value)
}
modelUsers := set.NewStrings()
for name := range users {
modelUsers.Add(name)
}
tw := output.TabWriter(writer)
w := output.Wrapper{tw}
w.Println("Name", "Display name", "Access", "Last connection")
for _, name := range modelUsers.SortedValues() {
user := users[name]
var highlight *ansiterm.Context
userName := name
if c.isLoggedInUser(name) {
userName += "*"
highlight = output.CurrentHighlight
}
w.PrintColor(highlight, userName)
w.Println(user.DisplayName, user.Access, user.LastConnection)
}
tw.Flush()
return nil
}
func (c *listCommand) formatControllerUsers(writer io.Writer, value interface{}) error {
controllerName, err := c.ControllerName()
if err != nil {
return errors.Trace(err)
}
users, valueConverted := value.([]UserInfo)
if !valueConverted {
return errors.Errorf("expected value of type %T, got %T", users, value)
}
tw := output.TabWriter(writer)
w := output.Wrapper{tw}
w.Println("Controller: " + controllerName)
w.Println()
w.Println("Name", "Display name", "Access", "Date created", "Last connection")
for _, user := range users {
conn := user.LastConnection
if user.Disabled {
conn += " (disabled)"
}
var highlight *ansiterm.Context
userName := user.Username
if c.isLoggedInUser(user.Username) {
userName += "*"
highlight = output.CurrentHighlight
}
w.PrintColor(highlight, userName)
w.Println(user.DisplayName, user.Access, user.DateCreated, conn)
}
tw.Flush()
return nil
}