feat(open,generate): implement command parity
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user