-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.go
More file actions
113 lines (95 loc) · 2.51 KB
/
users.go
File metadata and controls
113 lines (95 loc) · 2.51 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/mitchellh/cli"
"github.com/pr8kerl/organizer/aws"
)
// List Users
type ListUsersCommand struct {
AccountId string
Report bool
Region string
Ui cli.Ui
}
func listUsersCmdFactory() (cli.Command, error) {
ui := &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
return &ListUsersCommand{
AccountId: "",
Report: false,
Region: "",
Ui: ui,
}, nil
}
func (c *ListUsersCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("list users", flag.ContinueOnError)
cmdFlags.StringVar(&c.AccountId, "accountid", "", "list iam users for a specific account")
cmdFlags.BoolVar(&c.Report, "report", false, "generate and show iam credential reports distributions")
cmdFlags.Usage = func() { c.Ui.Output(c.Help()); os.Exit(1) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
org, err := aws.NewOrganization()
if err != nil {
fmt.Printf("error: could not initialize organization: %s\n", err)
return 1
}
if len(c.AccountId) > 0 {
if c.Report {
err = org.GenerateCredentialReportForAccount(c.AccountId)
if err != nil {
fmt.Printf("error: could generate iam credential report for account: %s\n", err)
return 1
}
err = org.PrintCredentialReportForAccount(c.AccountId)
if err != nil {
fmt.Printf("error: could get iam credential report for account: %s\n", err)
return 1
}
} else {
err = org.PrintUsersForAccount(c.AccountId)
if err != nil {
fmt.Printf("error: could not list iam users for account: %s\n", err)
return 1
}
}
} else {
if c.Report {
err = org.GenerateCredentialReports()
if err != nil {
fmt.Printf("error: could not generate all iam credential reports: %s\n", err)
return 1
}
err = org.PrintCredentialReports()
if err != nil {
fmt.Printf("error: could not print all iam credential reports: %s\n", err)
return 1
}
} else {
err = org.PrintUsers()
if err != nil {
fmt.Printf("error: could not list all iam users: %s\n", err)
return 1
}
}
}
return 0
}
func (c *ListUsersCommand) Help() string {
helpText := `usage: organizer list users [<args>]
List all iam users within accounts for an organization
Options:
-accountid list all iam users for a specific account only
-report run a credential report
`
return strings.TrimSpace(helpText)
}
func (c *ListUsersCommand) Synopsis() string {
return "list all iam users for all accounts in an organization"
}