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