-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (131 loc) · 3.3 KB
/
main.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/crocos/rds-testrunner/command"
"github.com/crocos/rds-testrunner/config"
"github.com/crocos/rds-testrunner/notify"
"github.com/mitchellh/cli"
"github.com/mitchellh/colorstring"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/rds"
)
const VERSION = "0.1.0"
func main() {
exitCode, err := realMain()
if err != nil {
log.Fatal(err)
}
os.Exit(exitCode)
}
func realMain() (exitCode int, err error) {
resourceName := "default"
args := make([]string, 0, len(os.Args))
for k, arg := range os.Args {
if arg == "" {
continue
}
if arg == "-r" || arg == "--r" {
resourceName = os.Args[k+1]
os.Args[k] = ""
os.Args[k+1] = ""
continue
}
args = append(args, arg)
}
credential, err := config.GetAwsCredential()
auth, err := aws.GetAuth(credential.Key, credential.Secret)
if err != nil {
exitCode = 1
return
}
// get resource
resourceData, err := config.GetResource(resourceName)
if err != nil {
exitCode = 1
return
}
// get notify config
notifyConfig, err := config.GetNotifyConfig()
if err != nil {
exitCode = 1
return
}
// get rds client
client := rds.New(auth, aws.Regions[resourceData.InstanceRegion])
// get cli
c := cli.NewCLI("rds-testrunner", VERSION)
c.Args = args[1:]
// get output ui
ui := &cli.PrefixedUi{
AskPrefix: "",
OutputPrefix: colorstring.Color("[bold][green][out] "),
InfoPrefix: colorstring.Color("[bold][yellow][info] "),
ErrorPrefix: colorstring.Color("[bold][red][error] "),
Ui: &cli.BasicUi{Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stderr},
}
// get notifier
notifier := notify.NewNotifier(notifyConfig)
// create command utils
base := command.Base{
Ui: ui,
Resource: resourceData,
Client: client,
Notifier: notifier,
}
c.Commands = map[string]cli.CommandFactory{
"open": func() (cli.Command, error) {
return &command.OpenCommand{
Base: base,
}, nil
},
"close": func() (cli.Command, error) {
return &command.CloseCommand{
Base: base,
}, nil
},
"list": func() (cli.Command, error) {
return &command.ListCommand{
Base: base,
}, nil
},
}
c.HelpFunc = helpFunc(args[0])
exitCode, err = c.Run()
return
}
func helpFunc(app string) cli.HelpFunc {
return func(commands map[string]cli.CommandFactory) string {
help := fmt.Sprintf("Usage: %s [--version] [--help] [-r] <command> [<command args>]\n\n", app)
help += "DB mirroring and Query execution measurement tool.\n\n"
help += "Available commands are:\n"
keys := make([]string, 0, len(commands))
maxKeyLen := 0
for key, _ := range commands {
if len(key) > maxKeyLen {
maxKeyLen = len(key)
}
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
commandFunc, ok := commands[key]
if !ok {
panic("command not found: " + key)
}
command, err := commandFunc()
if err != nil {
log.Printf("[ERR] cli: Command '%s' failed to load: %s", key, err)
continue
}
key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key)))
help += fmt.Sprintf(" %s %s\n", key, command.Synopsis())
}
help += "\nOptions:\n"
help += fmt.Sprintf(" -r <resource> Choice 'resource' name on your config file. if not set, use 'default'\n")
return help
}
}