-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubcmd.go
188 lines (160 loc) · 4.07 KB
/
subcmd.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
/*
Package subcmd provides sub commander.
*/
package subcmd
import (
"bytes"
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
// Runner defines a base interface for Command and Set.
// Runner interface is defined for use only with DefineSet function.
type Runner interface {
// Name returns name of runner.
Name() string
// Desc returns description of runner.
Desc() string
// Run runs runner with context and arguments.
Run(ctx context.Context, args []string) error
}
// CommandFunc is handler of sub-command, and an entry point.
type CommandFunc func(ctx context.Context, args []string) error
// Command repreesents a sub-command, and implements Runner interface.
type Command struct {
name string
desc string
runFn CommandFunc
}
var _ Runner = Command{}
// DefineCommand defines a Command with name, desc, and function.
func DefineCommand(name, desc string, fn CommandFunc) Command {
return Command{
name: name,
desc: desc,
runFn: fn,
}
}
// Name returns name of the command.
func (c Command) Name() string {
return c.name
}
// Desc returns description of the command.
func (c Command) Desc() string {
return c.desc
}
// Run executes sub-command. It will invoke CommandFunc which passed to DefineCommand.
func (c Command) Run(ctx context.Context, args []string) error {
ctx = withName(ctx, c)
if c.runFn == nil {
names := strings.Join(Names(ctx), " ")
return fmt.Errorf("no function declared for command: %s", names)
}
return c.runFn(ctx, args)
}
// Set provides set of Commands or nested Sets.
type Set struct {
name string
desc string
Runners []Runner
}
var _ Runner = Set{}
// DefineSet defines a set of Runners with name, and desc.
func DefineSet(name, desc string, runners ...Runner) Set {
return Set{
name: name,
desc: desc,
Runners: runners,
}
}
// DefineRootSet defines a set of Runners which used as root of Set (maybe
// passed to Run).
func DefineRootSet(runners ...Runner) Set {
return Set{name: rootName(), Runners: runners}
}
// Name returns name of Set.
func (s Set) Name() string {
return s.name
}
// Desc returns description of Set.
func (s Set) Desc() string {
return s.desc
}
// childRunner retrieves a child Runner with name
func (s Set) childRunner(name string) Runner {
for _, r := range s.Runners {
if r.Name() == name {
return r
}
}
return nil
}
type errorSetRun struct {
src Set
msg string
}
func (err *errorSetRun) Error() string {
// align width of name columns
w := 12
for _, r := range err.src.Runners {
if n := len(r.Name()) + 1; n > w {
w = (n + 3) / 4 * 4
}
}
// format error message
bb := &bytes.Buffer{}
fmt.Fprintf(bb, "%s.\n\nAvailable sub-commands are:\n", err.msg)
for _, r := range err.src.Runners {
fmt.Fprintf(bb, "\n\t%-*s%s", w, r.Name(), r.Desc())
}
return bb.String()
}
func (s Set) Run(ctx context.Context, args []string) error {
if len(args) == 0 {
return &errorSetRun{src: s, msg: "no commands selected"}
}
name := args[0]
child := s.childRunner(name)
if child == nil {
return &errorSetRun{src: s, msg: "command not found"}
}
return child.Run(withName(ctx, s), args[1:])
}
// Run runs a Runner with ctx and args.
func Run(r Runner, args ...string) error {
return r.Run(context.Background(), args)
}
type keyNames struct{}
// Names retrives names layer of current sub command.
func Names(ctx context.Context) []string {
if names, ok := ctx.Value(keyNames{}).([]string); ok {
return names
}
return nil
}
func withName(ctx context.Context, r Runner) context.Context {
return context.WithValue(ctx, keyNames{}, append(Names(ctx), r.Name()))
}
func stripExeExt(in string) string {
_, out := filepath.Split(in)
ext := filepath.Ext(out)
if ext == ".exe" {
return out[:len(out)-len(ext)]
}
return out
}
func rootName() string {
exe, err := os.Executable()
if err != nil {
panic(fmt.Sprintf("failed to obtain executable name: %s", err))
}
return stripExeExt(exe)
}
// FlagSet creates a new flag.FlagSet with name of subcommand.
func FlagSet(ctx context.Context) *flag.FlagSet {
name := strings.Join(Names(ctx), " ")
return flag.NewFlagSet(name, flag.ExitOnError)
}