diff --git a/pkg/commands/internal/builder.go b/pkg/commands/internal/builder.go index 39ec2a25..7253615a 100644 --- a/pkg/commands/internal/builder.go +++ b/pkg/commands/internal/builder.go @@ -53,9 +53,9 @@ func (b Builder) Build(ctx context.Context) error { b.log.Infof("Adding replace directives") - err = b.addReplaceDirectives(ctx) + err = b.addToGoMod(ctx) if err != nil { - return fmt.Errorf("add replace directives: %w", err) + return fmt.Errorf("add to go.mod: %w", err) } b.log.Infof("Running go mod tidy") @@ -103,30 +103,61 @@ func (b Builder) clone(ctx context.Context) error { return nil } -func (b Builder) addReplaceDirectives(ctx context.Context) error { +func (b Builder) addToGoMod(ctx context.Context) error { for _, plugin := range b.cfg.Plugins { - if plugin.Path == "" { + if plugin.Path != "" { + err := b.addReplaceDirective(ctx, plugin) + if err != nil { + return err + } + continue } - replace := fmt.Sprintf("%s=%s", plugin.Module, plugin.Path) - - cmd := exec.CommandContext(ctx, "go", "mod", "edit", "-replace", replace) - cmd.Dir = b.repo - - b.log.Infof("run: %s", strings.Join(cmd.Args, " ")) - - output, err := cmd.CombinedOutput() + err := b.goGet(ctx, plugin) if err != nil { - b.log.Warnf(string(output)) - - return fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err) + return err } } return nil } +func (b Builder) goGet(ctx context.Context, plugin *Plugin) error { + //nolint:gosec // the variables are user related. + cmd := exec.CommandContext(ctx, "go", "get", plugin.Module+"@"+plugin.Version) + cmd.Dir = b.repo + + b.log.Infof("run: %s", strings.Join(cmd.Args, " ")) + + output, err := cmd.CombinedOutput() + if err != nil { + b.log.Warnf(string(output)) + + return fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err) + } + + return nil +} + +func (b Builder) addReplaceDirective(ctx context.Context, plugin *Plugin) error { + replace := fmt.Sprintf("%s=%s", plugin.Module, plugin.Path) + + cmd := exec.CommandContext(ctx, "go", "mod", "edit", "-replace", replace) + cmd.Dir = b.repo + + b.log.Infof("run: %s", strings.Join(cmd.Args, " ")) + + output, err := cmd.CombinedOutput() + if err != nil { + b.log.Warnf(string(output)) + + return fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err) + } + + return nil +} + func (b Builder) goModTidy(ctx context.Context) error { cmd := exec.CommandContext(ctx, "go", "mod", "tidy") cmd.Dir = b.repo