OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / hashicorp / go-plugin / examples / basic / plugin / greeter_impl.go
1 package main
2
3 import (
4         "os"
5
6         "github.com/hashicorp/go-hclog"
7         "github.com/hashicorp/go-plugin"
8         "github.com/hashicorp/go-plugin/examples/basic/commons"
9 )
10
11 // Here is a real implementation of Greeter
12 type GreeterHello struct {
13         logger hclog.Logger
14 }
15
16 func (g *GreeterHello) Greet() string {
17         g.logger.Debug("message from GreeterHello.Greet")
18         return "Hello!"
19 }
20
21 // handshakeConfigs are used to just do a basic handshake between
22 // a plugin and host. If the handshake fails, a user friendly error is shown.
23 // This prevents users from executing bad plugins or executing a plugin
24 // directory. It is a UX feature, not a security feature.
25 var handshakeConfig = plugin.HandshakeConfig{
26         ProtocolVersion:  1,
27         MagicCookieKey:   "BASIC_PLUGIN",
28         MagicCookieValue: "hello",
29 }
30
31 func main() {
32         logger := hclog.New(&hclog.LoggerOptions{
33                 Level:      hclog.Trace,
34                 Output:     os.Stderr,
35                 JSONFormat: true,
36         })
37
38         greeter := &GreeterHello{
39                 logger: logger,
40         }
41         // pluginMap is the map of plugins we can dispense.
42         var pluginMap = map[string]plugin.Plugin{
43                 "greeter": &example.GreeterPlugin{Impl: greeter},
44         }
45
46         logger.Debug("message from plugin", "foo", "bar")
47
48         plugin.Serve(&plugin.ServeConfig{
49                 HandshakeConfig: handshakeConfig,
50                 Plugins:         pluginMap,
51         })
52 }