~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta6

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/user/info.go

  • Committer: Nicholas Skaggs
  • Date: 2016-04-20 14:50:35 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20160420145035-m3aj05xazahybzxn
Merge beta5 upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package user
5
5
 
6
6
import (
7
 
        "fmt"
8
7
        "time"
9
8
 
10
9
        "github.com/juju/cmd"
13
12
 
14
13
        "github.com/juju/juju/api/usermanager"
15
14
        "github.com/juju/juju/apiserver/params"
 
15
        "github.com/juju/juju/cmd/juju/common"
16
16
        "github.com/juju/juju/cmd/modelcmd"
17
17
)
18
18
 
20
20
Show information about a user.`[1:]
21
21
 
22
22
var helpDetails = `
23
 
By default, the YAML format is used and the user name is the current user.
 
23
By default, the YAML format is used and the user name is the current
 
24
user.
24
25
 
25
26
 
26
27
Examples:
27
 
juju show-user
28
 
juju show-user jsmith
29
 
juju show-user --format json
30
 
juju show-user --format yaml
31
 
 
 
28
    juju show-user
 
29
    juju show-user jsmith
 
30
    juju show-user --format json
 
31
    juju show-user --format yaml
 
32
    
32
33
See also: 
33
 
add-user
34
 
register
35
 
list-users`[1:]
 
34
    add-user
 
35
    register
 
36
    list-users`[1:]
36
37
 
37
38
// UserInfoAPI defines the API methods that the info command uses.
38
39
type UserInfoAPI interface {
49
50
}
50
51
 
51
52
func (c *infoCommandBase) SetFlags(f *gnuflag.FlagSet) {
52
 
        f.BoolVar(&c.exactTime, "exact-time", false, "use full timestamp precision")
 
53
        f.BoolVar(&c.exactTime, "exact-time", false, "Use full timestamp for connection times")
53
54
}
54
55
 
55
56
func NewShowUserCommand() cmd.Command {
75
76
func (c *infoCommand) Info() *cmd.Info {
76
77
        return &cmd.Info{
77
78
                Name:    "show-user",
78
 
                Args:    "<username>",
 
79
                Args:    "[<user name>]",
79
80
                Purpose: helpSummary,
80
81
                Doc:     helpDetails,
81
82
        }
139
140
                        Username:       info.Username,
140
141
                        DisplayName:    info.DisplayName,
141
142
                        Disabled:       info.Disabled,
142
 
                        LastConnection: LastConnection(info.LastConnection, now, c.exactTime),
 
143
                        LastConnection: common.LastConnection(info.LastConnection, now, c.exactTime),
143
144
                }
144
145
                if c.exactTime {
145
146
                        outInfo.DateCreated = info.DateCreated.String()
146
147
                } else {
147
 
                        outInfo.DateCreated = UserFriendlyDuration(info.DateCreated, now)
 
148
                        outInfo.DateCreated = common.UserFriendlyDuration(info.DateCreated, now)
148
149
                }
149
150
 
150
151
                output = append(output, outInfo)
152
153
 
153
154
        return output
154
155
}
155
 
 
156
 
// LastConnection turns the *time.Time returned from the API server
157
 
// into a user facing string with either exact time or a user friendly
158
 
// string based on the args.
159
 
func LastConnection(connectionTime *time.Time, now time.Time, exact bool) string {
160
 
        if connectionTime == nil {
161
 
                return "never connected"
162
 
        }
163
 
        if exact {
164
 
                return connectionTime.String()
165
 
        }
166
 
        return UserFriendlyDuration(*connectionTime, now)
167
 
}
168
 
 
169
 
// UserFriendlyDuration translates a time in the past into a user
170
 
// friendly string representation relative to the "now" time argument.
171
 
func UserFriendlyDuration(when, now time.Time) string {
172
 
        since := now.Sub(when)
173
 
        // if over 24 hours ago, just say the date.
174
 
        if since.Hours() >= 24 {
175
 
                return when.Format("2006-01-02")
176
 
        }
177
 
        if since.Hours() >= 1 {
178
 
                unit := "hours"
179
 
                if int(since.Hours()) == 1 {
180
 
                        unit = "hour"
181
 
                }
182
 
                return fmt.Sprintf("%d %s ago", int(since.Hours()), unit)
183
 
        }
184
 
        if since.Minutes() >= 1 {
185
 
                unit := "minutes"
186
 
                if int(since.Minutes()) == 1 {
187
 
                        unit = "minute"
188
 
                }
189
 
                return fmt.Sprintf("%d %s ago", int(since.Minutes()), unit)
190
 
        }
191
 
        if since.Seconds() >= 2 {
192
 
                return fmt.Sprintf("%d seconds ago", int(since.Seconds()))
193
 
        }
194
 
        return "just now"
195
 
}