36
40
func NewListCommand() cmd.Command {
37
return modelcmd.WrapController(&listCommand{})
41
return modelcmd.WrapController(&listCommand{
42
infoCommandBase: infoCommandBase{
43
clock: clock.WallClock,
40
48
// listCommand shows all the users in the Juju server.
41
49
type listCommand struct {
51
modelUserAPI modelUsersAPI
58
// ModelUsersAPI defines the methods on the client API that the
59
// users command calls.
60
type modelUsersAPI interface {
62
ModelUserInfo() ([]params.ModelUserInfo, error)
65
func (c *listCommand) getModelAPI() (modelUsersAPI, error) {
66
if c.modelUserAPI != nil {
67
return c.modelUserAPI, nil
69
conn, err := c.NewModelAPIRoot(c.modelName)
71
return nil, errors.Trace(err)
73
return conn.Client(), nil
46
76
// Info implements Command.Info.
97
// Init implements Command.Init.
98
func (c *listCommand) Init(args []string) (err error) {
99
c.modelName, err = cmd.ZeroOrOneArgs(args)
67
106
// Run implements Command.Run.
68
107
func (c *listCommand) Run(ctx *cmd.Context) (err error) {
108
if c.out.Name() == "tabular" {
109
// Only the tabular outputters need to know the current user,
110
// but both of them do, so do it in one place.
111
accountDetails, err := c.ClientStore().AccountDetails(c.ControllerName())
115
c.currentUser = names.NewUserTag(accountDetails.User).Canonical()
117
if c.modelName == "" {
118
return c.controllerUsers(ctx)
120
return c.modelUsers(ctx)
123
func (c *listCommand) modelUsers(ctx *cmd.Context) error {
124
client, err := c.getModelAPI()
130
result, err := client.ModelUserInfo()
134
if len(result) == 0 {
135
ctx.Infof("No users to display.")
138
return c.out.Write(ctx, common.ModelUserInfoFromParams(result, c.clock.Now()))
141
func (c *listCommand) controllerUsers(ctx *cmd.Context) error {
69
142
// Note: the InfoCommandBase and the UserInfo struct are defined
71
144
client, err := c.getUserInfoAPI()
155
if len(result) == 0 {
156
ctx.Infof("No users to display.")
82
160
return c.out.Write(ctx, c.apiUsersToUserInfoSlice(result))
85
163
func (c *listCommand) formatTabular(writer io.Writer, value interface{}) error {
164
if c.modelName == "" {
165
return c.formatControllerUsers(writer, value)
167
return c.formatModelUsers(writer, value)
170
func (c *listCommand) isLoggedInUser(username string) bool {
171
tag := names.NewUserTag(username)
172
return tag.Canonical() == c.currentUser
175
func (c *listCommand) formatModelUsers(writer io.Writer, value interface{}) error {
176
users, ok := value.(map[string]common.ModelUserInfo)
178
return errors.Errorf("expected value of type %T, got %T", users, value)
180
modelUsers := set.NewStrings()
181
for name := range users {
184
tw := output.TabWriter(writer)
185
w := output.Wrapper{tw}
186
w.Println("NAME", "DISPLAY NAME", "ACCESS", "LAST CONNECTION")
187
for _, name := range modelUsers.SortedValues() {
190
var highlight *ansiterm.Context
192
if c.isLoggedInUser(name) {
194
highlight = output.CurrentHighlight
196
w.PrintColor(highlight, userName)
197
w.Println(user.DisplayName, user.Access, user.LastConnection)
203
func (c *listCommand) formatControllerUsers(writer io.Writer, value interface{}) error {
86
204
users, valueConverted := value.([]UserInfo)
87
205
if !valueConverted {
88
206
return errors.Errorf("expected value of type %T, got %T", users, value)
90
accountDetails, err := c.ClientStore().AccountDetails(c.ControllerName())
94
loggedInUser := names.NewUserTag(accountDetails.User).Canonical()
96
209
tw := output.TabWriter(writer)
97
fmt.Fprintf(tw, "CONTROLLER: %v\n", c.ControllerName())
99
fmt.Fprint(tw, "NAME\tDISPLAY NAME\tACCESS\tDATE CREATED\tLAST CONNECTION\n")
210
w := output.Wrapper{tw}
211
w.Println("CONTROLLER: " + c.ControllerName())
213
w.Println("NAME", "DISPLAY NAME", "ACCESS", "DATE CREATED", "LAST CONNECTION")
100
214
for _, user := range users {
101
215
conn := user.LastConnection
102
216
if user.Disabled {
103
217
conn += " (disabled)"
219
var highlight *ansiterm.Context
105
220
userName := user.Username
106
if loggedInUser == names.NewUserTag(user.Username).Canonical() {
221
if c.isLoggedInUser(user.Username) {
108
output.CurrentHighlight.Fprintf(tw, "%s\t", userName)
110
fmt.Fprintf(tw, "%s\t", userName)
223
highlight = output.CurrentHighlight
112
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", user.DisplayName, user.Access, user.DateCreated, conn)
225
w.PrintColor(highlight, userName)
226
w.Println(user.DisplayName, user.Access, user.DateCreated, conn)