char/pkg/register/plugin_register.go

196 lines
3.5 KiB
Go
Raw Normal View History

2022-11-01 14:26:54 +01:00
package register
import (
"context"
"errors"
"fmt"
2022-11-01 21:15:32 +01:00
"log"
2022-11-01 14:26:54 +01:00
"os"
"os/exec"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"golang.org/x/sync/errgroup"
)
type PluginRegisterBuilder struct {
plugins map[string]PluginAPI
}
func NewPluginRegisterBuilder() *PluginRegisterBuilder {
2022-11-01 16:01:23 +01:00
return &PluginRegisterBuilder{
plugins: make(map[string]PluginAPI),
}
2022-11-01 14:26:54 +01:00
}
func (pr *PluginRegisterBuilder) Add(name, path string) *PluginRegisterBuilder {
2022-11-01 21:15:32 +01:00
pr.plugins[name] = PluginAPI{
path: path,
}
2022-11-01 14:26:54 +01:00
return pr
}
func (pr *PluginRegisterBuilder) Build(ctx context.Context) (*PluginRegister, error) {
clients := make(map[string]*pluginClientWrapper, 0)
errgroup, _ := errgroup.WithContext(ctx)
2022-11-01 21:15:32 +01:00
if err := os.MkdirAll(".char/plugins/", 0755); err != nil {
return nil, err
}
2022-11-01 14:26:54 +01:00
for name, p := range pr.plugins {
name, p := name, p
errgroup.Go(func() error {
2022-11-01 21:15:32 +01:00
pluginPath := fmt.Sprintf(".char/plugins/%s/dist/%s", name, name)
_, err := os.Stat(pluginPath)
if err != nil || os.Getenv("CHAR_DEV_MODE") == "true" {
log.Printf("building: %s", name)
cmd := exec.Command(
"sh",
"-c",
fmt.Sprintf(
"(cd .char/plugins/%s; go build -o dist/%s %s)",
name,
name,
p.path,
),
)
output, err := cmd.CombinedOutput()
if len(output) > 0 {
log.Println(string(output))
}
if err != nil {
return fmt.Errorf("could not build plugin: %w", err)
}
}
2022-11-01 14:26:54 +01:00
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "char",
},
Logger: hclog.New(&hclog.LoggerOptions{
Name: "char",
Output: os.Stdout,
2022-11-01 21:15:32 +01:00
Level: hclog.Error,
2022-11-01 14:26:54 +01:00
}),
2022-11-01 21:15:32 +01:00
Cmd: exec.Command(
fmt.Sprintf(
".char/plugins/%s/dist/%s",
name,
name,
),
),
2022-11-01 14:26:54 +01:00
Plugins: map[string]plugin.Plugin{
name: &p,
},
})
rpcClient, err := client.Client()
if err != nil {
return err
}
raw, err := rpcClient.Dispense(name)
if err != nil {
return err
}
pluginApi, ok := raw.(Plugin)
if !ok {
return errors.New("could not cast as plugin")
}
clients[name] = &pluginClientWrapper{
plugin: pluginApi,
client: client,
}
return nil
})
}
err := errgroup.Wait()
if err != nil {
return nil, err
}
return &PluginRegister{
clients: clients,
}, nil
}
// ---
type pluginClientWrapper struct {
plugin Plugin
client *plugin.Client
}
func (pcw *pluginClientWrapper) Close() {
pcw.client.Kill()
}
// ---
type PluginRegister struct {
clients map[string]*pluginClientWrapper
}
func (pr *PluginRegister) Close() error {
errgroup, _ := errgroup.WithContext(context.Background())
for _, c := range pr.clients {
c := c
errgroup.Go(func() error {
c.Close()
return nil
})
}
if err := errgroup.Wait(); err != nil {
return err
}
return nil
}
2022-11-01 21:15:32 +01:00
type AboutItem struct {
Name string
Version string
About string
}
func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) {
list := make([]AboutItem, 0)
2022-11-01 14:26:54 +01:00
errgroup, ctx := errgroup.WithContext(ctx)
for n, c := range pr.clients {
n, c := n, c
errgroup.Go(func() error {
2022-11-01 21:15:32 +01:00
about, err := c.plugin.About(ctx)
if err != nil {
return err
}
2022-11-01 14:26:54 +01:00
2022-11-01 21:15:32 +01:00
list = append(list, AboutItem{
Name: n,
Version: about.Version,
About: about.About,
})
2022-11-01 14:26:54 +01:00
return nil
})
}
if err := errgroup.Wait(); err != nil {
return nil, err
}
return list, nil
}