OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / hashicorp / go-plugin / examples / basic / main.go
1 package main
2
3 import (
4         "fmt"
5         "log"
6         "os"
7         "os/exec"
8
9         hclog "github.com/hashicorp/go-hclog"
10         "github.com/hashicorp/go-plugin"
11         "github.com/hashicorp/go-plugin/examples/basic/commons"
12 )
13
14 func main() {
15         // Create an hclog.Logger
16         logger := hclog.New(&hclog.LoggerOptions{
17                 Name:   "plugin",
18                 Output: os.Stdout,
19                 Level:  hclog.Debug,
20         })
21
22         // We're a host! Start by launching the plugin process.
23         client := plugin.NewClient(&plugin.ClientConfig{
24                 HandshakeConfig: handshakeConfig,
25                 Plugins:         pluginMap,
26                 Cmd:             exec.Command("./plugin/greeter"),
27                 Logger:          logger,
28         })
29         defer client.Kill()
30
31         // Connect via RPC
32         rpcClient, err := client.Client()
33         if err != nil {
34                 log.Fatal(err)
35         }
36
37         // Request the plugin
38         raw, err := rpcClient.Dispense("greeter")
39         if err != nil {
40                 log.Fatal(err)
41         }
42
43         // We should have a Greeter now! This feels like a normal interface
44         // implementation but is in fact over an RPC connection.
45         greeter := raw.(example.Greeter)
46         fmt.Println(greeter.Greet())
47 }
48
49 // handshakeConfigs are used to just do a basic handshake between
50 // a plugin and host. If the handshake fails, a user friendly error is shown.
51 // This prevents users from executing bad plugins or executing a plugin
52 // directory. It is a UX feature, not a security feature.
53 var handshakeConfig = plugin.HandshakeConfig{
54         ProtocolVersion:  1,
55         MagicCookieKey:   "BASIC_PLUGIN",
56         MagicCookieValue: "hello",
57 }
58
59 // pluginMap is the map of plugins we can dispense.
60 var pluginMap = map[string]plugin.Plugin{
61         "greeter": &example.GreeterPlugin{},
62 }