75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
// 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
|
|
}
|