Skip to content

Commit

Permalink
refactor: moved shared run/print code into function (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitram509 authored Apr 10, 2023
1 parent 451167c commit b250c9b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
26 changes: 26 additions & 0 deletions internal/multigitter/cmd.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 3 additions & 11 deletions internal/multigitter/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions internal/multigitter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit b250c9b

Please sign in to comment.