-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (116 loc) · 2.93 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/discentem/plugexperiments/commons"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
)
// 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",
}
pluginMap = map[string]plugin.Plugin{
"greeter": &commons.GreeterPlugin{},
"greeterToo": &commons.GreeterPlugin{},
"shard": &commons.ShardPlugin{},
}
)
func main() {
plugs, err := plugin.Discover("*", "./plugin/greeter")
if err != nil {
log.Fatal(err)
}
for i := range plugs {
if strings.HasSuffix(plugs[i], ".go") {
continue
}
// Create an hclog.Logger
logger := hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Output: os.Stdout,
Level: hclog.Debug,
})
// We're a host! Start by launching the plugin process.
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
Cmd: exec.Command(plugs[i]),
Logger: logger,
})
// Connect via RPC
rpcClient, err := client.Client()
if err != nil {
log.Fatal(err)
}
// Request the plugin
for _, p := range []string{"greeter", "greeterToo"} {
raw, err := rpcClient.Dispense(p)
if err != nil {
log.Fatal(err)
}
// We should have a Greeter now! This feels like a normal interface
// implementation but is in fact over an RPC connection.
greeter := raw.(commons.Greeter)
s, err := greeter.Greet()
if err != nil {
continue
}
fmt.Println(s)
f, err := greeter.GreetFancy()
if err != nil {
log.Fatal(err)
}
fmt.Println(f)
}
}
plugs, err = plugin.Discover("*", "./plugin/shard")
if err != nil {
log.Fatal(err)
}
for i := range plugs {
if strings.HasSuffix(plugs[i], ".go") {
continue
}
// Create an hclog.Logger
logger := hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Output: os.Stdout,
Level: hclog.Debug,
})
// We're a host! Start by launching the plugin process.
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
Cmd: exec.Command(plugs[i]),
Logger: logger,
})
defer client.Kill()
// Connect via RPC
rpcClient, err := client.Client()
if err != nil {
log.Fatal(err)
}
raw, err := rpcClient.Dispense("shard")
if err != nil {
log.Fatal(err)
}
// We should have a Shard now! This feels like a normal interface
// implementation but is in fact over an RPC connection.
shard := raw.(commons.Shard)
s, err := shard.Get()
if err != nil {
log.Fatal(err)
}
fmt.Println(s)
}
}