Skip to content

Commit

Permalink
fix: use plugin version (#4655)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Apr 17, 2024
1 parent 3ba2604 commit 003b048
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions pkg/commands/internal/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -103,25 +103,56 @@ 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)
err := b.goGet(ctx, plugin)
if err != nil {
return err
}
}

return nil
}

cmd := exec.CommandContext(ctx, "go", "mod", "edit", "-replace", replace)
cmd.Dir = b.repo
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, " "))
b.log.Infof("run: %s", strings.Join(cmd.Args, " "))

output, err := cmd.CombinedOutput()
if err != nil {
b.log.Warnf(string(output))
output, err := cmd.CombinedOutput()
if err != nil {
b.log.Warnf(string(output))

return fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err)
}
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
Expand Down

0 comments on commit 003b048

Please sign in to comment.