-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcli.go
391 lines (335 loc) · 9.95 KB
/
cli.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package clif
import (
"fmt"
"os"
"os/signal"
"reflect"
"strings"
)
// HeraldCallback is type for creator method, which can be registered with
// the `Herald()` method.
type HeraldCallback func(*Cli) *Command
// NamedParameters ...
type NamedParameters map[string]interface{}
// Cli is a command line interface object
type Cli struct {
// Name is the name of the console application used in the generated help
Name string
// Version is used in the generated help
Version string
// Description is used in the generated help
Description string
// Commands contain all registered commands and can be manipulated directly
Commands map[string]*Command
// Heralds contain list of command-create-callbacks which will be executed on `Run()`
Heralds []HeraldCallback
// Registry is a container holding objects for injection
Registry *Registry
// DefaultOptions contains options which are added to all commands early in the `Run()` call.
DefaultOptions []*Option
// DefaultCommand contains name of the command which is executed if non is given. Defaults to "list"
DefaultCommand string
// PreCall is executed before the chosen command is called, if defined
PreCall func(c *Command) error
// OnInterrupt, when set with `SetOnInterrupt`, is callback which is executed
// if user triggers interrupt (ctrl+c). If an error is returned, then the
// cli application will die with a non-zero status and print the error message.
onInterrupt func() error
// interruptChan is channel for set interrupt callback
interruptChan chan os.Signal
}
type CallError struct {
err error
}
func NewCallError(err error) *CallError {
return &CallError{err}
}
func IsCallError(err error) bool {
if err == nil {
return false
} else if _, ok := err.(*CallError); ok {
return true
}
return false
}
func (this *CallError) Error() string {
return fmt.Sprintf("Failure in execution: %s", this.err)
}
// New constructs new cli
func New(name, version, desc string) *Cli {
this := &Cli{
Name: name,
Version: version,
Description: desc,
Commands: make(map[string]*Command),
Heralds: make([]HeraldCallback, 0),
Registry: NewRegistry(),
DefaultOptions: make([]*Option, 0),
DefaultCommand: "list",
}
// add default helper commands.
this.Add(NewHelpCommand(), NewListCommand())
// setup output & input
out := NewColorOutput(os.Stdout)
this.Register(this).
SetOutput(out).
SetInput(NewDefaultInput(os.Stdin, out))
return this
}
// Add is a builder method for adding a new command
func (this *Cli) Add(cmd ...*Command) *Cli {
for _, c := range cmd {
this.Commands[c.Name] = c.SetCli(this)
}
return this
}
// NewDefaultOption creates and adds a new option to default list.
func (this *Cli) NewDefaultOption(name, alias, usage, _default string, required, multiple bool) *Cli {
return this.AddDefaultOptions(NewOption(name, alias, usage, _default, required, multiple))
}
// AddDefaultOptions adds a list of options to default options.
func (this *Cli) AddDefaultOptions(opts ...*Option) *Cli {
this.DefaultOptions = append(this.DefaultOptions, opts...)
return this
}
// Call executes command by building all input parameters based on objects
// registered in the container and running the callback.
func (this *Cli) Call(c *Command) ([]reflect.Value, error) {
this.Register(c)
if c.preCall != nil {
if _, err := this.call(*c.preCall, c); err != nil {
return nil, err
}
}
res, err := this.call(c.Call, c)
if err != nil {
return res, err
}
if c.postCall != nil {
if _, err := this.call(*c.postCall, c); err != nil {
return nil, err
}
}
return res, nil
}
func (this *Cli) call(call reflect.Value, c *Command) ([]reflect.Value, error) {
// build callback arguments and execute
method := call.Type()
input := make([]reflect.Value, method.NumIn())
named := NamedParameters(make(map[string]interface{}))
namedType := reflect.TypeOf(named).String()
namedIndex := -1
for i := 0; i < method.NumIn(); i++ {
t := method.In(i)
s := t.String()
if this.Registry.Has(s) {
input[i] = this.Registry.Get(s)
} else if s == namedType {
if namedIndex > -1 {
return nil, fmt.Errorf("Callback has more than the one allowed input parameter of type %s, which is used to inject named parameters", namedType)
}
namedIndex = i
} else {
return nil, fmt.Errorf("Callback parameter of type %s for command \"%s\" was not found in registry", s, c.Name)
}
}
if namedIndex > -1 {
this.Registry.Reduce(func(name string, value interface{}) bool {
if strings.Index(name, "N:") == 0 {
named[name[2:]] = value
}
return false
})
input[namedIndex] = reflect.ValueOf(NamedParameters(named))
}
outLen := method.NumOut()
res := call.Call(input)
if outLen > 0 && method.Out(outLen-1).String() == "error" {
vals := make([]reflect.Value, outLen-1)
if outLen > 1 {
for i := 0; i < outLen-1; i++ {
vals[i] = res[i]
}
}
if !res[outLen-1].IsNil() {
if err := res[outLen-1].Interface().(error); err != nil {
return vals, NewCallError(err)
}
}
return vals, nil
}
return res, nil
}
// Herald registers command constructors, which will be executed in `Run()`.
func (this *Cli) Herald(cmd ...HeraldCallback) *Cli {
for _, c := range cmd {
this.Heralds = append(this.Heralds, c)
}
return this
}
// New creates and adds a new command
func (this *Cli) New(name, usage string, call CallMethod) *Cli {
return this.Add(NewCommand(name, usage, call).SetCli(this))
}
// Output is shorthand for currently registered output
func (this *Cli) Output() Output {
t := reflect.TypeOf((*Output)(nil)).Elem()
out := this.Registry.Get(t.String())
return out.Interface().(Output)
}
// RegisterAs is builder method and registers object in registry
func (this *Cli) Register(v interface{}) *Cli {
this.Registry.Register(v)
return this
}
// RegisterAs is builder method and registers object under alias in registry
func (this *Cli) RegisterAs(n string, v interface{}) *Cli {
this.Registry.Alias(n, v)
return this
}
// RegisterNamed registers a parameter for injecting in a named map[string]inteface{}
func (this *Cli) RegisterNamed(n string, v interface{}) *Cli {
this.Registry.Alias(fmt.Sprintf("N:%s", n), v)
return this
}
// Named returns a named parameter of registry or nil
func (this *Cli) Named(n string) interface{} {
n = fmt.Sprintf("N:%s", n)
if v, ok := this.Registry.Container[n]; ok {
return v.Interface()
}
return nil
}
// Run with OS command line arguments
func (this *Cli) Run() {
this.RunWith(os.Args[1:])
}
// RunWith runs the cli with custom list of arguments
func (this *Cli) RunWith(args []string) {
if args == nil {
args = []string{}
}
// late init commands
for _, cb := range this.Heralds {
this.Add(cb(this))
}
this.Heralds = make([]HeraldCallback, 0)
for _, cmd := range this.Commands {
for _, opt := range this.DefaultOptions {
cmd.AddOption(opt)
}
}
// extract & continue with command
cname, cargs := this.SeparateArgs(args)
if c, ok := this.Commands[cname]; ok {
// parse arguments & options
err := c.Parse(cargs)
if help := c.Option("help"); help != nil && help.Bool() {
this.Output().Printf(DescribeCommand(c))
return
}
if err != nil {
this.Output().Printf(DescribeCommand(c))
Die("Parse error: %s", err)
}
if this.PreCall != nil {
if err = this.PreCall(c); err != nil {
Die(err.Error())
}
}
// execute callback & handle result
if _, err := this.Call(c); err != nil {
if IsCallError(err) {
Die(err.Error())
} else {
Die(err.Error())
}
}
} else {
if cname == "" && len(args) > 0 {
cname = args[0]
}
Die("Command \"%s\" unknown", cname)
}
}
// SeparateArgs takes (command line) args and tries to separate command name and
// the actual args & options
func (this *Cli) SeparateArgs(args []string) (string, []string) {
// determine command or fallback to default
// command name must NOT be first arg anymore, but first argument which is
// not an option i.e. does not begin with "-".
//
// With "foo" as command name, the following is valid:
// ./cli foo --bar boing baz
// ./cli --bar=boing foo baz
// ./cli foo baz --bar=boing
// And the following is invalid
// ./cli --bar boing baz foo
// ./cli baz --bar foo
// ./cli --bar baz foo
name := ""
largs := len(args)
cargs := []string{}
// special case: help command
_, hasListCommand := this.Commands["list"]
if largs == 0 {
return this.DefaultCommand, cargs
} else if hasListCommand && (args[0] == "-h" || args[0] == "--help") {
return "list", cargs
}
found := false
for _, arg := range args {
if found || strings.Index(arg, "-") == 0 {
cargs = append(cargs, arg)
} else {
name = arg
found = true
}
}
return name, cargs
}
// SetDefaultCommand is builder method and overwrites the default command ("list") with something else
func (this *Cli) SetDefaultCommand(v string) *Cli {
this.DefaultCommand = v
return this
}
// SetDescription is builder method and sets description
func (this *Cli) SetDescription(v string) *Cli {
this.Description = v
return this
}
// SetOutput is builder method and replaces current input
func (this *Cli) SetInput(in Input) *Cli {
t := reflect.TypeOf((*Input)(nil)).Elem()
this.Registry.Alias(t.String(), in)
return this
}
// SetOutput is builder method and replaces current output
func (this *Cli) SetOutput(out Output) *Cli {
t := reflect.TypeOf((*Output)(nil)).Elem()
this.Registry.Alias(t.String(), out)
return this
}
// SetOnInterrupt sets callback for interrupt signal (ctrl+c)
func (this *Cli) SetOnInterrupt(cb func() error) *Cli {
this.onInterrupt = cb
if this.interruptChan == nil {
this.interruptChan = make(chan os.Signal, 1)
signal.Notify(this.interruptChan, os.Interrupt)
go func() {
<-this.interruptChan
if err := this.onInterrupt(); err != nil {
Die(err.Error())
} else {
Exit(0)
}
}()
}
return this
}
// SetPreCall is builder method and sets a prepare method, which is called
// before any command is run
func (this *Cli) SetPreCall(cb func(c *Command) error) *Cli {
this.PreCall = cb
return this
}