Two bugs found by adversarial tests: 1. _Plugin wrapper panicked on nil WName/WVersion — the daemon calls Name()/Version() at startup to log loaded plugins, so a plugin with nil func fields would crash the daemon. Fixed with nil guards returning empty strings. 2. LoadDir returned nil for empty directories instead of an empty slice — inconsistent with the contract (nil means 'no plugin dir configured', empty means 'no plugins found'). Added 9 adversarial tests that attempt to break LoadDir: - Nil function fields (must not panic on load or call) - Empty name/version (valid per interface contract) - Wrong export type (int instead of Plugin — must skip) - Partial interface implementation (missing method — must skip) - Empty directory (returns empty slice, not nil) - Nested subdirectories (must not be traversed) - Panic in Name() function (deferred to call time, loader survives) - Unused import (must not crash eval) - Go build tags (must be ignored in Yaegi) Why: per test-first-build skill, every build/test failure results in a test that would have caught it. Adversarial tests alongside confirmation tests prevent silent breakage.
97 lines
2.6 KiB
Go
97 lines
2.6 KiB
Go
// Package plugin defines the core plugin interface for extending archie-core's
|
|
// daemon at startup. Plugins are Yaegi-interpreted .go files loaded from
|
|
// ~/.config/archie/plugins/ that register themselves with the daemon's
|
|
// extension registry. PRD section 5 Layer 2.
|
|
package plugin
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/traefik/yaegi/interp"
|
|
"github.com/traefik/yaegi/stdlib"
|
|
)
|
|
|
|
// Plugin is a core plugin loaded by the daemon at startup. Each .go file
|
|
// in ~/.config/archie/plugins/ must export a variable named "Plugin" that
|
|
// satisfies this interface.
|
|
type Plugin interface {
|
|
Name() string
|
|
Version() string
|
|
}
|
|
|
|
// Registry holds loaded plugins and dispatches extension point calls.
|
|
type Registry struct {
|
|
plugins []Plugin
|
|
}
|
|
|
|
// Register adds a plugin to the registry.
|
|
func (r *Registry) Register(p Plugin) {
|
|
r.plugins = append(r.plugins, p)
|
|
}
|
|
|
|
// Plugins returns all registered plugins.
|
|
func (r *Registry) Plugins() []Plugin { return r.plugins }
|
|
|
|
// LoadDir discovers and evaluates .go files in the given directory.
|
|
// 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).
|
|
//
|
|
// 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
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read plugin dir %s: %w", dir, err)
|
|
}
|
|
|
|
var names []string
|
|
for _, e := range entries {
|
|
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
|
|
names = append(names, e.Name())
|
|
}
|
|
}
|
|
sort.Strings(names)
|
|
|
|
plugins := make([]Plugin, 0)
|
|
for _, name := range names {
|
|
path := filepath.Join(dir, name)
|
|
src, err := os.ReadFile(path)
|
|
if err != nil {
|
|
continue // skip unreadable files
|
|
}
|
|
i := interp.New(interp.Options{})
|
|
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
|
|
}
|
|
v, err := i.Eval("main.Plugin")
|
|
if err != nil {
|
|
continue // skip files without a Plugin export
|
|
}
|
|
p, ok := v.Interface().(Plugin)
|
|
if !ok {
|
|
continue // skip files with wrong Plugin type
|
|
}
|
|
plugins = append(plugins, p)
|
|
}
|
|
return plugins, nil
|
|
}
|