feat(modules): port yazi module to Go

This commit is contained in:
MDietermann
2026-04-14 08:47:00 +02:00
parent f5a45c4be6
commit 6dbfc41ce2
2 changed files with 187 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
// Package yazi installs a declarative list of pacman packages.
package yazi
import (
"context"
"fmt"
"strings"
"github.com/MDietermann/ArduinoArchSetup/internal/module"
"github.com/MDietermann/ArduinoArchSetup/internal/platform"
)
type Module struct {
pkgs []string
pm platform.PackageManager
}
func New(pkgs []string, pm platform.PackageManager) Module {
return Module{pkgs: pkgs, pm: pm}
}
type state struct{ Missing []string }
func (Module) Meta() module.Meta {
return module.Meta{ID: "yazi", Name: "Yazi file manager", Description: "Install yazi."}
}
func (m Module) Detect(ctx context.Context) (module.State, error) {
var missing []string
for _, p := range m.pkgs {
ok, err := m.pm.IsInstalled(ctx, p)
if err != nil {
return nil, err
}
if !ok {
missing = append(missing, p)
}
}
return state{Missing: missing}, nil
}
func (m Module) Plan(ctx context.Context, cur module.State) (module.Plan, error) {
s, _ := cur.(state)
if len(s.Missing) == 0 {
return module.Plan{}, nil
}
missing := append([]string{}, s.Missing...)
return module.Plan{Actions: []module.Action{{
Description: "pacman install " + strings.Join(missing, ", "),
Kind: module.ActionPackageInstall,
Exec: func(ctx context.Context) error { return m.pm.Install(ctx, missing) },
}}}, nil
}
func (m Module) Apply(ctx context.Context, p module.Plan, r module.Reporter) error {
for i, a := range p.Actions {
r.Progress(m.Meta().ID, i+1, len(p.Actions), a.Description)
if err := a.Exec(ctx); err != nil {
return err
}
}
return nil
}
func (m Module) Verify(ctx context.Context) error {
st, err := m.Detect(ctx)
if err != nil {
return err
}
if missing := st.(state).Missing; len(missing) > 0 {
return fmt.Errorf("packages not installed after apply: %v", missing)
}
return nil
}

View File

@@ -0,0 +1,113 @@
package yazi
import (
"context"
"strings"
"testing"
"github.com/MDietermann/ArduinoArchSetup/internal/module"
"github.com/MDietermann/ArduinoArchSetup/internal/platform"
)
type fakeRunner struct {
runCalls [][]string
outCalls [][]string
installed map[string]bool // key: pkg name
}
func (f *fakeRunner) Run(_ context.Context, name string, args ...string) error {
f.runCalls = append(f.runCalls, append([]string{name}, args...))
return nil
}
func (f *fakeRunner) Output(_ context.Context, name string, args ...string) ([]byte, error) {
f.outCalls = append(f.outCalls, append([]string{name}, args...))
if name == "pacman" && len(args) == 2 && args[0] == "-Q" {
if f.installed[args[1]] {
return []byte(args[1] + " 1.0-1\n"), nil
}
return nil, errExitOne
}
return nil, nil
}
type exitOneErr struct{}
func (exitOneErr) Error() string { return "exit status 1" }
var errExitOne = exitOneErr{}
type nopReporter struct{}
func (nopReporter) Progress(string, int, int, string) {}
func (nopReporter) Log(string, string) {}
func TestDetectAbsent(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{}}
m := New([]string{"yazi"}, platform.NewPacman(r))
st, err := m.Detect(context.Background())
if err != nil {
t.Fatal(err)
}
s := st.(state)
if len(s.Missing) != 1 {
t.Fatalf("want 1 missing, got %v", s.Missing)
}
}
func TestDetectPresent(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{"yazi": true}}
m := New([]string{"yazi"}, platform.NewPacman(r))
st, _ := m.Detect(context.Background())
if len(st.(state).Missing) != 0 {
t.Fatalf("want 0 missing, got %v", st.(state).Missing)
}
}
func TestPlanAbsentEmitsInstallAction(t *testing.T) {
m := New([]string{"yazi"}, nil)
p, _ := m.Plan(context.Background(), state{Missing: []string{"yazi"}})
if len(p.Actions) != 1 {
t.Fatalf("want 1 action, got %d", len(p.Actions))
}
if !strings.Contains(p.Actions[0].Description, "yazi") {
t.Fatalf("desc: %s", p.Actions[0].Description)
}
}
func TestPlanPresentEmpty(t *testing.T) {
m := New([]string{"yazi"}, nil)
p, _ := m.Plan(context.Background(), state{Missing: nil})
if !p.IsEmpty() {
t.Fatal("want empty plan")
}
}
func TestApplyShellsOut(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{}}
m := New([]string{"yazi"}, platform.NewPacman(r))
st, _ := m.Detect(context.Background())
p, _ := m.Plan(context.Background(), st)
if err := m.Apply(context.Background(), p, nopReporter{}); err != nil {
t.Fatal(err)
}
sawInstall := false
for _, c := range r.runCalls {
if len(c) >= 4 && c[0] == "sudo" && c[1] == "pacman" && c[2] == "-S" {
sawInstall = true
}
}
if !sawInstall {
t.Fatalf("no pacman install call; calls=%v", r.runCalls)
}
}
func TestVerifyAfterApply(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{"yazi": true}}
m := New([]string{"yazi"}, platform.NewPacman(r))
if err := m.Verify(context.Background()); err != nil {
t.Fatal(err)
}
}
var _ module.Module = Module{}