This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
org_list_users.go
81 lines (65 loc) · 2.04 KB
/
org_list_users.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
package secrethub
import (
"fmt"
"sort"
"text/tabwriter"
"github.com/secrethub/secrethub-cli/internals/cli"
"github.com/secrethub/secrethub-cli/internals/cli/ui"
"github.com/secrethub/secrethub-go/internals/api"
)
// OrgListUsersCommand handles listing the users of an organization.
type OrgListUsersCommand struct {
orgName api.OrgName
useTimestamps bool
io ui.IO
newClient newClientFunc
timeFormatter TimeFormatter
}
// NewOrgListUsersCommand creates a new OrgListUsersCommand.
func NewOrgListUsersCommand(io ui.IO, newClient newClientFunc) *OrgListUsersCommand {
return &OrgListUsersCommand{
io: io,
newClient: newClient,
}
}
// Register registers the command, arguments and flags on the provided Registerer.
func (cmd *OrgListUsersCommand) Register(r cli.Registerer) {
clause := r.Command("list-users", "List all members of an organization.")
clause.Alias("list-members")
registerTimestampFlag(clause, &cmd.useTimestamps)
clause.BindAction(cmd.Run)
clause.BindArguments([]cli.Argument{
{Value: &cmd.orgName, Name: "org-name", Required: true, Description: "The organization name."},
})
}
// Run lists the users of an organization.
func (cmd *OrgListUsersCommand) Run() error {
cmd.beforeRun()
return cmd.run()
}
// beforeRun configures the command using the flag values.
func (cmd *OrgListUsersCommand) beforeRun() {
cmd.timeFormatter = NewTimeFormatter(cmd.useTimestamps)
}
// run lists the users of an organization.
func (cmd *OrgListUsersCommand) run() error {
client, err := cmd.newClient()
if err != nil {
return err
}
resp, err := client.Orgs().Members().List(cmd.orgName.Value())
if err != nil {
return err
}
sort.Sort(api.SortOrgMemberByUsername(resp))
w := tabwriter.NewWriter(cmd.io.Output(), 0, 2, 2, ' ', 0)
fmt.Fprintf(w, "%s\t%s\t%s\n", "USER", "ROLE", "LAST CHANGED")
for _, member := range resp {
fmt.Fprintf(w, "%s\t%s\t%s\n", member.User.Username, member.Role, cmd.timeFormatter.Format(member.LastChangedAt.Local()))
}
err = w.Flush()
if err != nil {
return err
}
return nil
}