forked from iron-io/ironcli
/
main.go
190 lines (164 loc) · 4.09 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
// Package contains the command line interface for iron-worker.
package main
import (
"flag"
"fmt"
"os"
"runtime"
"strings"
"github.com/iron-io/ironcli/vendored/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": runner{},
"worker": mapper{
"docker-login": new(DockerLoginCmd),
"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),
},
}
)
const (
LINES = "-----> "
BLANKS = " "
INFO = " for more info"
Version = "v0.0.16"
)
func usage() {
fmt.Fprintln(os.Stderr, "usage: ", os.Args[0], `[product] [command] [flags] [args]
where [product] is one of:
mq
worker
run
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)
}
fmt.Fprintln(os.Stderr, p, "commands:")
for _, cmd := range prod.Commands() {
fmt.Fprintln(os.Stderr, "\t", cmd)
}
os.Exit(0)
}
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
runner struct{}
)
func (r runner) Commands() []string { return []string{"just run!"} } // --help handled in Flags()
func (r runner) Command(args ...string) (Command, error) {
run := new(RunCmd)
err := run.Flags(args[0:]...)
if err == nil {
err = run.Args()
}
return run, 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)
}
cmdName := flag.Arg(1)
cmd, err := cmds.Command(flag.Args()[1:]...)
if err != nil {
if err == flag.ErrHelp && cmd != nil {
cmd.Usage()
}
switch strings.TrimSpace(cmdName) {
case "-h", "help", "--help", "-help":
pusage(product)
default:
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()
}