diff --git a/internal/multigitter/cmd.go b/internal/multigitter/cmd.go new file mode 100644 index 00000000..aee9dfae --- /dev/null +++ b/internal/multigitter/cmd.go @@ -0,0 +1,26 @@ +package multigitter + +import ( + "context" + "fmt" + "github.com/lindell/multi-gitter/internal/scm" + "os" + "os/exec" +) + +func prepareScriptCommand( + ctx context.Context, + repo scm.Repository, + workDir string, + scriptPath string, + arguments []string, +) (cmd *exec.Cmd) { + // Run the command that might or might not change the content of the repo + // If the command return a non-zero exit code, abort. + cmd = exec.CommandContext(ctx, scriptPath, arguments...) + cmd.Dir = workDir + cmd.Env = append(os.Environ(), + fmt.Sprintf("REPOSITORY=%s", repo.FullName()), + ) + return cmd +} diff --git a/internal/multigitter/print.go b/internal/multigitter/print.go index 9cb938b1..8be890f2 100755 --- a/internal/multigitter/print.go +++ b/internal/multigitter/print.go @@ -3,13 +3,11 @@ package multigitter import ( "context" "fmt" + "github.com/lindell/multi-gitter/internal/multigitter/repocounter" + "github.com/lindell/multi-gitter/internal/scm" log "github.com/sirupsen/logrus" "io" "os" - "os/exec" - - "github.com/lindell/multi-gitter/internal/multigitter/repocounter" - "github.com/lindell/multi-gitter/internal/scm" ) // Printer contains fields to be able to do the print command @@ -81,13 +79,7 @@ func (r Printer) runSingleRepo(ctx context.Context, repo scm.Repository) error { return err } - // Run the command that might or might not change the content of the repo - // If the command return a non-zero exit code, abort. - cmd := exec.CommandContext(ctx, r.ScriptPath, r.Arguments...) - cmd.Dir = tmpDir - cmd.Env = append(os.Environ(), - fmt.Sprintf("REPOSITORY=%s", repo.FullName()), - ) + cmd := prepareScriptCommand(ctx, repo, tmpDir, r.ScriptPath, r.Arguments) cmd.Stdout = r.Stdout cmd.Stderr = r.Stderr diff --git a/internal/multigitter/run.go b/internal/multigitter/run.go index 53191a2f..8f17b1f1 100755 --- a/internal/multigitter/run.go +++ b/internal/multigitter/run.go @@ -229,13 +229,7 @@ func (r *Runner) runSingleRepo(ctx context.Context, repo scm.Repository) (scm.Pu } } - // Run the command that might or might not change the content of the repo - // If the command return a non-zero exit code, abort. - cmd := exec.CommandContext(ctx, r.ScriptPath, r.Arguments...) - cmd.Dir = tmpDir - cmd.Env = append(os.Environ(), - fmt.Sprintf("REPOSITORY=%s", repo.FullName()), - ) + cmd := prepareScriptCommand(ctx, repo, tmpDir, r.ScriptPath, r.Arguments) if r.DryRun { cmd.Env = append(cmd.Env, "DRY_RUN=true") }