forked from coreos/updateservicectl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
231 lines (200 loc) · 5.07 KB
/
cmd.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"text/tabwriter"
"github.com/coreos/updatectl/auth"
"github.com/coreos/updatectl/client/update/v1"
"github.com/coreos/updatectl/version"
)
const (
OK = iota
// Error Codes
ERROR_API
ERROR_USAGE
ERROR_NO_COMMAND
cliName = "updatectl"
cliDescription = "updatectl is a command line driven interface to the roller."
)
type StringFlag struct {
value *string
required bool
}
func (f *StringFlag) Set(value string) error {
f.value = &value
return nil
}
func (f *StringFlag) Get() *string {
return f.value
}
func (f *StringFlag) String() string {
if f.value != nil {
return *f.value
}
return ""
}
type Command struct {
Name string // Name of the Command and the string to use to invoke it
Summary string // One-sentence summary of what the Command does
Usage string // Usage options/arguments
Description string // Detailed description of command
Flags flag.FlagSet // Set of flags associated with this command
Run handlerFunc // Run a command with the given arguments
Subcommands []*Command // Subcommands for this command.
}
var (
out *tabwriter.Writer
globalFlagSet *flag.FlagSet
commands []*Command
globalFlags struct {
Server string
User string
Key string
Debug bool
Version bool
Help bool
SkipSSLVerify bool
}
)
func init() {
out = new(tabwriter.Writer)
out.Init(os.Stdout, 0, 8, 1, '\t', 0)
server := "http://localhost:8000" // default server
if serverEnv := os.Getenv("UPDATECTL_SERVER"); serverEnv != "" {
server = serverEnv
}
globalFlagSet = flag.NewFlagSet(cliName, flag.ExitOnError)
globalFlagSet.StringVar(&globalFlags.Server, "server", server, "Update server to connect to")
globalFlagSet.BoolVar(&globalFlags.Debug, "debug", false, "Output debugging info to stderr")
globalFlagSet.BoolVar(&globalFlags.Version, "version", false, "Print version information and exit.")
globalFlagSet.BoolVar(&globalFlags.Help, "help", false, "Print usage information and exit.")
globalFlagSet.BoolVar(&globalFlags.SkipSSLVerify, "skip-ssl-verify", false, "Don't check SSL certificates.")
globalFlagSet.StringVar(&globalFlags.User, "user", os.Getenv("UPDATECTL_USER"), "API Username")
globalFlagSet.StringVar(&globalFlags.Key, "key", os.Getenv("UPDATECTL_KEY"), "API Key")
commands = []*Command{
// admin.go
cmdAdminUser,
// app.go
cmdApp,
// channel.go
cmdChannel,
// database.go
cmdDatabase,
// group.go
cmdGroup,
// help.go
cmdHelp,
// instance.go
cmdInstance,
// pkg.go
cmdPackage,
// watch.go
cmdWatch,
// upstream.go
cmdUpstream,
}
}
type handlerFunc func([]string, *update.Service, *tabwriter.Writer) int
func getHawkClient(user string, key string) *http.Client {
return &http.Client{
Transport: &auth.HawkRoundTripper{
User: user,
Token: key,
SkipSSLVerify: globalFlags.SkipSSLVerify,
},
}
}
func handle(fn handlerFunc) func(f *flag.FlagSet) int {
return func(f *flag.FlagSet) (exit int) {
user := globalFlags.User
key := globalFlags.Key
client := getHawkClient(user, key)
service, err := update.New(client)
if err != nil {
log.Fatal(err)
}
service.BasePath = globalFlags.Server + "/_ah/api/update/v1/"
exit = fn(f.Args(), service, out)
return
}
}
func printVersion(out *tabwriter.Writer) {
fmt.Fprintf(out, "%s version %s\n", cliName, version.Version)
out.Flush()
}
func getAllFlags() (flags []*flag.Flag) {
return getFlags(globalFlagSet)
}
func getFlags(flagset *flag.FlagSet) (flags []*flag.Flag) {
flags = make([]*flag.Flag, 0)
flagset.VisitAll(func(f *flag.Flag) {
flags = append(flags, f)
})
return
}
// determine which Command should be run
func findCommand(search string, args []string, commands []*Command) (cmd *Command, name string) {
if len(args) < 1 {
return
}
if search == "" {
search = args[0]
} else {
search = fmt.Sprintf("%s %s", search, args[0])
}
name = search
for _, c := range commands {
if c.Name == search {
cmd = c
if errHelp := c.Flags.Parse(args[1:]); errHelp != nil {
printCommandUsage(cmd)
os.Exit(ERROR_USAGE)
}
if len(cmd.Subcommands) != 0 {
subArgs := cmd.Flags.Args()
var subCmd *Command
subCmd, name = findCommand(search, subArgs, cmd.Subcommands)
if subCmd != nil {
cmd = subCmd
}
}
break
}
}
return
}
func main() {
globalFlagSet.Parse(os.Args[1:])
var args = globalFlagSet.Args()
if globalFlags.Version {
printVersion(out)
os.Exit(OK)
}
if globalFlags.Help {
printGlobalUsage()
os.Exit(OK)
}
// no command specified - trigger help
if len(args) < 1 {
args = append(args, "help")
}
cmd, name := findCommand("", args, commands)
if cmd == nil {
fmt.Printf("%v: unknown subcommand: %q\n", cliName, name)
fmt.Printf("Run '%v help' for usage.\n", cliName)
os.Exit(ERROR_NO_COMMAND)
}
if cmd.Run == nil {
printCommandUsage(cmd)
os.Exit(ERROR_USAGE)
} else {
exit := handle(cmd.Run)(&cmd.Flags)
if exit == ERROR_USAGE {
printCommandUsage(cmd)
}
os.Exit(exit)
}
}