OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / hashicorp / go-plugin / examples / grpc / main.go
1 package main
2
3 import (
4         "fmt"
5         "io/ioutil"
6         "log"
7         "os"
8         "os/exec"
9
10         "github.com/hashicorp/go-plugin"
11         "github.com/hashicorp/go-plugin/examples/grpc/shared"
12 )
13
14 func main() {
15         // We don't want to see the plugin logs.
16         log.SetOutput(ioutil.Discard)
17
18         // We're a host. Start by launching the plugin process.
19         client := plugin.NewClient(&plugin.ClientConfig{
20                 HandshakeConfig: shared.Handshake,
21                 Plugins:         shared.PluginMap,
22                 Cmd:             exec.Command("sh", "-c", os.Getenv("KV_PLUGIN")),
23                 AllowedProtocols: []plugin.Protocol{
24                         plugin.ProtocolNetRPC, plugin.ProtocolGRPC},
25         })
26         defer client.Kill()
27
28         // Connect via RPC
29         rpcClient, err := client.Client()
30         if err != nil {
31                 fmt.Println("Error:", err.Error())
32                 os.Exit(1)
33         }
34
35         // Request the plugin
36         raw, err := rpcClient.Dispense("kv")
37         if err != nil {
38                 fmt.Println("Error:", err.Error())
39                 os.Exit(1)
40         }
41
42         // We should have a KV store now! This feels like a normal interface
43         // implementation but is in fact over an RPC connection.
44         kv := raw.(shared.KV)
45         os.Args = os.Args[1:]
46         switch os.Args[0] {
47         case "get":
48                 result, err := kv.Get(os.Args[1])
49                 if err != nil {
50                         fmt.Println("Error:", err.Error())
51                         os.Exit(1)
52                 }
53
54                 fmt.Println(string(result))
55
56         case "put":
57                 err := kv.Put(os.Args[1], []byte(os.Args[2]))
58                 if err != nil {
59                         fmt.Println("Error:", err.Error())
60                         os.Exit(1)
61                 }
62
63         default:
64                 fmt.Println("Please only use 'get' or 'put'")
65                 os.Exit(1)
66         }
67 }