-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathpluginFlag.go
66 lines (55 loc) · 1.44 KB
/
pluginFlag.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
package subcmds
import (
"fmt"
"os/exec"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/yoheimuta/protolint/internal/addon/plugin/shared"
"github.com/hashicorp/go-plugin"
)
// PluginFlag manages a flag for plugins.
type PluginFlag struct {
raws []string
}
// String implements flag.Value.
func (f *PluginFlag) String() string {
return fmt.Sprint(strings.Join(f.raws, ","))
}
// Set implements flag.Value.
func (f *PluginFlag) Set(value string) error {
f.raws = append(f.raws, value)
return nil
}
// BuildPlugins builds all plugins.
func (f *PluginFlag) BuildPlugins(verbose bool) ([]shared.RuleSet, error) {
var plugins []shared.RuleSet
for _, value := range f.raws {
level := hclog.Warn
if verbose {
level = hclog.Trace
}
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: shared.Handshake,
Plugins: shared.PluginMap,
Cmd: exec.Command("sh", "-c", value),
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolGRPC,
},
Logger: hclog.New(&hclog.LoggerOptions{
Output: hclog.DefaultOutput,
Level: level,
Name: "plugin",
}),
})
rpcClient, err := client.Client()
if err != nil {
return nil, fmt.Errorf("failed client.Client(), err=%s", err)
}
ruleSet, err := rpcClient.Dispense("ruleSet")
if err != nil {
return nil, fmt.Errorf("failed Dispense, err=%s", err)
}
plugins = append(plugins, ruleSet.(shared.RuleSet))
}
return plugins, nil
}