-
Notifications
You must be signed in to change notification settings - Fork 4
/
app_plugin.go
235 lines (196 loc) · 5.52 KB
/
app_plugin.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
package fisk
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
type pluginDelegator struct {
command string
flags map[string]*string
cumuFlags map[string]*[]string
boolFlags map[string]*bool
unNegBoolFlags map[string]*bool
args map[string]*string
cumuArgs map[string]*[]string
proxyGlobals []string
globalFlags *flagGroup
}
func (a *Application) introspectModel() *ApplicationModel {
model := a.Model()
var nf []*FlagModel
for _, flag := range model.Flags {
if flag.Name == "help" || strings.HasPrefix(flag.Name, "help-") || strings.HasPrefix(flag.Name, "completion-") || strings.HasPrefix(flag.Name, "fisk-") {
continue
}
nf = append(nf, flag)
}
model.Flags = nf
var nc []*CmdModel
for _, cmd := range model.Commands {
if cmd.Name == "help" {
continue
}
nc = append(nc, cmd)
}
model.Commands = nc
return model
}
func (a *Application) introspectAction(_ *ParseContext) error {
a.Writer(os.Stdout)
j, err := json.Marshal(a.introspectModel())
if err != nil {
return err
}
fmt.Println(string(j))
a.terminate(0)
return nil
}
func (c *CmdClause) addArgsFromModel(model *ArgGroupModel) {
if model == nil {
return
}
for _, arg := range model.Args {
a := c.Arg(arg.Name, arg.Help)
a.placeholder = arg.PlaceHolder
a.required = arg.Required
a.hidden = arg.Hidden
a.defaultValues = arg.Default
a.envar = arg.Envar
switch {
case arg.Cumulative:
c.pluginDelegator.cumuArgs[arg.Name] = a.Strings()
default:
c.pluginDelegator.args[arg.Name] = a.String()
}
}
}
func (c *CmdClause) addFlagsFromModel(model *FlagGroupModel, appFlags *FlagGroupModel) {
if model == nil {
return
}
for _, flag := range model.Flags {
if _, ok := c.pluginDelegator.globalFlags.long[flag.Name]; ok {
c.pluginDelegator.proxyGlobals = append(c.pluginDelegator.proxyGlobals, flag.Name)
continue
}
f := c.Flag(flag.Name, flag.Help)
f.shorthand = flag.Short
f.defaultValues = flag.Default
f.envar = flag.Envar
f.placeholder = flag.PlaceHolder
f.required = flag.Required
f.hidden = flag.Hidden
switch {
case flag.Boolean && flag.Negatable:
c.pluginDelegator.boolFlags[flag.Name] = f.Bool()
case flag.Boolean:
c.pluginDelegator.unNegBoolFlags[flag.Name] = f.UnNegatableBool()
case flag.Cumulative:
c.pluginDelegator.cumuFlags[flag.Name] = f.Strings()
default:
c.pluginDelegator.flags[flag.Name] = f.String()
}
}
}
func (c *CmdClause) addCommandsFromModel(model *CmdGroupModel) {
if model == nil {
return
}
for _, cmd := range model.Commands {
cm := c.Command(cmd.Name, cmd.Help)
cm.pluginDelegator = c.pluginDelegator
cm.aliases = cmd.Aliases
cm.helpLong = cmd.HelpLong
cm.hidden = cmd.Hidden
cm.isDefault = cmd.Default
cm.addArgsFromModel(cmd.ArgGroupModel)
cm.addFlagsFromModel(cmd.FlagGroupModel, nil)
cm.addCommandsFromModel(cmd.CmdGroupModel)
cm.Action(func(pc *ParseContext) error {
parts := strings.Split(pc.SelectedCommand.FullCommand(), " ")
args := parts[1:]
for k, v := range cm.pluginDelegator.args {
if v != nil {
args = append(args, fmt.Sprintf("%s=%s", k, *v))
}
}
for k, v := range cm.pluginDelegator.flags {
if v != nil {
args = append(args, fmt.Sprintf("--%s=%s", k, *v))
}
}
for k, v := range cm.pluginDelegator.cumuFlags {
if v == nil {
continue
}
for _, cv := range *v {
args = append(args, fmt.Sprintf("--%s=%s", k, cv))
}
}
for k, v := range cm.pluginDelegator.boolFlags {
if *v {
args = append(args, fmt.Sprintf("--%s", k))
} else {
args = append(args, fmt.Sprintf("--no-%s", k))
}
}
for k, v := range cm.pluginDelegator.unNegBoolFlags {
if *v {
args = append(args, fmt.Sprintf("--%s", k))
}
}
for _, f := range cm.pluginDelegator.proxyGlobals {
args = append(args, fmt.Sprintf("--%s=%s", f, cm.pluginDelegator.globalFlags.long[f].value.String()))
}
// must be last
for _, v := range cm.pluginDelegator.cumuArgs {
if v != nil {
args = append(args, *v...)
}
}
if os.Getenv("FISK_DEBUG") != "" {
fmt.Printf("Fisk Plugin Running: %s %s\n", cm.pluginDelegator.command, strings.Join(args, " "))
}
cmd := exec.Command(cm.pluginDelegator.command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
return cmd.Run()
})
}
}
func (a *Application) registerPluginModel(command string, model *ApplicationModel) (*CmdClause, error) {
cmd := a.Command(model.Name, model.Help)
cmd.pluginDelegator = &pluginDelegator{
command: command,
flags: map[string]*string{},
cumuFlags: map[string]*[]string{},
args: map[string]*string{},
cumuArgs: map[string]*[]string{},
boolFlags: map[string]*bool{},
unNegBoolFlags: map[string]*bool{},
globalFlags: a.flagGroup,
}
cmd.addArgsFromModel(model.ArgGroupModel)
cmd.addFlagsFromModel(model.FlagGroupModel, a.Model().FlagGroupModel)
cmd.addCommandsFromModel(model.CmdGroupModel)
return cmd, nil
}
// ExternalPluginCommand extends the application using a plugin and a model describing the application
func (a *Application) ExternalPluginCommand(command string, model json.RawMessage) (*CmdClause, error) {
var m ApplicationModel
err := json.Unmarshal(model, &m)
if err != nil {
return nil, err
}
if m.Name == "" {
return nil, fmt.Errorf("plugin declared no name")
}
if m.Help == "" {
return nil, fmt.Errorf("plugin declared no help")
}
return a.registerPluginModel(command, &m)
}