-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.go
174 lines (157 loc) · 3.19 KB
/
command.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
package cmd
import (
"fmt"
"path"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func _default(c *cobra.Command, args []string) {
fmt.Println("default called with args:", args)
}
//CommandOpt command option definition
type CommandOpt func(*Command)
//Command struct
type Command struct {
*cobra.Command
dir string
name string
parent *Command
childs map[string]*Command
}
//create a new command
func newCommand(opts ...CommandOpt) *Command {
cmd := &Command{
Command: &cobra.Command{
Run: _default,
},
parent: nil,
childs: make(map[string]*Command),
}
for _, opt := range opts {
opt(cmd)
}
return cmd
}
//build relations
func (c *Command) build() {
//replace rootCmd
if c.dir == "" {
if c.name != "" {
rootCmd.name = c.name
rootCmd.Command.Use = c.name
}
if c.Command.Short != "" {
rootCmd.Command.Short = c.Command.Short
}
if c.Command.Long != "" {
rootCmd.Command.Long = c.Command.Long
}
if len(rootCmd.childs) == 0 {
rootCmd.Command.Run = c.Command.Run
}
return
}
parent := rootCmd
if c.dir != "/" {
dirs := strings.Split(strings.TrimPrefix(c.dir, "/"), "/")
for _, d := range dirs {
if ci, ok := parent.childs[d]; ok {
parent = ci
} else {
nc := newCommand()
nc.setName(d)
nc.parent = parent
nc.dir = path.Join(nc.parent.dir, nc.parent.name)
parent.Command.Run = nil
parent.AddCommand(nc.Command)
parent.childs[d] = nc
parent = nc
}
}
}
c.parent = parent
c.parent.Command.Run = nil
c.parent.AddCommand(c.Command)
c.parent.childs[c.name] = c
}
func (c *Command) bind() error {
if err := viper.BindPFlags(c.Flags()); err != nil {
return err
}
if err := viper.BindPFlags(c.PersistentFlags()); err != nil {
return err
}
for _, child := range c.childs {
if err := child.bind(); err != nil {
return err
}
}
return nil
}
//Execute command
func (c *Command) Execute() error {
if err := c.bind(); err != nil {
return err
}
return c.Command.Execute()
}
//Path of command
func Path(p string) CommandOpt {
return func(cmd *Command) {
if len(p) > 0 {
if p == "/" {
cmd.dir = ""
cmd.setName(_program())
} else {
dir, name := filepath.Split(p)
cmd.dir = dir
if dir != "/" {
cmd.dir = strings.TrimSuffix(dir, "/")
}
cmd.setName(name)
}
}
}
}
func (cmd *Command) setName(name string) {
cmd.name = name
cmd.Command.Use = name
cmd.Command.Short = fmt.Sprintf("%s command", name)
}
//Parent of command
func Parent(parent string) CommandOpt {
return func(cmd *Command) {
cmd.dir = parent
}
}
//Name of command
func Name(name string) CommandOpt {
return func(cmd *Command) {
cmd.name = name
cmd.Command.Use = name
}
}
//Short Description of command
func Short(short string) CommandOpt {
return func(cmd *Command) {
cmd.Command.Short = short
}
}
//Description of command
func Description(desc string) CommandOpt {
return func(cmd *Command) {
cmd.Command.Long = desc
}
}
//MainFunc type
type MainFunc func(cmd *Command, args []string) error
//Main of command
func Main(main MainFunc) CommandOpt {
return func(cmd *Command) {
cmd.Command.Run = func(c *cobra.Command, args []string) {
exit(main(cmd, args))
}
}
}