Skip to content

Commit

Permalink
go goextensions/builder: allow configuration of additional shell.RunO…
Browse files Browse the repository at this point in the history
…ptions

This is useful for overriding things such as the PATH environment to change the go binary used to specific versions.
  • Loading branch information
jsm committed Jan 13, 2025
1 parent b358b1d commit 990976f
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions goextensions/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ type GoBuilder struct {
err error

// Options:
LogToStdout bool
ModuleRoot string
LogToStdout bool
ModuleRoot string
ShellRunOptions []shell.RunOption
}

func NewGoBuilder() *GoBuilder {
Expand Down Expand Up @@ -113,13 +114,15 @@ func (b *GoBuilder) build() {
}

fmt.Fprintf(stdout, "building packages: %s", pkgList)
b.err = shell.Run(b.ctx, fmt.Sprintf("go install -v %s", pkgList), func(r *interp.Runner) {
shellRunOptions := []shell.RunOption{func(r *interp.Runner) {
r.Stdout = stdout
r.Stderr = stderr
if b.ModuleRoot != "" {
r.Dir = b.ModuleRoot
}
})
}}
shellRunOptions = append(shellRunOptions, b.ShellRunOptions...)
b.err = shell.Run(b.ctx, fmt.Sprintf("go install -v %s", pkgList), shellRunOptions...)
fmt.Fprintln(stdout, "done building packages")

close(b.doneCh)
Expand Down Expand Up @@ -225,6 +228,8 @@ func (builder *GoBuilder) WrapWithGoBuild(pkg string) taskrunner.TaskOption {
// be thrown in the from the registry when the task is added.
if task.Run != nil {
newTask.Run = func(ctx context.Context, shellRun shell.ShellRun) error {
shellRun = injectShellRunOptions(shellRun, builder.ShellRunOptions)

if err := builder.Build(ctx, shellRun, pkg); err != nil {
return err
}
Expand All @@ -241,6 +246,8 @@ func (builder *GoBuilder) WrapWithGoBuild(pkg string) taskrunner.TaskOption {
}
} else {
newTask.RunWithFlags = func(ctx context.Context, shellRun shell.ShellRun, flags map[string]taskrunner.FlagArg) error {
shellRun = injectShellRunOptions(shellRun, builder.ShellRunOptions)

if err := builder.Build(ctx, shellRun, pkg); err != nil {
return err
}
Expand Down Expand Up @@ -268,3 +275,14 @@ func (builder *GoBuilder) WrapWithGoBuild(pkg string) taskrunner.TaskOption {
return &newTask
}
}

// injectShellRunOptions injects additional options into shellRun.
func injectShellRunOptions(shellRun shell.ShellRun, additionalOptions []shell.RunOption) shell.ShellRun {
return func(ctx context.Context, command string, opts ...shell.RunOption) error {
// Create a new options slice combining the builder options with the ones passed in.
var optionsWithBuilderOptions []shell.RunOption
optionsWithBuilderOptions = append(optionsWithBuilderOptions, opts...)
optionsWithBuilderOptions = append(optionsWithBuilderOptions, additionalOptions...)
return shellRun(ctx, command, optionsWithBuilderOptions...)
}
}

0 comments on commit 990976f

Please sign in to comment.