From 2b30b371e4c0114d608bfdb08e4077eb99de827a Mon Sep 17 00:00:00 2001 From: MDietermann Date: Tue, 14 Apr 2026 08:46:49 +0200 Subject: [PATCH] feat(modules): port gittools (lazygit) module to Go --- internal/modules/gittools/gittools.go | 74 ++++++++++++++ internal/modules/gittools/gittools_test.go | 113 +++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 internal/modules/gittools/gittools.go create mode 100644 internal/modules/gittools/gittools_test.go diff --git a/internal/modules/gittools/gittools.go b/internal/modules/gittools/gittools.go new file mode 100644 index 0000000..dfad873 --- /dev/null +++ b/internal/modules/gittools/gittools.go @@ -0,0 +1,74 @@ +// Package gittools installs a declarative list of pacman packages. +package gittools + +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: "gittools", Name: "Git tools", Description: "Install lazygit."} +} + +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 +} diff --git a/internal/modules/gittools/gittools_test.go b/internal/modules/gittools/gittools_test.go new file mode 100644 index 0000000..7041ab9 --- /dev/null +++ b/internal/modules/gittools/gittools_test.go @@ -0,0 +1,113 @@ +package gittools + +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{"lazygit"}, 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{"lazygit": true}} + m := New([]string{"lazygit"}, 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{"lazygit"}, nil) + p, _ := m.Plan(context.Background(), state{Missing: []string{"lazygit"}}) + if len(p.Actions) != 1 { + t.Fatalf("want 1 action, got %d", len(p.Actions)) + } + if !strings.Contains(p.Actions[0].Description, "lazygit") { + t.Fatalf("desc: %s", p.Actions[0].Description) + } +} + +func TestPlanPresentEmpty(t *testing.T) { + m := New([]string{"lazygit"}, 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{"lazygit"}, 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{"lazygit": true}} + m := New([]string{"lazygit"}, platform.NewPacman(r)) + if err := m.Verify(context.Background()); err != nil { + t.Fatal(err) + } +} + +var _ module.Module = Module{}