github.com/hashicorp/go-plugin@v1.6.0/examples/negotiated/plugin-go/main.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package main 5 6 import ( 7 "fmt" 8 "io/ioutil" 9 10 "github.com/hashicorp/go-plugin" 11 "github.com/hashicorp/go-plugin/examples/grpc/shared" 12 ) 13 14 // Here is a real implementation of KV that uses grpc and writes to a local 15 // file with the key name and the contents are the value of the key. 16 type KVGRPC struct{} 17 18 func (KVGRPC) Put(key string, value []byte) error { 19 value = []byte(fmt.Sprintf("%s\n\nWritten from plugin version 3\n", string(value))) 20 return ioutil.WriteFile("kv_"+key, value, 0644) 21 } 22 23 func (KVGRPC) Get(key string) ([]byte, error) { 24 d, err := ioutil.ReadFile("kv_" + key) 25 if err != nil { 26 return nil, err 27 } 28 return append(d, []byte("Read by plugin version 3\n")...), nil 29 } 30 31 // Here is a real implementation of KV that writes to a local file with 32 // the key name and the contents are the value of the key. 33 type KV struct{} 34 35 func (KV) Put(key string, value []byte) error { 36 value = []byte(fmt.Sprintf("%s\n\nWritten from plugin version 2\n", string(value))) 37 return ioutil.WriteFile("kv_"+key, value, 0644) 38 } 39 40 func (KV) Get(key string) ([]byte, error) { 41 d, err := ioutil.ReadFile("kv_" + key) 42 if err != nil { 43 return nil, err 44 } 45 return append(d, []byte("Read by plugin version 2\n")...), nil 46 } 47 48 func main() { 49 plugin.Serve(&plugin.ServeConfig{ 50 HandshakeConfig: shared.Handshake, 51 VersionedPlugins: map[int]plugin.PluginSet{ 52 // Version 2 only uses NetRPC 53 2: { 54 "kv": &shared.KVPlugin{Impl: &KV{}}, 55 }, 56 // Version 3 only uses GRPC 57 3: { 58 "kv": &shared.KVGRPCPlugin{Impl: &KVGRPC{}}, 59 }, 60 }, 61 62 // A non-nil value here enables gRPC serving for this plugin... 63 GRPCServer: plugin.DefaultGRPCServer, 64 }) 65 }