-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
469 lines (421 loc) · 14.5 KB
/
interface.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package plugin
import (
"bytes"
"encoding/gob"
"fmt"
"net/rpc"
"os"
"strconv"
"strings"
"github.com/hashicorp/go-plugin"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func init() {
gob.Register(Manifest{})
gob.Register(ExecutedCommand{})
gob.Register(ExecutedHook{})
}
// An spellshape plugin must implements the Plugin interface.
//
//go:generate mockery --srcpkg . --name Interface --structname PluginInterface --filename interface.go --with-expecter
type Interface interface {
// Manifest declares the plugin's Command(s) and Hook(s).
Manifest() (Manifest, error)
// Execute will be invoked by spellshape when a plugin Command is executed.
// It is global for all commands declared in Manifest, if you have declared
// multiple commands, use cmd.Path to distinguish them.
Execute(cmd ExecutedCommand) error
// ExecuteHookPre is invoked by spellshape when a command specified by the Hook
// path is invoked.
// It is global for all hooks declared in Manifest, if you have declared
// multiple hooks, use hook.Name to distinguish them.
ExecuteHookPre(hook ExecutedHook) error
// ExecuteHookPost is invoked by spellshape when a command specified by the hook
// path is invoked.
// It is global for all hooks declared in Manifest, if you have declared
// multiple hooks, use hook.Name to distinguish them.
ExecuteHookPost(hook ExecutedHook) error
// ExecuteHookCleanUp is invoked by spellshape when a command specified by the
// hook path is invoked. Unlike ExecuteHookPost, it is invoked regardless of
// execution status of the command and hooks.
// It is global for all hooks declared in Manifest, if you have declared
// multiple hooks, use hook.Name to distinguish them.
ExecuteHookCleanUp(hook ExecutedHook) error
}
// Manifest represents the plugin behavior.
type Manifest struct {
Name string
// Commands contains the commands that will be added to the list of spellshape
// commands. Each commands are independent, for nested commands use the
// inner Commands field.
Commands []Command
// Hooks contains the hooks that will be attached to the existing spellshape
// commands.
Hooks []Hook
// SharedHost enables sharing a single plugin server across all running instances
// of a plugin. Useful if a plugin adds or extends long running commands
//
// Example: if a plugin defines a hook on `spellshape chain serve`, a plugin server is instanciated
// when the command is run. Now if you want to interact with that instance from commands
// defined in that plugin, you need to enable `SharedHost`, or else the commands will just
// instantiate separate plugin servers.
//
// When enabled, all plugins of the same `Path` loaded from the same configuration will
// attach it's rpc client to a an existing rpc server.
//
// If a plugin instance has no other running plugin servers, it will create one and it will be the host.
SharedHost bool `yaml:"shared_host"`
}
// ImportCobraCommand allows to hydrate m with a standard root cobra commands.
func (m *Manifest) ImportCobraCommand(c *cobra.Command, placeCommandUnder string) {
m.Commands = append(m.Commands, convertCobraCommand(c, placeCommandUnder))
}
func convertCobraCommand(c *cobra.Command, placeCommandUnder string) Command {
cmd := Command{
Use: c.Use,
Aliases: c.Aliases,
Short: c.Short,
Long: c.Long,
Hidden: c.Hidden,
PlaceCommandUnder: placeCommandUnder,
Flags: convertPFlags(c),
}
for _, c := range c.Commands() {
cmd.Commands = append(cmd.Commands, convertCobraCommand(c, ""))
}
return cmd
}
// Command represents a plugin command.
type Command struct {
// Same as cobra.Command.Use
Use string
// Same as cobra.Command.Aliases
Aliases []string
// Same as cobra.Command.Short
Short string
// Same as cobra.Command.Long
Long string
// Same as cobra.Command.Hidden
Hidden bool
// Flags holds the list of command flags
Flags []Flag
// PlaceCommandUnder indicates where the command should be placed.
// For instance `spellshape scaffold` will place the command at the
// `scaffold` command.
// An empty value is interpreted as `spellshape` (==root).
PlaceCommandUnder string
// List of sub commands
Commands []Command
}
// PlaceCommandUnderFull returns a normalized p.PlaceCommandUnder, by adding
// the `spellshape ` prefix if not present.
func (c Command) PlaceCommandUnderFull() string {
return commandFull(c.PlaceCommandUnder)
}
func commandFull(cmdPath string) string {
const rootCmdName = "spellshape"
if !strings.HasPrefix(cmdPath, rootCmdName) {
cmdPath = rootCmdName + " " + cmdPath
}
return strings.TrimSpace(cmdPath)
}
// ToCobraCommand turns Command into a cobra.Command so it can be added to a
// parent command.
func (c Command) ToCobraCommand() (*cobra.Command, error) {
cmd := &cobra.Command{
Use: c.Use,
Aliases: c.Aliases,
Short: c.Short,
Long: c.Long,
Hidden: c.Hidden,
}
for _, f := range c.Flags {
err := f.feedFlagSet(cmd)
if err != nil {
return nil, err
}
}
return cmd, nil
}
// Hook represents a user defined action within a plugin.
type Hook struct {
// Name identifies the hook for the client to invoke the correct hook
// must be unique
Name string
// PlaceHookOn indicates the command to register the hooks for
PlaceHookOn string
}
// PlaceHookOnFull returns a normalized p.PlaceCommandUnder, by adding the
// `spellshape ` prefix if not present.
func (h Hook) PlaceHookOnFull() string {
return commandFull(h.PlaceHookOn)
}
// ExecutedCommand represents a plugin command under execution.
type ExecutedCommand struct {
// Use is copied from Command.Use
Use string
// Path contains the command path, e.g. `spellshape scaffold foo`
Path string
// Args are the command arguments
Args []string
// Full list of args taken from os.Args
OSArgs []string
// With contains the plugin config parameters
With map[string]string
flags *pflag.FlagSet
pflags *pflag.FlagSet
}
// ExecutedHook represents a plugin hook under execution.
type ExecutedHook struct {
// ExecutedCommand gives access to the command attached by the hook.
ExecutedCommand ExecutedCommand
// Hook is a copy of the original Hook defined in the Manifest.
Hook
}
// Flags gives access to the commands' flags, like cobra.Command.Flags.
func (c *ExecutedCommand) Flags() *pflag.FlagSet {
if c.flags == nil {
c.flags = pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
}
return c.flags
}
// PersistentFlags gives access to the commands' persistent flags, like
// cobra.Command.PersistentFlags.
func (c *ExecutedCommand) PersistentFlags() *pflag.FlagSet {
if c.pflags == nil {
c.pflags = pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
}
return c.pflags
}
// SetFlags set the flags.
// As a plugin developer, you probably don't need to use it.
func (c *ExecutedCommand) SetFlags(cmd *cobra.Command) {
c.flags = cmd.Flags()
c.pflags = cmd.PersistentFlags()
}
// Flag is a serializable representation of pflag.Flag.
type Flag struct {
Name string // name as it appears on command line
Shorthand string // one-letter abbreviated flag
Usage string // help message
DefValue string // default value (as text); for usage message
Type FlagType
Value string
// Persistent indicates wether or not the flag is propagated on children
// commands
Persistent bool
}
// FlagType represents the pflag.Flag.Value.Type().
type FlagType string
const (
// NOTE(tb): we declare only the main used cobra flag types for simplicity
// If a plugin receives an unhandled type, it will output an error.
FlagTypeString FlagType = "string"
FlagTypeInt FlagType = "int"
FlagTypeUint FlagType = "uint"
FlagTypeInt64 FlagType = "int64"
FlagTypeUint64 FlagType = "uint64"
FlagTypeBool FlagType = "bool"
FlagTypeStringSlice FlagType = "stringSlice"
)
// feedFlagSet fills flagger with f.
func (f Flag) feedFlagSet(fgr flagger) error {
fs := fgr.Flags()
if f.Persistent {
fs = fgr.PersistentFlags()
}
switch f.Type {
case FlagTypeBool:
defVal, _ := strconv.ParseBool(f.DefValue)
fs.BoolP(f.Name, f.Shorthand, defVal, f.Usage)
fs.Set(f.Name, f.Value)
case FlagTypeInt:
defVal, _ := strconv.Atoi(f.DefValue)
fs.IntP(f.Name, f.Shorthand, defVal, f.Usage)
fs.Set(f.Name, f.Value)
case FlagTypeUint:
defVal, _ := strconv.ParseUint(f.DefValue, 10, 64)
fs.UintP(f.Name, f.Shorthand, uint(defVal), f.Usage)
fs.Set(f.Name, f.Value)
case FlagTypeInt64:
defVal, _ := strconv.ParseInt(f.DefValue, 10, 64)
fs.Int64P(f.Name, f.Shorthand, defVal, f.Usage)
fs.Set(f.Name, f.Value)
case FlagTypeUint64:
defVal, _ := strconv.ParseUint(f.DefValue, 10, 64)
fs.Uint64P(f.Name, f.Shorthand, defVal, f.Usage)
fs.Set(f.Name, f.Value)
case FlagTypeString:
fs.StringP(f.Name, f.Shorthand, f.DefValue, f.Usage)
fs.Set(f.Name, f.Value)
case FlagTypeStringSlice:
s := strings.Trim(f.DefValue, "[]")
defValue := strings.Fields(s)
fs.StringSliceP(f.Name, f.Shorthand, defValue, f.Usage)
fs.Set(f.Name, strings.Trim(f.Value, "[]"))
default:
return fmt.Errorf("flagset unmarshal: unhandled flag type %q in flag %#v", f.Type, f)
}
return nil
}
// gobCommandFlags is used to gob encode/decode Command.
// Command can't be encoded because :
// - flags is unexported (because we want to expose it via the Flags() method,
// like a regular cobra.Command)
// - flags type is *pflag.FlagSet which is also full of unexported fields.
type gobCommandContextFlags struct {
CommandContext gobCommandContext
Flags []Flag
}
// gobCommandContext is the same as ExecutedCommand but without GobDecode
// attached, which avoids infinite loops.
type gobCommandContext ExecutedCommand
// GobEncode implements gob.Encoder.
// It actually encodes a gobCommandContext struct built from c.
func (c ExecutedCommand) GobEncode() ([]byte, error) {
var b bytes.Buffer
err := gob.NewEncoder(&b).Encode(gobCommandContextFlags{
CommandContext: gobCommandContext(c),
Flags: convertPFlags(&c),
})
return b.Bytes(), err
}
// flagger matches both cobra.Command and Command.
type flagger interface {
Flags() *pflag.FlagSet
PersistentFlags() *pflag.FlagSet
}
func convertPFlags(fgr flagger) []Flag {
var ff []Flag
if fgr.Flags() != nil {
fgr.Flags().VisitAll(func(pf *pflag.Flag) {
ff = append(ff, Flag{
Name: pf.Name,
Shorthand: pf.Shorthand,
Usage: pf.Usage,
DefValue: pf.DefValue,
Value: pf.Value.String(),
Type: FlagType(pf.Value.Type()),
})
})
}
if fgr.PersistentFlags() != nil {
fgr.PersistentFlags().VisitAll(func(pf *pflag.Flag) {
ff = append(ff, Flag{
Name: pf.Name,
Shorthand: pf.Shorthand,
Usage: pf.Usage,
DefValue: pf.DefValue,
Value: pf.Value.String(),
Type: FlagType(pf.Value.Type()),
Persistent: true,
})
})
}
return ff
}
// GobDecode implements gob.Decoder.
// It actually decodes a gobCommandContext struct and fills c with it.
func (c *ExecutedCommand) GobDecode(bz []byte) error {
var gb gobCommandContextFlags
err := gob.NewDecoder(bytes.NewReader(bz)).Decode(&gb)
if err != nil {
return err
}
*c = ExecutedCommand(gb.CommandContext)
for _, f := range gb.Flags {
err := f.feedFlagSet(c)
if err != nil {
return err
}
}
return nil
}
// handshakeConfigs are used to just do a basic handshake between
// a plugin and host. If the handshake fails, a user friendly error is shown.
// This prevents users from executing bad plugins or executing a plugin
// directory. It is a UX feature, not a security feature.
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
func HandshakeConfig() plugin.HandshakeConfig {
return handshakeConfig
}
// InterfaceRPC is an implementation that talks over RPC.
type InterfaceRPC struct{ client *rpc.Client }
// Manifest implements Interface.Manifest.
func (g *InterfaceRPC) Manifest() (Manifest, error) {
var resp Manifest
return resp, g.client.Call("Plugin.Manifest", new(interface{}), &resp)
}
// Execute implements Interface.Commands.
func (g *InterfaceRPC) Execute(c ExecutedCommand) error {
var resp interface{}
return g.client.Call("Plugin.Execute", map[string]interface{}{
"executedCommand": c,
}, &resp)
}
func (g *InterfaceRPC) ExecuteHookPre(hook ExecutedHook) error {
var resp interface{}
return g.client.Call("Plugin.ExecuteHookPre", map[string]interface{}{
"executedHook": hook,
}, &resp)
}
func (g *InterfaceRPC) ExecuteHookPost(hook ExecutedHook) error {
var resp interface{}
return g.client.Call("Plugin.ExecuteHookPost", map[string]interface{}{
"executedHook": hook,
}, &resp)
}
func (g *InterfaceRPC) ExecuteHookCleanUp(hook ExecutedHook) error {
var resp interface{}
return g.client.Call("Plugin.ExecuteHookCleanUp", map[string]interface{}{
"executedHook": hook,
}, &resp)
}
// InterfaceRPCServer is the RPC server that InterfaceRPC talks to, conforming to
// the requirements of net/rpc.
type InterfaceRPCServer struct {
// This is the real implementation
Impl Interface
}
func (s *InterfaceRPCServer) Manifest(args interface{}, resp *Manifest) error {
var err error
*resp, err = s.Impl.Manifest()
return err
}
func (s *InterfaceRPCServer) Execute(args map[string]interface{}, resp *interface{}) error {
return s.Impl.Execute(args["executedCommand"].(ExecutedCommand))
}
func (s *InterfaceRPCServer) ExecuteHookPre(args map[string]interface{}, resp *interface{}) error {
return s.Impl.ExecuteHookPre(args["executedHook"].(ExecutedHook))
}
func (s *InterfaceRPCServer) ExecuteHookPost(args map[string]interface{}, resp *interface{}) error {
return s.Impl.ExecuteHookPost(args["executedHook"].(ExecutedHook))
}
func (s *InterfaceRPCServer) ExecuteHookCleanUp(args map[string]interface{}, resp *interface{}) error {
return s.Impl.ExecuteHookCleanUp(args["executedHook"].(ExecutedHook))
}
// This is the implementation of plugin.Interface so we can serve/consume this
//
// This has two methods: Server must return an RPC server for this plugin
// type. We construct a InterfaceRPCServer for this.
//
// Client must return an implementation of our interface that communicates
// over an RPC client. We return InterfaceRPC for this.
//
// Ignore MuxBroker. That is used to create more multiplexed streams on our
// plugin connection and is a more advanced use case.
type InterfacePlugin struct {
// Impl Injection
Impl Interface
}
func (p *InterfacePlugin) Server(*plugin.MuxBroker) (interface{}, error) {
return &InterfaceRPCServer{Impl: p.Impl}, nil
}
func (InterfacePlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &InterfaceRPC{client: c}, nil
}