-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
217 lines (189 loc) · 4.91 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Package contains the command line interface for iron-worker.
package main
import (
"flag"
"fmt"
"os"
"runtime"
"strings"
"github.com/fatih/color"
)
var (
// These are located after binary on command line
// TODO(reed): kind of awkward, since there are 2 different flag sets now:
// e.g.
// ironcli -token=123456789 upload -max-concurrency=10 my_worker
versionFlag = flag.Bool("version", false, "print the version number")
helpFlag = flag.Bool("help", false, "show this")
hFlag = flag.Bool("h", false, "show this")
tokenFlag = flag.String("token", "", "provide OAuth token")
projectIDFlag = flag.String("project-id", "", "provide project ID")
envFlag = flag.String("env", "", "provide specific dev environment")
red, yellow, green func(a ...interface{}) string
// i.e. worker: { commands... }
// mq: { commands... }
commands = map[string]commander{
"run": single{
new(RunCmd),
},
"docker": mapper{
"login": new(DockerLoginCmd),
},
"register": single{
new(RegisterCmd),
},
"worker": mapper{
"upload": new(UploadCmd),
"queue": new(QueueCmd),
"schedule": new(SchedCmd),
"status": new(StatusCmd),
"log": new(LogCmd),
},
"mq": mapper{
"push": new(PushCmd),
"pop": new(PopCmd),
"reserve": new(ReserveCmd),
"delete": new(DeleteCmd),
"peek": new(PeekCmd),
"clear": new(ClearCmd),
"list": new(ListCmd),
"create": new(CreateCmd),
"rm": new(RmCmd),
"info": new(InfoCmd),
},
"lambda": mapper{
"create-function": new(LambdaCreateCmd),
"test-function": new(LambdaTestFunctionCmd),
"publish-function": new(LambdaPublishCmd),
"aws-import": new(LambdaImportCmd),
},
}
)
const (
LINES = "-----> "
BLANKS = " "
INFO = " for more info"
Version = "0.1.6"
)
func usage() {
fmt.Fprintln(os.Stderr, "usage: ", os.Args[0], `[product] [command] [flags] [args]
where [product] is one of:
mq Commands to manage messages and queues on IronMQ.
worker Commands to queue and view IronWorker tasks.
docker Login to Docker Registry.
register Register an image or code package with IronWorker.
lambda Commands to convert AWS Lambda functions to Docker containers.
run '`+os.Args[0], `[product] -help for a list of commands.
run '`+os.Args[0], `[product] [command] -help' for [command]'s flags/args.
`)
fmt.Fprintln(os.Stderr, `[flags]:`)
flag.PrintDefaults()
os.Exit(0)
}
func pusage(p string) {
prod, ok := commands[p]
if !ok {
fmt.Fprintln(os.Stderr, red("invalid product ", `"`+p+`", `, "see -help"))
os.Exit(1)
}
subs := prod.Commands()
if len(subs) > 0 {
fmt.Fprintln(os.Stderr, p, "commands:")
for _, cmd := range subs {
fmt.Fprintln(os.Stderr, "\t", cmd)
}
}
}
type commander interface {
// Given a full set of command line args, call Args and Flags with
// whatever position needed to be sufficiently rad.
Command(args ...string) (Command, error)
Commands() []string
}
type (
// mapper expects > 0 args, calls flags after first arg
mapper map[string]Command
// runner calls flags on first (zeroeth) arg
single struct{ cmd Command }
)
func (s single) Commands() []string { return []string{} } // --help handled in Flags()
func (s single) Command(args ...string) (Command, error) {
err := s.cmd.Flags(args[0:]...)
if err == nil {
err = s.cmd.Args()
}
return s.cmd, err
}
func (m mapper) Commands() []string {
var c []string
for cmd := range m {
c = append(c, cmd)
}
return c
}
func (m mapper) Command(args ...string) (Command, error) {
c, ok := m[args[0]]
if !ok {
return nil, fmt.Errorf("command not found: %s", args[0])
}
err := c.Flags(args[1:]...)
if err == nil {
err = c.Args()
}
return c, err
}
func main() {
if runtime.GOOS == "windows" {
red = fmt.Sprint
yellow = fmt.Sprint
green = fmt.Sprint
} else {
red = color.New(color.FgRed).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
}
flag.Parse()
if *helpFlag || *hFlag {
usage()
} else if *versionFlag {
fmt.Println(Version)
os.Exit(0)
}
if flag.NArg() < 1 {
usage()
}
product := flag.Arg(0)
cmds, ok := commands[product]
if !ok || flag.NArg() < 2 {
pusage(product)
os.Exit(0)
}
cmdName := flag.Arg(1)
cmd, err := cmds.Command(flag.Args()[1:]...)
if err != nil {
// A single command or mapper subcommand will fail with ErrHelp.
if err == flag.ErrHelp {
if cmd != nil {
cmd.Usage()
}
os.Exit(0)
} else {
// A mapper top level command with -h as the 'subcommand' will not fail
// with ErrHelp, but complain about invalid flags, so we need to handle
// it separately.
switch strings.TrimSpace(cmdName) {
case "-h", "help", "--help", "-help":
pusage(product)
os.Exit(0)
}
fmt.Fprintln(os.Stderr, red(err))
os.Exit(1)
}
}
err = cmd.Config()
if err != nil {
fmt.Fprintln(os.Stderr, red(err))
os.Exit(2)
}
cmd.Run()
}