Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor redundant code into a function #339

Merged
merged 3 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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