add basic plugin architecture

This commit is contained in:
Kasper Juul Hermansen 2022-11-01 21:15:32 +01:00
parent e0e0290dcf
commit d484d44981
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
15 changed files with 150 additions and 28 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist/

View File

@ -15,20 +15,23 @@ func NewLsCommand() *cobra.Command {
r, err := register.
NewPluginRegisterBuilder().
Add("gocli", "").
Add("gocli", "plugins/gocli/main.go").
Add("rust", "plugins/rust/main.go").
Build(ctx)
if err != nil {
return err
}
defer r.Close()
about, err := r.About(ctx)
if err != nil {
return err
}
for plugin, aboutText := range about {
fmt.Printf("plugin: %s\n", plugin)
fmt.Printf("\tabout: %s\n", aboutText)
for _, a := range about {
fmt.Printf("plugin: %s\n", a.Name)
fmt.Printf("\tversion: %s\n", a.Version)
fmt.Printf("\tabout: %s\n", a.About)
fmt.Println()
}

View File

@ -0,0 +1 @@
../../../../

BIN
examples/basic/char Executable file

Binary file not shown.

4
examples/basic/char.yml Normal file
View File

@ -0,0 +1,4 @@
registry: git.front.kjuulh.io
plugins:
"kjuulh/char#plugins/gocli": {}
"kjuulh/char#plugins/rust": {}

View File

@ -1,3 +1,5 @@
#!/bin/bash
go run ../../main.go ls
go build -o char ../../main.go
CHAR_DEV_MODE=true ./char ls

View File

@ -1 +0,0 @@
package script

View File

@ -1,5 +1,13 @@
package register
type Plugin interface {
About() string
import "context"
type About struct {
Name string `json:"name"`
Version string `json:"version"`
About string `json:"about"`
}
type Plugin interface {
About(ctx context.Context) (*About, error)
}

View File

@ -7,6 +7,7 @@ import (
)
type PluginAPI struct {
path string
Impl Plugin
}

View File

@ -14,7 +14,7 @@ type PluginBuilder struct {
func NewPluginBuilder(name string, p Plugin) *PluginBuilder {
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Level: hclog.Error,
Output: os.Stderr,
JSONFormat: false,
})

View File

@ -1,7 +1,8 @@
package register
import (
"log"
"context"
"encoding/json"
"net/rpc"
)
@ -11,12 +12,18 @@ type PluginClient struct {
var _ Plugin = &PluginClient{}
func (pc *PluginClient) About() string {
func (pc *PluginClient) About(ctx context.Context) (*About, error) {
var resp string
err := pc.client.Call("Plugin.About", new(any), &resp)
if err != nil {
log.Fatal(err)
return nil, err
}
return resp
var about About
err = json.Unmarshal([]byte(resp), &about)
if err != nil {
return nil, err
}
return &about, nil
}

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
@ -23,7 +24,9 @@ func NewPluginRegisterBuilder() *PluginRegisterBuilder {
}
func (pr *PluginRegisterBuilder) Add(name, path string) *PluginRegisterBuilder {
pr.plugins[name] = PluginAPI{}
pr.plugins[name] = PluginAPI{
path: path,
}
return pr
}
@ -32,10 +35,38 @@ func (pr *PluginRegisterBuilder) Build(ctx context.Context) (*PluginRegister, er
clients := make(map[string]*pluginClientWrapper, 0)
errgroup, _ := errgroup.WithContext(ctx)
if err := os.MkdirAll(".char/plugins/", 0755); err != nil {
return nil, err
}
for name, p := range pr.plugins {
name, p := name, p
errgroup.Go(func() error {
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)
}
}
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: plugin.HandshakeConfig{
ProtocolVersion: 1,
@ -45,13 +76,15 @@ func (pr *PluginRegisterBuilder) Build(ctx context.Context) (*PluginRegister, er
Logger: hclog.New(&hclog.LoggerOptions{
Name: "char",
Output: os.Stdout,
Level: hclog.Debug,
Level: hclog.Error,
}),
Cmd: exec.Command("sh", "-c", fmt.Sprintf(
"(cd ./.char/plugins/%s; go run plugins/%s/main.go)",
name,
name,
)),
Cmd: exec.Command(
fmt.Sprintf(
".char/plugins/%s/dist/%s",
name,
name,
),
),
Plugins: map[string]plugin.Plugin{
name: &p,
},
@ -126,17 +159,30 @@ func (pr *PluginRegister) Close() error {
return nil
}
func (pr *PluginRegister) About(ctx context.Context) (map[string]string, error) {
list := make(map[string]string, len(pr.clients))
type AboutItem struct {
Name string
Version string
About string
}
func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) {
list := make([]AboutItem, 0)
errgroup, ctx := errgroup.WithContext(ctx)
for n, c := range pr.clients {
n, c := n, c
errgroup.Go(func() error {
about := c.plugin.About()
about, err := c.plugin.About(ctx)
if err != nil {
return err
}
list[n] = about
list = append(list, AboutItem{
Name: n,
Version: about.Version,
About: about.About,
})
return nil
})
}

View File

@ -1,10 +1,25 @@
package register
import (
"context"
"encoding/json"
)
type PluginServer struct {
Impl Plugin
}
func (ps *PluginServer) About(args any, resp *string) error {
*resp = ps.Impl.About()
r, err := ps.Impl.About(context.Background())
if err != nil {
return err
}
respB, err := json.Marshal(r)
if err != nil {
return err
}
*resp = string(respB)
return nil
}

View File

@ -10,9 +10,12 @@ import (
type GoCliPlugin struct {
}
// About implements register.Plugin
func (*GoCliPlugin) About() string {
return "gocli"
func (*GoCliPlugin) About(ctx context.Context) (*register.About, error) {
return &register.About{
Name: "gocli",
Version: "v0.0.1",
About: "golang cli provides a set of actions and presets supporting golang development",
}, nil
}
var _ register.Plugin = &GoCliPlugin{}

32
plugins/rust/main.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
"context"
"log"
"git.front.kjuulh.io/kjuulh/char/pkg/register"
)
type GoCliPlugin struct {
}
func (*GoCliPlugin) About(ctx context.Context) (*register.About, error) {
return &register.About{
Name: "rust",
Version: "v0.0.1",
About: "golang cli provides a set of actions and presets supporting golang development",
}, nil
}
var _ register.Plugin = &GoCliPlugin{}
func main() {
if err := register.
NewPluginBuilder(
"rust",
&GoCliPlugin{},
).
Serve(context.Background()); err != nil {
log.Fatal(err)
}
}