feat(modules): port zellij module to Go
This commit is contained in:
110
internal/modules/zellij/zellij.go
Normal file
110
internal/modules/zellij/zellij.go
Normal file
@@ -0,0 +1,110 @@
|
||||
// Package zellij installs zellij and optionally writes a remote config.
|
||||
package zellij
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/MDietermann/ArduinoArchSetup/internal/config"
|
||||
"github.com/MDietermann/ArduinoArchSetup/internal/module"
|
||||
"github.com/MDietermann/ArduinoArchSetup/internal/platform"
|
||||
)
|
||||
|
||||
type Module struct {
|
||||
cfg config.ZellijConfig
|
||||
pm platform.PackageManager
|
||||
}
|
||||
|
||||
func New(cfg config.ZellijConfig, pm platform.PackageManager) Module {
|
||||
return Module{cfg: cfg, pm: pm}
|
||||
}
|
||||
|
||||
type state struct {
|
||||
Installed bool
|
||||
ConfigOK bool
|
||||
ConfigPath string
|
||||
}
|
||||
|
||||
func (Module) Meta() module.Meta {
|
||||
return module.Meta{ID: "zellij", Name: "Zellij terminal multiplexer", Description: "Install zellij and config."}
|
||||
}
|
||||
|
||||
func configPath() (string, error) {
|
||||
h, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(h, ".config", "zellij", "config.kdl"), nil
|
||||
}
|
||||
|
||||
func (m Module) Detect(ctx context.Context) (module.State, error) {
|
||||
var s state
|
||||
ok, err := m.pm.IsInstalled(ctx, "zellij")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.Installed = ok
|
||||
p, err := configPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.ConfigPath = p
|
||||
if m.cfg.ConfigURL == "" {
|
||||
s.ConfigOK = true
|
||||
} else if _, err := os.Stat(p); err == nil {
|
||||
s.ConfigOK = true
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (m Module) Plan(ctx context.Context, cur module.State) (module.Plan, error) {
|
||||
s, _ := cur.(state)
|
||||
var actions []module.Action
|
||||
if !s.Installed {
|
||||
actions = append(actions, module.Action{
|
||||
Description: "pacman install zellij",
|
||||
Kind: module.ActionPackageInstall,
|
||||
Exec: func(ctx context.Context) error { return m.pm.Install(ctx, []string{"zellij"}) },
|
||||
})
|
||||
}
|
||||
if !s.ConfigOK {
|
||||
actions = append(actions, module.Action{
|
||||
Description: "download zellij config",
|
||||
Kind: module.ActionFileWrite,
|
||||
Exec: func(ctx context.Context) error {
|
||||
if err := os.MkdirAll(filepath.Dir(s.ConfigPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return platform.Download(ctx, m.cfg.ConfigURL, s.ConfigPath, 0o644)
|
||||
},
|
||||
})
|
||||
}
|
||||
return module.Plan{Actions: actions}, 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
|
||||
}
|
||||
s := st.(state)
|
||||
if !s.Installed {
|
||||
return fmt.Errorf("zellij not installed")
|
||||
}
|
||||
if !s.ConfigOK {
|
||||
return fmt.Errorf("zellij config missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user