feat(zsh): implement PlanForce
This commit is contained in:
@@ -3,7 +3,6 @@ package zsh
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -133,5 +132,42 @@ func (m Module) Verify(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m Module) PlanForce(ctx context.Context) (module.Plan, error) {
|
func (m Module) PlanForce(ctx context.Context) (module.Plan, error) {
|
||||||
return module.Plan{}, errors.New("PlanForce not implemented")
|
actions := []module.Action{{
|
||||||
|
Description: "pacman -S (force) zsh",
|
||||||
|
Kind: module.ActionPackageInstall,
|
||||||
|
Exec: func(ctx context.Context) error { return m.pm.Reinstall(ctx, []string{"zsh"}) },
|
||||||
|
}}
|
||||||
|
actions = append(actions, module.Action{
|
||||||
|
Description: "reclone oh-my-zsh from " + m.cfg.OhMyZshRepo + " (force)",
|
||||||
|
Kind: module.ActionGitClone,
|
||||||
|
Exec: func(ctx context.Context) error {
|
||||||
|
omz, err := omzHome()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.RemoveAll(omz); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.git.Clone(ctx, m.cfg.OhMyZshRepo, omz)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
for _, p := range m.cfg.Plugins {
|
||||||
|
p := p
|
||||||
|
actions = append(actions, module.Action{
|
||||||
|
Description: "reclone plugin " + p.Name + " (force)",
|
||||||
|
Kind: module.ActionGitClone,
|
||||||
|
Exec: func(ctx context.Context) error {
|
||||||
|
omz, err := omzHome()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dest := filepath.Join(omz, "custom", "plugins", p.Name)
|
||||||
|
if err := os.RemoveAll(dest); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.git.Clone(ctx, p.Repo, dest)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return module.Plan{Actions: actions}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,4 +114,46 @@ func TestZshPlanApplyInstallsEverything(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPlanForceReinstallsEverything(t *testing.T) {
|
||||||
|
home := t.TempDir()
|
||||||
|
t.Setenv("HOME", home)
|
||||||
|
os.MkdirAll(filepath.Join(home, ".oh-my-zsh", ".git"), 0o755)
|
||||||
|
os.MkdirAll(filepath.Join(home, ".oh-my-zsh", "custom", "plugins", "zsh-autosuggestions", ".git"), 0o755)
|
||||||
|
r := &fakeRunner{installed: map[string]bool{"zsh": true}}
|
||||||
|
m := New(config.ZshConfig{
|
||||||
|
OhMyZshRepo: "https://x/omz.git",
|
||||||
|
Plugins: []config.ZshPlugin{{Name: "zsh-autosuggestions", Repo: "https://x/a.git"}},
|
||||||
|
}, platform.NewPacman(r), platform.GitClient{Runner: r})
|
||||||
|
plan, err := m.PlanForce(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("PlanForce: %v", err)
|
||||||
|
}
|
||||||
|
if plan.IsEmpty() {
|
||||||
|
t.Fatal("force plan must not be empty")
|
||||||
|
}
|
||||||
|
for _, a := range plan.Actions {
|
||||||
|
if err := a.Exec(context.Background()); err != nil {
|
||||||
|
t.Fatalf("Exec: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sawReinstall, sawOMZ, sawPlugin := false, false, false
|
||||||
|
for _, c := range r.runCalls {
|
||||||
|
if len(c) >= 4 && c[0] == "sudo" && c[1] == "pacman" && c[2] == "-S" && c[3] == "--noconfirm" {
|
||||||
|
sawReinstall = true
|
||||||
|
}
|
||||||
|
if len(c) >= 4 && c[0] == "git" && c[3] == "https://x/omz.git" {
|
||||||
|
sawOMZ = true
|
||||||
|
}
|
||||||
|
if len(c) >= 4 && c[0] == "git" && c[3] == "https://x/a.git" {
|
||||||
|
sawPlugin = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !sawReinstall || !sawOMZ || !sawPlugin {
|
||||||
|
t.Fatalf("missing force action; calls=%v", r.runCalls)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(home, ".oh-my-zsh")); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("oh-my-zsh dir should have been removed; stat err=%v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var _ module.Module = Module{}
|
var _ module.Module = Module{}
|
||||||
|
|||||||
Reference in New Issue
Block a user