/
main.go
62 lines (52 loc) · 1.6 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
package main
import (
"fmt"
"io/ioutil"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/go-plugin/examples/grpc/shared"
)
// Here is a real implementation of KV that uses grpc and writes to a local
// file with the key name and the contents are the value of the key.
type KVGRPC struct{}
func (KVGRPC) Put(key string, value []byte) error {
value = []byte(fmt.Sprintf("%s\n\nWritten from plugin version 3\n", string(value)))
return ioutil.WriteFile("kv_"+key, value, 0644)
}
func (KVGRPC) Get(key string) ([]byte, error) {
d, err := ioutil.ReadFile("kv_" + key)
if err != nil {
return nil, err
}
return append(d, []byte("Read by plugin version 3\n")...), nil
}
// Here is a real implementation of KV that writes to a local file with
// the key name and the contents are the value of the key.
type KV struct{}
func (KV) Put(key string, value []byte) error {
value = []byte(fmt.Sprintf("%s\n\nWritten from plugin version 2\n", string(value)))
return ioutil.WriteFile("kv_"+key, value, 0644)
}
func (KV) Get(key string) ([]byte, error) {
d, err := ioutil.ReadFile("kv_" + key)
if err != nil {
return nil, err
}
return append(d, []byte("Read by plugin version 2\n")...), nil
}
func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: shared.Handshake,
VersionedPlugins: map[int]plugin.PluginSet{
// Version 2 only uses NetRPC
2: {
"kv": &shared.KVPlugin{Impl: &KV{}},
},
// Version 3 only uses GRPC
3: {
"kv": &shared.KVGRPCPlugin{Impl: &KVGRPC{}},
},
},
// A non-nil value here enables gRPC serving for this plugin...
GRPCServer: plugin.DefaultGRPCServer,
})
}