forked from src-d/go-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addcommand.go
91 lines (73 loc) · 2.08 KB
/
addcommand.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
package cli
import (
"fmt"
"reflect"
"github.com/jessevdk/go-flags"
)
// AddCommand adds a new command to the application. The command must have a
// special field defining name, short-description and long-description (see
// package documentation). It panics if the command is not valid.
// Returned CommandAdder can be used to add subcommands.
//
// Additional functions can be passed to manipulate the resulting *flags.Command
// after its initialization.
func (a *App) AddCommand(cmd interface{}, cfs ...func(*flags.Command)) CommandAdder {
return commandAdder{a.Parser}.AddCommand(cmd, cfs...)
}
// CommandAdder can be used to add subcommands.
type CommandAdder interface {
// AddCommand adds the given commands as subcommands of the
// cuurent one.
AddCommand(interface{}, ...func(*flags.Command)) CommandAdder
}
type commandAdder struct {
internalCommandAdder
}
type internalCommandAdder interface {
AddCommand(string, string, string, interface{}) (*flags.Command, error)
}
func (a commandAdder) AddCommand(cmd interface{}, cfs ...func(*flags.Command)) CommandAdder {
typ, err := getStructType(cmd)
if err != nil {
panic(err)
}
pc, err := getPlainCommandField(typ)
if err != nil {
panic(err)
}
name := pc.Tag.Get("name")
shortDescription := pc.Tag.Get("short-description")
longDescription := pc.Tag.Get("long-description")
c, err := a.internalCommandAdder.AddCommand(
name, shortDescription, longDescription, cmd)
if err != nil {
panic(err)
}
for _, cf := range cfs {
cf(c)
}
return commandAdder{c}
}
func getPlainCommandField(typ reflect.Type) (reflect.StructField, error) {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if isPlainCommandField(field) {
return field, nil
}
}
return reflect.StructField{}, fmt.Errorf("PlainCommand not found")
}
func isPlainCommandField(field reflect.StructField) bool {
if !field.Anonymous {
return false
}
if field.Type == reflect.TypeOf(PlainCommand{}) {
return true
}
for i := 0; i < field.Type.NumField(); i++ {
if isPlainCommandField(field.Type.Field(i)) {
return true
}
}
return false
}