char/pkg/register/plugin_builder.go

49 lines
834 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
}
func NewPluginBuilder(name string, p Plugin) *PluginBuilder {
logger := hclog.New(&hclog.LoggerOptions{
2022-11-01 21:15:32 +01:00
Level: hclog.Error,
2022-11-01 14:26:54 +01:00
Output: os.Stderr,
JSONFormat: false,
})
var pluginMap = map[string]plugin.Plugin{
name: &PluginAPI{
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
}