char/pkg/register/plugin_register.go

249 lines
4.7 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"
2022-11-02 20:25:55 +01:00
"strings"
2022-11-01 14:26:54 +01:00
"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-02 22:49:32 +01:00
pluginPath := fmt.Sprintf(".char/plugins/%s/dist/plugin", name)
2022-11-01 21:15:32 +01:00
_, 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(
2022-11-02 20:25:55 +01:00
"(cd .char/plugins/%s; go build -o dist/plugin %s/main.go)",
2022-11-01 21:15:32 +01:00
name,
2022-11-02 20:25:55 +01:00
strings.TrimSuffix(
strings.TrimSuffix(
p.path,
"main.go",
),
"/",
),
2022-11-01 21:15:32 +01:00
),
)
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-05 23:27:47 +01:00
Level: hclog.Debug,
2022-11-01 14:26:54 +01:00
}),
2022-11-01 21:15:32 +01:00
Cmd: exec.Command(
fmt.Sprintf(
2022-11-02 20:25:55 +01:00
".char/plugins/%s/dist/plugin",
2022-11-01 21:15:32 +01:00
name,
),
),
2022-11-01 14:26:54 +01:00
Plugins: map[string]plugin.Plugin{
2022-11-02 20:30:44 +01:00
PluginKey: &p,
2022-11-01 14:26:54 +01:00
},
})
rpcClient, err := client.Client()
if err != nil {
return err
}
2022-11-02 20:25:55 +01:00
raw, err := rpcClient.Dispense("plugin")
2022-11-01 14:26:54 +01:00
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-02 22:10:15 +01:00
type CommandAboutItem struct {
Name string
Args []string
Required []string
}
type CommandAboutItems []*CommandAboutItem
func FromAboutCommands(commands []*AboutCommand) CommandAboutItems {
cai := make(CommandAboutItems, 0)
for _, command := range commands {
cai = append(cai, &CommandAboutItem{
Name: command.Name,
Args: command.Args,
Required: command.Required,
})
}
return cai
}
2022-11-01 21:15:32 +01:00
type AboutItem struct {
2022-11-05 23:05:41 +01:00
Name string
Version string
About string
Vars []string
Commands CommandAboutItems
ClientName string
2022-11-01 21:15:32 +01:00
}
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)
2022-11-05 23:05:41 +01:00
for name, c := range pr.clients {
name, c := name, c
2022-11-01 14:26:54 +01:00
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{
2022-11-05 23:05:41 +01:00
Name: about.Name,
Version: about.Version,
About: about.About,
Vars: about.Vars,
Commands: FromAboutCommands(about.Commands),
ClientName: name,
2022-11-01 21:15:32 +01:00
})
2022-11-01 14:26:54 +01:00
return nil
})
}
if err := errgroup.Wait(); err != nil {
return nil, err
}
return list, nil
}
2022-11-05 23:05:41 +01:00
func (pr *PluginRegister) Do(ctx context.Context, clientName string, commandName string, args map[string]string) error {
errgroup, ctx := errgroup.WithContext(ctx)
client, ok := pr.clients[clientName]
if !ok {
return fmt.Errorf("plugin was not found: %s", clientName)
}
errgroup.Go(func() error {
2022-11-05 23:19:09 +01:00
return client.plugin.Do(ctx, &DoCommand{
CommandName: commandName,
Args: args,
})
2022-11-05 23:05:41 +01:00
})
2022-11-05 23:09:16 +01:00
if err := errgroup.Wait(); err != nil {
return err
}
2022-11-05 23:05:41 +01:00
return nil
}