From 447594a5d8851018cb1d3acd872240d8be7cd2c8 Mon Sep 17 00:00:00 2001 From: nitram509 Date: Sun, 2 Apr 2023 00:58:59 +0200 Subject: [PATCH 1/3] refactor redundant code into a function --- internal/multigitter/cmd.go | 23 +++++++++++++++++++++++ internal/multigitter/print.go | 14 +++----------- internal/multigitter/run.go | 11 +---------- 3 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 internal/multigitter/cmd.go diff --git a/internal/multigitter/cmd.go b/internal/multigitter/cmd.go new file mode 100644 index 00000000..f0dec57f --- /dev/null +++ b/internal/multigitter/cmd.go @@ -0,0 +1,23 @@ +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, dryRun bool) (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()), + ) + if dryRun { + cmd.Env = append(cmd.Env, "DRY_RUN=true") + } + return cmd +} diff --git a/internal/multigitter/print.go b/internal/multigitter/print.go index 9cb938b1..3d10d879 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, false) cmd.Stdout = r.Stdout cmd.Stderr = r.Stderr diff --git a/internal/multigitter/run.go b/internal/multigitter/run.go index 53191a2f..b15874dc 100755 --- a/internal/multigitter/run.go +++ b/internal/multigitter/run.go @@ -229,16 +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()), - ) - if r.DryRun { - cmd.Env = append(cmd.Env, "DRY_RUN=true") - } + cmd := prepareScriptCommand(ctx, repo, tmpDir, r.ScriptPath, r.Arguments, r.DryRun) // Setup logger that transfers stdout and stderr from the run to logs writer := logger.NewLogger(log) From 10189cb73a175d81484fac117eff1e34d39b0921 Mon Sep 17 00:00:00 2001 From: nitram509 Date: Mon, 10 Apr 2023 18:22:22 +0200 Subject: [PATCH 2/3] incorporate feedback --- internal/multigitter/cmd.go | 9 +++++---- internal/multigitter/print.go | 2 +- internal/multigitter/run.go | 5 ++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/multigitter/cmd.go b/internal/multigitter/cmd.go index f0dec57f..3d14fec8 100644 --- a/internal/multigitter/cmd.go +++ b/internal/multigitter/cmd.go @@ -8,7 +8,11 @@ import ( "os/exec" ) -func prepareScriptCommand(ctx context.Context, repo scm.Repository, workDir string, scriptPath string, arguments []string, dryRun bool) (cmd *exec.Cmd) { +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...) @@ -16,8 +20,5 @@ func prepareScriptCommand(ctx context.Context, repo scm.Repository, workDir stri cmd.Env = append(os.Environ(), fmt.Sprintf("REPOSITORY=%s", repo.FullName()), ) - if dryRun { - cmd.Env = append(cmd.Env, "DRY_RUN=true") - } return cmd } diff --git a/internal/multigitter/print.go b/internal/multigitter/print.go index 3d10d879..8be890f2 100755 --- a/internal/multigitter/print.go +++ b/internal/multigitter/print.go @@ -79,7 +79,7 @@ func (r Printer) runSingleRepo(ctx context.Context, repo scm.Repository) error { return err } - cmd := prepareScriptCommand(ctx, repo, tmpDir, r.ScriptPath, r.Arguments, false) + 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 b15874dc..8f17b1f1 100755 --- a/internal/multigitter/run.go +++ b/internal/multigitter/run.go @@ -229,7 +229,10 @@ func (r *Runner) runSingleRepo(ctx context.Context, repo scm.Repository) (scm.Pu } } - cmd := prepareScriptCommand(ctx, repo, tmpDir, r.ScriptPath, r.Arguments, r.DryRun) + cmd := prepareScriptCommand(ctx, repo, tmpDir, r.ScriptPath, r.Arguments) + if r.DryRun { + cmd.Env = append(cmd.Env, "DRY_RUN=true") + } // Setup logger that transfers stdout and stderr from the run to logs writer := logger.NewLogger(log) From 380c9fd56ef1931dcd5b7c03c9e40c2538054875 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Mon, 10 Apr 2023 21:39:42 +0200 Subject: [PATCH 3/3] slight change in argument formatting --- internal/multigitter/cmd.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/multigitter/cmd.go b/internal/multigitter/cmd.go index 3d14fec8..aee9dfae 100644 --- a/internal/multigitter/cmd.go +++ b/internal/multigitter/cmd.go @@ -8,11 +8,13 @@ import ( "os/exec" ) -func prepareScriptCommand(ctx context.Context, +func prepareScriptCommand( + ctx context.Context, repo scm.Repository, workDir string, scriptPath string, - arguments []string) (cmd *exec.Cmd) { + 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...)