feat: wire Layer 2 daemon plugin loader into archied startup
Daemon plugins (Yaegi-interpreted .go files) are now loaded from the configured plugin_dir at startup. Each file must export a 'Plugin' variable using the _Plugin wrapper struct, which bridges Yaegi types to the Go plugin.Plugin interface. Changes: - internal/plugin/pluginextract/ — generated Yaegi symbol table for the plugin package (mirrors wfextract pattern), including the _Plugin interface wrapper that lets interpreted code implement Go interfaces - LoadDir now accepts optional symbol tables via variadic parameter, avoiding an import cycle between plugin and pluginextract - Plugin tests rewritten as behavioral (not structural), verifying actual Yaegi interpretation of valid, invalid, and missing plugins - config.PluginDir field for specifying the plugin directory - daemon.PluginRegistry field for holding loaded plugins - main.go wires plugin loading: LoadDir at startup, register into daemon, log each loaded plugin Why: the Plugin interface and LoadDir existed but weren't connected to the daemon. Skills define workflows; daemon plugins are the extension point for forge resolvers, storage backends, and other core-daemon concerns.
This commit is contained in:
1 parent
8958a82233
commit
f38ba5d515
8 files changed
+256
-35
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -40,7 +41,12 @@ func (r *Registry) Plugins() []Plugin { return r.plugins }
|
||||
// Each file must export a variable named "Plugin" that implements the
|
||||
// Plugin interface. Failed plugins are logged and skipped — the daemon
|
||||
// starts with the remaining plugins (PRD section 5).
|
||||
func LoadDir(dir string) ([]Plugin, error) {
|
||||
//
|
||||
// extraSymbols are additional Yaegi symbol tables made available to
|
||||
// interpreted code. Callers should pass pluginextract.Symbols so that
|
||||
// interpreted types can satisfy the plugin.Plugin interface across
|
||||
// the Yaegi/Go boundary.
|
||||
func LoadDir(dir string, extraSymbols ...map[string]map[string]reflect.Value) ([]Plugin, error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -68,6 +74,11 @@ func LoadDir(dir string) ([]Plugin, error) {
|
||||
if err := i.Use(stdlib.Symbols); err != nil {
|
||||
continue
|
||||
}
|
||||
for _, s := range extraSymbols {
|
||||
if err := i.Use(s); err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if _, err := i.Eval(string(src)); err != nil {
|
||||
continue // skip files that don't compile
|
||||
}
|
||||
|
||||
+161
-26
@@ -1,45 +1,180 @@
|
||||
package plugin
|
||||
package plugin_test
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
// ── regression: Core plugin registry nonexistent ─────────────────────
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
|
||||
)
|
||||
|
||||
// ── Plugin interface and registry ────────────────────────────────────
|
||||
|
||||
func TestPluginInterfaceExists(t *testing.T) {
|
||||
// PRD section 5 Layer 2: plugins implement Name(), Version().
|
||||
// The Plugin interface must be usable for type assertions from
|
||||
// Yaegi-interpreted code.
|
||||
var p Plugin
|
||||
_ = p // ensures the type exists at compile time
|
||||
|
||||
// Verify the methods exist on the interface.
|
||||
var p plugin.Plugin
|
||||
_ = p
|
||||
type nameVersioner interface {
|
||||
Name() string
|
||||
Version() string
|
||||
}
|
||||
var _ nameVersioner = p // compile-time check
|
||||
var _ nameVersioner = p
|
||||
}
|
||||
|
||||
func TestRegistryExists(t *testing.T) {
|
||||
// A registry holds loaded plugins and dispatches extension points.
|
||||
r := &Registry{}
|
||||
r := &plugin.Registry{}
|
||||
if r == nil {
|
||||
t.Error("Gap: Registry type is not defined. " +
|
||||
"Define a Registry that holds loaded plugins and dispatches " +
|
||||
"extension calls (forge, ticketing, storage, secrets, notify).")
|
||||
t.Fatal("Registry type is not defined")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadPluginsFromConfigDir(t *testing.T) {
|
||||
// The daemon loads .go files from ~/.config/archie/plugins/ at
|
||||
// startup. Failed plugins are logged and skipped.
|
||||
plugins, err := LoadDir("/nonexistent/path")
|
||||
if err != nil {
|
||||
t.Error("Gap: LoadDir function is not defined. " +
|
||||
"Define LoadDir(path string) ([]Plugin, error) that discovers " +
|
||||
"and evaluates .go files from ~/.config/archie/plugins/.")
|
||||
// ── LoadDir behavioral tests ─────────────────────────────────────────
|
||||
|
||||
var symbols = pluginextract.Symbols
|
||||
|
||||
func TestLoadDirLoadsValidPlugin(t *testing.T) {
|
||||
// LoadDir must Yaegi-interpret .go files and return Plugins whose
|
||||
// exported "Plugin" variable satisfies the Plugin interface.
|
||||
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "hello.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 1 {
|
||||
t.Fatalf("LoadDir returned %d plugins, want 1", len(plugins))
|
||||
}
|
||||
if plugins[0].Name() != "hello" {
|
||||
t.Errorf("plugin.Name() = %q, want hello", plugins[0].Name())
|
||||
}
|
||||
if plugins[0].Version() != "1.0.0" {
|
||||
t.Errorf("plugin.Version() = %q, want 1.0.0", plugins[0].Version())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirSkipsInvalidPlugin(t *testing.T) {
|
||||
// A .go file that doesn't compile must be skipped — not crash the loader.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "broken.go"), []byte(`package main
|
||||
this is not valid Go syntax @@@@
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 0 {
|
||||
t.Errorf("LoadDir returned %d plugins from broken source, want 0", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirSkipsFileWithoutPluginExport(t *testing.T) {
|
||||
// A valid Go file without a "Plugin" export must be skipped.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "notaplugin.go"), []byte(`package main
|
||||
|
||||
func Something() string { return "nope" }
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 0 {
|
||||
t.Errorf("LoadDir returned %d plugins from file without Plugin export, want 0", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirSkipsNonGoFiles(t *testing.T) {
|
||||
// Only .go files are loaded. Other files are ignored.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "readme.md"), []byte(`# Not a plugin`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "hello.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 1 {
|
||||
t.Fatalf("LoadDir returned %d plugins, want 1 (only .go files count)", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirMultiplePlugins(t *testing.T) {
|
||||
// Multiple valid plugins must all be loaded, sorted by filename.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "01-alpha.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "alpha" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "02-beta.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "beta" },
|
||||
WVersion: func() string { return "2.0.0" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 2 {
|
||||
t.Fatalf("LoadDir returned %d plugins, want 2", len(plugins))
|
||||
}
|
||||
if plugins[0].Name() != "alpha" {
|
||||
t.Errorf("plugin[0].Name() = %q, want alpha (sorted by filename)", plugins[0].Name())
|
||||
}
|
||||
if plugins[1].Name() != "beta" {
|
||||
t.Errorf("plugin[1].Name() = %q, want beta", plugins[1].Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirNonexistentDirReturnsNil(t *testing.T) {
|
||||
plugins, err := plugin.LoadDir("/nonexistent/path/for/plugins")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if plugins != nil {
|
||||
// Even an empty directory should return empty slice, not nil.
|
||||
_ = plugins
|
||||
t.Errorf("LoadDir returned %v, want nil for nonexistent directory", plugins)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Code generated by 'yaegi extract github.com/samcharles93/archie-core/internal/plugin'. DO NOT EDIT.
|
||||
|
||||
package pluginextract
|
||||
|
||||
import (
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Symbols["github.com/samcharles93/archie-core/internal/plugin/plugin"] = map[string]reflect.Value{
|
||||
// function, constant and variable definitions
|
||||
"LoadDir": reflect.ValueOf(plugin.LoadDir),
|
||||
|
||||
// type definitions
|
||||
"Plugin": reflect.ValueOf((*plugin.Plugin)(nil)),
|
||||
"Registry": reflect.ValueOf((*plugin.Registry)(nil)),
|
||||
|
||||
// interface wrapper definitions
|
||||
"_Plugin": reflect.ValueOf((*_github_com_samcharles93_archie_core_internal_plugin_Plugin)(nil)),
|
||||
}
|
||||
}
|
||||
|
||||
// _github_com_samcharles93_archie_core_internal_plugin_Plugin is an interface wrapper for Plugin type
|
||||
type _github_com_samcharles93_archie_core_internal_plugin_Plugin struct {
|
||||
IValue interface{}
|
||||
WName func() string
|
||||
WVersion func() string
|
||||
}
|
||||
|
||||
func (W _github_com_samcharles93_archie_core_internal_plugin_Plugin) Name() string {
|
||||
return W.WName()
|
||||
}
|
||||
func (W _github_com_samcharles93_archie_core_internal_plugin_Plugin) Version() string {
|
||||
return W.WVersion()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Package pluginextract holds the Yaegi symbol table for internal/plugin,
|
||||
// generated by `go generate`. Kept separate from internal/plugin itself
|
||||
// so the generated file can import it without a self-import cycle.
|
||||
package pluginextract
|
||||
|
||||
import "reflect"
|
||||
|
||||
//go:generate go run github.com/traefik/yaegi/cmd/yaegi extract -name pluginextract github.com/samcharles93/archie-core/internal/plugin
|
||||
|
||||
var Symbols = map[string]map[string]reflect.Value{}
|
||||
Reference in new issue
Block a user