-
Notifications
You must be signed in to change notification settings - Fork 0
/
goat.go
200 lines (166 loc) · 4.32 KB
/
goat.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
package goat
import (
"github.com/urfave/cli/v2"
"log"
"os"
"reflect"
)
type RunConfig struct {
Flags []cli.Flag
// TODO: Replace this with a function that takes the action function and returns ActionFunc
// This is needed for supporting function literals instead of named functions.
// This is also required for anything beyond named functions.
Action cli.ActionFunc
CtxFlagBuilder func(c *cli.Context) map[string]any
Name string
Usage string
}
var runConfigByFunction map[reflect.Value]RunConfig
var functionByCliActionFunc map[reflect.Value]reflect.Value
func init() {
runConfigByFunction = make(map[reflect.Value]RunConfig)
functionByCliActionFunc = make(map[reflect.Value]reflect.Value)
}
// Register registers a RunConfig generated from a function.
//
// This is only used in generated code.
func Register(app any, config RunConfig) {
appValue := reflect.ValueOf(app)
runConfigByFunction[appValue] = config
functionByCliActionFunc[reflect.ValueOf(config.Action)] = appValue
}
func RunWithArgsE(f any, args []string) error {
config := runConfigByFunction[reflect.ValueOf(f)]
app := &cli.App{
Flags: config.Flags,
Action: config.Action,
Name: config.Name,
Usage: config.Usage,
}
return app.Run(args)
}
func FuncToApp(f any) *cli.App {
config := runConfigByFunction[reflect.ValueOf(f)]
app := &cli.App{
Flags: config.Flags,
Action: config.Action,
Name: config.Name,
Usage: config.Usage,
}
return app
}
// RunE takes a free function and runs it as a CLI app.
func RunE(f any) error {
return RunWithArgsE(f, os.Args)
}
// Run takes a free function and runs it as a CLI app, terminating with a log if an error occurs.
func Run(f any) {
err := RunE(f)
if err != nil {
log.Fatal(err)
}
}
type AppPart interface {
cliCommand() *cli.Command
}
type GoatCommand struct {
command *cli.Command
}
func (g *GoatCommand) cliCommand() *cli.Command {
return g.command
}
type GoatGroup struct {
command *cli.Command
}
func (g *GoatGroup) cliCommand() *cli.Command {
return g.command
}
func (g *GoatGroup) Usage(usage string) *GoatGroup {
g.command.Usage = usage
return g
}
func PartsToCommands(parts []AppPart) []*cli.Command {
commands := make([]*cli.Command, len(parts))
for i, part := range parts {
commands[i] = part.cliCommand()
}
return commands
}
func Command(f any, subcommands ...AppPart) *GoatCommand {
config := runConfigByFunction[reflect.ValueOf(f)]
return &GoatCommand{&cli.Command{
Flags: config.Flags,
Action: config.Action,
Name: config.Name,
Usage: config.Usage,
Subcommands: PartsToCommands(subcommands),
}}
}
func Group(name string, subcommands ...AppPart) *GoatGroup {
return &GoatGroup{&cli.Command{
Name: name,
Subcommands: PartsToCommands(subcommands),
}}
}
type Application struct{ *cli.App }
func (app Application) RunWithArgsE(args []string) error {
return app.App.Run(args)
}
func (app Application) RunE() error {
return app.App.Run(os.Args)
}
func (app Application) Run() {
err := app.App.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func App(name string, commands ...AppPart) Application {
return Application{
App: &cli.App{
Name: name,
Commands: PartsToCommands(commands),
},
}
}
// Flag creates a flag-descriptor to be used during code-generation to describe the flag.
//
// Should be used with the FluentFlag.Name, FluentFlag.Usage and FluentFlag.Default methods.
//
// Example:
// func f(myFlag int) {
// Flag(myFlag).
// Name("my-flag").
// Usage("Just a flag.")
// }
func Flag(any) FluentFlag {
return FluentFlag{}
}
type FluentFlag struct{}
// Name sets the name of a flag.
func (f FluentFlag) Name(string) FluentFlag {
return FluentFlag{}
}
// Usage sets the usage of a flag.
func (f FluentFlag) Usage(string) FluentFlag {
return FluentFlag{}
}
// Default sets the default value for a flag.
//
// Must be called with the same type as the flag.
func (f FluentFlag) Default(any) FluentFlag {
return FluentFlag{}
}
type FluentSelf struct{}
// Self begins a description-chain for the current function.
func Self() FluentSelf {
return FluentSelf{}
}
// Name sets the name of the current function.
func (s FluentSelf) Name(string) FluentSelf {
return FluentSelf{}
}
// Usage sets the usage of the current function.
func (s FluentSelf) Usage(string) FluentSelf {
return FluentSelf{}
}