feat(open,generate): implement command parity

This commit is contained in:
Micheal Wilkinson
2026-03-20 17:53:39 +00:00
parent 043b859a42
commit 59caa62ac6
2 changed files with 75 additions and 9 deletions

View File

@@ -192,6 +192,68 @@ func (a *App) Destroy(castle string) error {
return os.RemoveAll(castleRoot)
}
func (a *App) Open(castle string) error {
if strings.TrimSpace(castle) == "" {
castle = "dotfiles"
}
editor := strings.TrimSpace(os.Getenv("EDITOR"))
if editor == "" {
return errors.New("the $EDITOR environment variable must be set to use this command")
}
castleHome := filepath.Join(a.ReposDir, castle, "home")
if info, err := os.Stat(castleHome); err != nil || !info.IsDir() {
return fmt.Errorf("could not open %s, expected %s to exist and contain dotfiles", castle, castleHome)
}
castleRoot := filepath.Join(a.ReposDir, castle)
cmd := exec.Command("sh", "-c", editor+" .")
cmd.Dir = castleRoot
cmd.Stdout = a.Stdout
cmd.Stderr = a.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("open failed: %w", err)
}
return nil
}
func (a *App) Generate(castlePath string) error {
trimmed := strings.TrimSpace(castlePath)
if trimmed == "" {
return errors.New("generate requires PATH")
}
absCastle, err := filepath.Abs(trimmed)
if err != nil {
return err
}
if err := os.MkdirAll(absCastle, 0o755); err != nil {
return err
}
if err := runGitWithIO(absCastle, a.Stdout, a.Stderr, "init"); err != nil {
return err
}
githubUser := ""
if out, cfgErr := gitOutput(absCastle, "config", "github.user"); cfgErr == nil {
githubUser = strings.TrimSpace(out)
}
if githubUser != "" {
repoName := filepath.Base(absCastle)
url := fmt.Sprintf("git@github.com:%s/%s.git", githubUser, repoName)
if err := runGitWithIO(absCastle, a.Stdout, a.Stderr, "remote", "add", "origin", url); err != nil {
return err
}
}
return os.MkdirAll(filepath.Join(absCastle, "home"), 0o755)
}
func (a *App) Link(castle string) error {
if strings.TrimSpace(castle) == "" {
castle = "dotfiles"