feat(cli): implement exec and exec_all commands
This commit is contained in:
@@ -219,6 +219,75 @@ func (a *App) Open(castle string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) Exec(castle string, command []string) error {
|
||||
commandString := strings.TrimSpace(strings.Join(command, " "))
|
||||
if commandString == "" {
|
||||
return errors.New("exec requires COMMAND")
|
||||
}
|
||||
|
||||
castleRoot := filepath.Join(a.ReposDir, castle)
|
||||
if _, err := os.Stat(castleRoot); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("castle %q not found", castle)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := exec.Command("sh", "-c", commandString)
|
||||
cmd.Dir = castleRoot
|
||||
cmd.Stdout = a.Stdout
|
||||
cmd.Stderr = a.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("exec failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) ExecAll(command []string) error {
|
||||
commandString := strings.TrimSpace(strings.Join(command, " "))
|
||||
if commandString == "" {
|
||||
return errors.New("exec_all requires COMMAND")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(a.ReposDir); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var castles []string
|
||||
err := filepath.WalkDir(a.ReposDir, func(path string, d fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if !d.IsDir() || d.Name() != ".git" {
|
||||
return nil
|
||||
}
|
||||
|
||||
castleRoot := filepath.Dir(path)
|
||||
rel, err := filepath.Rel(a.ReposDir, castleRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
castles = append(castles, rel)
|
||||
return filepath.SkipDir
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sort.Strings(castles)
|
||||
for _, castle := range castles {
|
||||
if err := a.Exec(castle, []string{commandString}); err != nil {
|
||||
return fmt.Errorf("exec_all failed for %q: %w", castle, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) Generate(castlePath string) error {
|
||||
trimmed := strings.TrimSpace(castlePath)
|
||||
if trimmed == "" {
|
||||
|
||||
79
internal/homesick/core/exec_test.go
Normal file
79
internal/homesick/core/exec_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.hrafn.xyz/aether/gosick/internal/homesick/core"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type ExecSuite struct {
|
||||
suite.Suite
|
||||
tmpDir string
|
||||
homeDir string
|
||||
reposDir string
|
||||
stdout *bytes.Buffer
|
||||
stderr *bytes.Buffer
|
||||
app *core.App
|
||||
}
|
||||
|
||||
func TestExecSuite(t *testing.T) {
|
||||
suite.Run(t, new(ExecSuite))
|
||||
}
|
||||
|
||||
func (s *ExecSuite) SetupTest() {
|
||||
s.tmpDir = s.T().TempDir()
|
||||
s.homeDir = filepath.Join(s.tmpDir, "home")
|
||||
s.reposDir = filepath.Join(s.homeDir, ".homesick", "repos")
|
||||
require.NoError(s.T(), os.MkdirAll(s.reposDir, 0o755))
|
||||
|
||||
s.stdout = &bytes.Buffer{}
|
||||
s.stderr = &bytes.Buffer{}
|
||||
s.app = &core.App{
|
||||
HomeDir: s.homeDir,
|
||||
ReposDir: s.reposDir,
|
||||
Stdout: s.stdout,
|
||||
Stderr: s.stderr,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ExecSuite) createCastle(name string) string {
|
||||
castleRoot := filepath.Join(s.reposDir, name)
|
||||
require.NoError(s.T(), os.MkdirAll(castleRoot, 0o755))
|
||||
return castleRoot
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExec_UnknownCastleReturnsError() {
|
||||
err := s.app.Exec("nonexistent", []string{"pwd"})
|
||||
require.Error(s.T(), err)
|
||||
require.Contains(s.T(), err.Error(), "not found")
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExec_RunsCommandInCastleRoot() {
|
||||
castleRoot := s.createCastle("dotfiles")
|
||||
|
||||
require.NoError(s.T(), s.app.Exec("dotfiles", []string{"pwd"}))
|
||||
require.Contains(s.T(), s.stdout.String(), castleRoot)
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExec_ForwardsStdoutAndStderr() {
|
||||
s.createCastle("dotfiles")
|
||||
|
||||
require.NoError(s.T(), s.app.Exec("dotfiles", []string{"echo out && echo err >&2"}))
|
||||
require.Contains(s.T(), s.stdout.String(), "out")
|
||||
require.Contains(s.T(), s.stderr.String(), "err")
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExecAll_RunsCommandForEachCastle() {
|
||||
zeta := s.createCastle("zeta")
|
||||
alpha := s.createCastle("alpha")
|
||||
require.NoError(s.T(), os.MkdirAll(filepath.Join(zeta, ".git"), 0o755))
|
||||
require.NoError(s.T(), os.MkdirAll(filepath.Join(alpha, ".git"), 0o755))
|
||||
|
||||
require.NoError(s.T(), s.app.ExecAll([]string{"basename \"$PWD\""}))
|
||||
require.Equal(s.T(), "alpha\nzeta\n", s.stdout.String())
|
||||
}
|
||||
Reference in New Issue
Block a user