gosick #1
@@ -180,9 +180,14 @@ type openCmd struct {
|
|||||||
Castle string `arg:"" optional:"" name:"CASTLE" help:"Castle name."`
|
Castle string `arg:"" optional:"" name:"CASTLE" help:"Castle name."`
|
||||||
}
|
}
|
||||||
|
|
||||||
type execCmd struct{}
|
type execCmd struct {
|
||||||
|
Castle string `arg:"" name:"CASTLE" help:"Castle name."`
|
||||||
|
Command []string `arg:"" name:"COMMAND" help:"Shell command to execute in the castle root."`
|
||||||
|
}
|
||||||
|
|
||||||
type execAllCmd struct{}
|
type execAllCmd struct {
|
||||||
|
Command []string `arg:"" name:"COMMAND" help:"Shell command to execute in each castle root."`
|
||||||
|
}
|
||||||
|
|
||||||
type rcCmd struct {
|
type rcCmd struct {
|
||||||
Castle string `arg:"" optional:"" name:"CASTLE" help:"Castle name."`
|
Castle string `arg:"" optional:"" name:"CASTLE" help:"Castle name."`
|
||||||
@@ -200,8 +205,8 @@ func (c *commitCmd) Run(app *core.App) error {
|
|||||||
func (c *destroyCmd) Run(app *core.App) error { return app.Destroy(defaultCastle(c.Castle)) }
|
func (c *destroyCmd) Run(app *core.App) error { return app.Destroy(defaultCastle(c.Castle)) }
|
||||||
func (c *cdCmd) Run(app *core.App) error { return app.ShowPath(defaultCastle(c.Castle)) }
|
func (c *cdCmd) Run(app *core.App) error { return app.ShowPath(defaultCastle(c.Castle)) }
|
||||||
func (c *openCmd) Run(app *core.App) error { return app.Open(defaultCastle(c.Castle)) }
|
func (c *openCmd) Run(app *core.App) error { return app.Open(defaultCastle(c.Castle)) }
|
||||||
func (c *execCmd) Run() error { return notImplemented("exec") }
|
func (c *execCmd) Run(app *core.App) error { return app.Exec(c.Castle, c.Command) }
|
||||||
func (c *execAllCmd) Run() error { return notImplemented("exec_all") }
|
func (c *execAllCmd) Run(app *core.App) error { return app.ExecAll(c.Command) }
|
||||||
func (c *rcCmd) Run(app *core.App) error { return app.Rc(defaultCastle(c.Castle)) }
|
func (c *rcCmd) Run(app *core.App) error { return app.Rc(defaultCastle(c.Castle)) }
|
||||||
func (c *generateCmd) Run(app *core.App) error { return app.Generate(c.Path) }
|
func (c *generateCmd) Run(app *core.App) error { return app.Generate(c.Path) }
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,17 @@ func (s *CLISuite) TestRun_Cd_ExplicitCastle() {
|
|||||||
require.Empty(s.T(), s.stderr.String())
|
require.Empty(s.T(), s.stderr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *CLISuite) TestRun_Exec_RunsCommandInCastleRoot() {
|
||||||
|
castleRoot := filepath.Join(s.homeDir, ".homesick", "repos", "dotfiles")
|
||||||
|
require.NoError(s.T(), os.MkdirAll(castleRoot, 0o755))
|
||||||
|
|
||||||
|
exitCode := cli.Run([]string{"exec", "dotfiles", "pwd"}, s.stdout, s.stderr)
|
||||||
|
|
||||||
|
require.Equal(s.T(), 0, exitCode)
|
||||||
|
require.Contains(s.T(), s.stdout.String(), castleRoot)
|
||||||
|
require.Empty(s.T(), s.stderr.String())
|
||||||
|
}
|
||||||
|
|
||||||
func (s *CLISuite) TestRun_CloneSubcommandHelp() {
|
func (s *CLISuite) TestRun_CloneSubcommandHelp() {
|
||||||
exitCode := cli.Run([]string{"clone", "--help"}, s.stdout, s.stderr)
|
exitCode := cli.Run([]string{"clone", "--help"}, s.stdout, s.stderr)
|
||||||
|
|
||||||
|
|||||||
@@ -219,6 +219,75 @@ func (a *App) Open(castle string) error {
|
|||||||
return nil
|
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 {
|
func (a *App) Generate(castlePath string) error {
|
||||||
trimmed := strings.TrimSpace(castlePath)
|
trimmed := strings.TrimSpace(castlePath)
|
||||||
if trimmed == "" {
|
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