forked from 99designs/iamy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiamy.go
87 lines (73 loc) · 1.96 KB
/
iamy.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
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
Version string = "dev"
defaultDir string
dryRun *bool
)
type logWriter struct{ *log.Logger }
func (w logWriter) Write(b []byte) (int, error) {
w.Printf("%s", b)
return len(b), nil
}
type Ui struct {
*log.Logger
Error, Debug *log.Logger
Exit func(code int)
}
func main() {
var (
debug = kingpin.Flag("debug", "Show debugging output").Bool()
pull = kingpin.Command("pull", "Syncs IAM users, groups and policies from the active AWS account to files")
pullDir = pull.Flag("dir", "The directory to dump yaml files to").Default(defaultDir).Short('d').String()
canDelete = pull.Flag("delete", "Delete extraneous files from destination dir").Bool()
push = kingpin.Command("push", "Syncs IAM users, groups and policies from files to the active AWS account")
pushDir = push.Flag("dir", "The directoy to load yaml files from").Default(defaultDir).Short('d').ExistingDir()
)
dryRun = kingpin.Flag("dry-run", "Show what would happen, but don't prompt to do it").Bool()
kingpin.Version(Version)
kingpin.CommandLine.Help =
`Read and write AWS IAM users, policies, groups and roles from YAML files.`
ui := Ui{
Logger: log.New(os.Stdout, "", 0),
Error: log.New(os.Stderr, "", 0),
Debug: log.New(ioutil.Discard, "", 0),
Exit: os.Exit,
}
cmd := kingpin.Parse()
if *debug {
ui.Debug = log.New(os.Stderr, "DEBUG ", log.LstdFlags)
log.SetFlags(0)
log.SetOutput(&logWriter{ui.Debug})
} else {
log.SetOutput(ioutil.Discard)
}
switch cmd {
case push.FullCommand():
PushCommand(ui, PushCommandInput{
Dir: *pushDir,
})
case pull.FullCommand():
PullCommand(ui, PullCommandInput{
Dir: *pullDir,
CanDelete: *canDelete,
})
}
}
func init() {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
dir, err = filepath.EvalSymlinks(dir)
if err != nil {
panic(err)
}
defaultDir = filepath.Clean(dir)
}