Skip to content

Commit

Permalink
Refactor go mod tidy to one place
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Feb 17, 2022
1 parent 70961eb commit 6f07696
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 1 addition & 3 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {

log.Println("[INFO] Building k6")

// tidy the module to ensure go.mod and go.sum are consistent with the module prereq
tidyCmd := buildEnv.newCommand("go", "mod", "tidy")
if err := buildEnv.runCommand(ctx, tidyCmd, b.TimeoutGet); err != nil {
if err := buildEnv.execGoModTidy(ctx); err != nil {
return err
}

Expand Down
16 changes: 9 additions & 7 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ func (env environment) runCommand(ctx context.Context, cmd *exec.Cmd, timeout ti
}
}

// tidy the module to ensure go.mod will not have versions such as `latest`
func (env environment) execGoModTidy(ctx context.Context) error {
tidyCmd := env.newCommand("go", "mod", "tidy")
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
}

func (env environment) execGoModRequire(ctx context.Context, modulePath, moduleVersion string) error {
mod := modulePath
if moduleVersion != "" {
Expand All @@ -256,21 +262,17 @@ func (env environment) execGoModRequire(ctx context.Context, modulePath, moduleV
if err != nil {
return err
}
// tidy the module to ensure go.mod will not have versions such as `latest`
tidyCmd := env.newCommand("go", "mod", "tidy")
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
return env.execGoModTidy(ctx)
}

func (env environment) execGoModReplace(ctx context.Context, modulePath, replaceRepo string) error {
replace := fmt.Sprintf("%s=%s", modulePath, replaceRepo)
cmd := env.newCommand("go", "mod", "edit", "-replace", replace)
err := env.runCommand(ctx, cmd, 10*time.Second)
err := env.runCommand(ctx, cmd, env.timeoutGoGet)
if err != nil {
return err
}
// tidy the module to ensure go.mod will not have versions such as `latest`
tidyCmd := env.newCommand("go", "mod", "tidy")
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
return env.execGoModTidy(ctx)
}

type goModTemplateContext struct {
Expand Down

0 comments on commit 6f07696

Please sign in to comment.