char/pkg/register/plugin_builder.go

49 lines
826 B
Go
Raw Normal View History

2022-11-01 14:26:54 +01:00
package register
import (
"context"
"os"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
)
type PluginBuilder struct {
serveConfig *plugin.ServeConfig
}
2022-11-02 20:25:55 +01:00
func NewPluginBuilder(p Plugin) *PluginBuilder {
2022-11-01 14:26:54 +01:00
logger := hclog.New(&hclog.LoggerOptions{
2022-11-05 23:27:47 +01:00
Level: hclog.Debug,
2022-11-01 14:26:54 +01:00
Output: os.Stderr,
JSONFormat: false,
})
var pluginMap = map[string]plugin.Plugin{
2022-11-02 20:30:44 +01:00
PluginKey: &PluginAPI{
2022-11-01 14:26:54 +01:00
Impl: p,
},
}
serveConfig := &plugin.ServeConfig{
HandshakeConfig: plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "char",
},
Plugins: pluginMap,
Logger: logger,
}
return &PluginBuilder{
serveConfig: serveConfig,
}
}
func (pr *PluginBuilder) Serve(ctx context.Context) error {
plugin.Serve(
pr.serveConfig,
)
return nil
}