feat(modules): port packages module to Go
This commit is contained in:
74
internal/modules/packages/packages.go
Normal file
74
internal/modules/packages/packages.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Package packages installs a declarative list of pacman packages.
|
||||
package packages
|
||||
|
||||
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: "packages", Name: "Base packages", Description: "Install base pacman package list."}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
113
internal/modules/packages/packages_test.go
Normal file
113
internal/modules/packages/packages_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package packages
|
||||
|
||||
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{"curl", "wget"}, platform.NewPacman(r))
|
||||
st, err := m.Detect(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := st.(state)
|
||||
if len(s.Missing) != 2 {
|
||||
t.Fatalf("want 2 missing, got %v", s.Missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectPresent(t *testing.T) {
|
||||
r := &fakeRunner{installed: map[string]bool{"curl": true, "wget": true}}
|
||||
m := New([]string{"curl", "wget"}, 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{"curl", "wget"}, nil)
|
||||
p, _ := m.Plan(context.Background(), state{Missing: []string{"curl", "wget"}})
|
||||
if len(p.Actions) != 1 {
|
||||
t.Fatalf("want 1 action, got %d", len(p.Actions))
|
||||
}
|
||||
if !strings.Contains(p.Actions[0].Description, "curl") {
|
||||
t.Fatalf("desc: %s", p.Actions[0].Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanPresentEmpty(t *testing.T) {
|
||||
m := New([]string{"curl"}, 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{"curl"}, 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{"curl": true}}
|
||||
m := New([]string{"curl"}, platform.NewPacman(r))
|
||||
if err := m.Verify(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var _ module.Module = Module{}
|
||||
Reference in New Issue
Block a user