feat(plugins): implement yaegi plugin loader with basic test

Move yaegi_symbols to pkg/yaegi_symbols/ to avoid import cycle.
Create pkg/plugins/yaegi/ package with LoadPlugin function.
The basic Plugin interface assertion (Name, Version) passes.
This commit is contained in:
kolaente
2026-01-29 20:53:31 +01:00
parent 14c028d78e
commit 5dc8982c65
12 changed files with 114 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package yaegi
import (
"fmt"
"os"
"path/filepath"
"strings"
"code.vikunja.io/api/pkg/plugins"
"code.vikunja.io/api/pkg/yaegi_symbols"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)
// LoadPlugin loads a plugin from a directory of Go source files using the Yaegi interpreter.
func LoadPlugin(dir string) (plugins.Plugin, error) {
i := interp.New(interp.Options{})
i.Use(stdlib.Symbols)
i.Use(yaegi_symbols.Symbols)
// Read all .go files in the directory
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("reading plugin dir %s: %w", dir, err)
}
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".go") {
continue
}
src, err := os.ReadFile(filepath.Join(dir, e.Name()))
if err != nil {
return nil, fmt.Errorf("reading %s: %w", e.Name(), err)
}
_, err = i.Eval(string(src))
if err != nil {
return nil, fmt.Errorf("evaluating %s: %w", e.Name(), err)
}
}
// Look up the NewPlugin entry point
v, err := i.Eval("main.NewPlugin")
if err != nil {
return nil, fmt.Errorf("looking up NewPlugin: %w", err)
}
newPlugin, ok := v.Interface().(func() plugins.Plugin)
if !ok {
return nil, fmt.Errorf("NewPlugin has wrong signature: %T", v.Interface())
}
return newPlugin(), nil
}

View File

@@ -0,0 +1,44 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package yaegi
import (
"os"
"path/filepath"
"testing"
)
func TestLoadPlugin(t *testing.T) {
pluginDir := filepath.Join("..", "..", "..", "examples", "plugins", "example")
mainGo := filepath.Join(pluginDir, "main.go")
if _, err := os.Stat(mainGo); err != nil {
t.Fatalf("example plugin source not found at %s: %v", mainGo, err)
}
p, err := LoadPlugin(pluginDir)
if err != nil {
t.Fatalf("LoadPlugin failed: %v", err)
}
if p.Name() != "example" {
t.Errorf("expected plugin name 'example', got %q", p.Name())
}
if p.Version() != "1.0.0" {
t.Errorf("expected version '1.0.0', got %q", p.Version())
}
}