forked from iron-io/ironcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (118 loc) · 2.79 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
// Package contains the command line interface for iron-worker.
package main
import (
"flag"
"fmt"
"os"
"strings"
)
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, "what year is it")
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")
// i.e. worker: { commands... }
// mq: { commands... }
commands map[string]map[string]Command
)
const (
LINES = "-----> "
BLANKS = " "
INFO = " for more info"
Version = "0.0.8"
)
func usage() {
fmt.Fprintln(os.Stderr, "usage of", os.Args[0]+`:
`, os.Args[0], `[product] [command] [flags] [args]
where [product] is one of:
worker
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) {
// TODO list commands
switch p {
case "worker":
fmt.Fprintln(os.Stderr, p, "commands:")
for cmd := range commands["worker"] {
fmt.Fprintln(os.Stderr, "\t", cmd)
}
os.Exit(0)
case "mq":
fmt.Fprintln(os.Stderr, "not yet")
os.Exit(1)
default:
fmt.Fprintln(os.Stderr, "invalid product", `"`+p+`",`, "see -help")
os.Exit(1)
}
}
func init() {
commands = map[string]map[string]Command{
"worker": map[string]Command{
"upload": new(UploadCmd),
"queue": new(QueueCmd),
"schedule": new(SchedCmd),
"status": new(StatusCmd),
"log": new(LogCmd),
},
// TODO mq
}
}
func main() {
flag.Parse()
if *helpFlag || *hFlag {
usage()
} else if *versionFlag {
fmt.Fprintln(os.Stderr, Version)
os.Exit(0)
}
if flag.NArg() < 1 {
usage()
}
product := flag.Arg(0)
cmds, ok := commands[product]
if !ok {
pusage(product)
}
if flag.NArg() < 2 {
pusage(product)
}
cmdName := flag.Arg(1)
cmd, ok := cmds[cmdName]
if !ok {
switch strings.TrimSpace(cmdName) {
case "-h", "help", "--help", "-help":
pusage(product)
default:
fmt.Fprintln(os.Stderr, cmdName, "not a command, see -h")
}
os.Exit(1)
}
// each command defines its flags, err is either ErrHelp or bad flag value
if err := cmd.Flags(flag.Args()[2:]...); err != nil {
if err != flag.ErrHelp {
fmt.Println(err)
}
os.Exit(2)
}
if err := cmd.Args(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
err := cmd.Config()
if err != nil {
fmt.Println(err)
os.Exit(2)
}
cmd.Run()
}