-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathplugin.go
130 lines (104 loc) · 2.43 KB
/
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
package commands
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bryanl/doit/pluginhost"
"github.com/spf13/cobra"
)
// Plugin creates the plugin commands heirarchy.
func Plugin() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "plugin",
Short: "plugin commands",
Long: "plugin is used to access plugin commands",
Aliases: []string{"p"},
},
}
CmdBuilder(cmd, RunPluginList, "list", "list plugins", Writer,
aliasOpt("ls"))
CmdBuilder(cmd, RunPluginRun, "run", "run plugin", Writer)
return cmd
}
// RunPluginRun is a command for running a plugin.
func RunPluginRun(c *CmdConfig) error {
if len(c.Args) == 0 {
return fmt.Errorf("missing plugin name")
}
plugs, err := searchPlugins()
if err != nil {
return err
}
var selectedPlugin *plugDesc
for i, p := range plugs {
if p.Name == c.Args[0] {
selectedPlugin = &plugs[i]
}
}
if selectedPlugin == nil {
return fmt.Errorf("unknown plugin %q", c.Args[0])
}
var pluginArgs []string
if len(c.Args) > 1 {
pluginArgs = c.Args[1:]
}
host, err := pluginhost.NewHost(selectedPlugin.Path)
if err != nil {
return err
}
var method string
var methodArgs []string
switch l := len(pluginArgs); {
case l == 0:
method = "Default"
case l == 1:
method = pluginArgs[0]
default:
method = pluginArgs[0]
methodArgs = pluginArgs[1:]
}
if len(pluginArgs) > 1 {
methodArgs = pluginArgs[1:]
}
results, err := host.Call(selectedPlugin.Name+"."+strings.Title(method), methodArgs...)
if err != nil {
return err
}
fmt.Fprintln(c.Out, results)
return nil
}
// RunPluginList is a command for listing available plugins.
func RunPluginList(c *CmdConfig) error {
plugs, err := searchPlugins()
if err != nil {
return err
}
item := &plugin{plugins: plugs}
return c.Display(item)
}
type plugDesc struct {
Path string `json:"path"`
Name string `json:"name"`
}
func searchPlugins() ([]plugDesc, error) {
envPath := os.Getenv("PATH")
paths := strings.Split(envPath, string(os.PathListSeparator))
var plugs []plugDesc
for _, p := range paths {
matches, err := filepath.Glob(filepath.Join(p, "doit-provider-*"))
if err != nil {
return nil, err
}
for _, pluginPath := range matches {
name := pluginName(pluginPath)
plugs = append(plugs, plugDesc{Path: pluginPath, Name: name})
}
}
return plugs, nil
}
func pluginName(p string) string {
base := filepath.Base(p)
return strings.TrimPrefix(base, "doit-provider-")
}