11 "github.com/hashicorp/go-plugin"
12 "github.com/hashicorp/go-plugin/examples/bidirectional/shared"
15 type addHelper struct{}
17 func (*addHelper) Sum(a, b int64) (int64, error) {
22 // We don't want to see the plugin logs.
23 log.SetOutput(ioutil.Discard)
25 // We're a host. Start by launching the plugin process.
26 client := plugin.NewClient(&plugin.ClientConfig{
27 HandshakeConfig: shared.Handshake,
28 Plugins: shared.PluginMap,
29 Cmd: exec.Command("sh", "-c", os.Getenv("COUNTER_PLUGIN")),
30 AllowedProtocols: []plugin.Protocol{
31 plugin.ProtocolNetRPC, plugin.ProtocolGRPC},
36 rpcClient, err := client.Client()
38 fmt.Println("Error:", err.Error())
43 raw, err := rpcClient.Dispense("counter")
45 fmt.Println("Error:", err.Error())
49 // We should have a Counter store now! This feels like a normal interface
50 // implementation but is in fact over an RPC connection.
51 counter := raw.(shared.Counter)
56 result, err := counter.Get(os.Args[1])
58 fmt.Println("Error:", err.Error())
65 i, err := strconv.Atoi(os.Args[2])
67 fmt.Println("Error:", err.Error())
71 err = counter.Put(os.Args[1], int64(i), &addHelper{})
73 fmt.Println("Error:", err.Error())
78 fmt.Println("Please only use 'get' or 'put'")