Skip to content

Commit bf2ff68

Browse files
committed
Add BuildOptions to buildutils.Build
1 parent b32f665 commit bf2ff68

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

buildutils/buildutils.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,73 @@ func (b *Builder) execCommand(name string, args ...string) error {
4545
return nil
4646
}
4747

48+
// ModMode is the module download mode to use.
49+
type ModMode string
50+
51+
const (
52+
// ModModeVendor causes modules to be resolved from a vendor folder.
53+
ModModeVendor = "vendor"
54+
// ModModeReadonly expect all modules to be present in the module cache for the current module.
55+
ModModeReadonly = "readonly"
56+
// ModModeMod fetches any module before building.
57+
ModModeMod = "mod"
58+
)
59+
60+
// BuildOptions are options to supply for a Build.
61+
type BuildOptions struct {
62+
// ForceRebuild forces rebuilding of packages that are already up-to-date.
63+
ForceRebuild bool
64+
// Mod specifies the module download mode to use.
65+
Mod *ModMode
66+
}
67+
68+
// ApplyOptions applies the slice of BuildOption to this BuildOptions.
69+
func (o *BuildOptions) ApplyOptions(opts []BuildOption) {
70+
for _, opt := range opts {
71+
opt.ApplyToBuild(o)
72+
}
73+
}
74+
75+
// ApplyToBuild implements BuildOption.
76+
func (o *BuildOptions) ApplyToBuild(o2 *BuildOptions) {
77+
if o.ForceRebuild {
78+
o2.ForceRebuild = true
79+
}
80+
if o.Mod != nil {
81+
o2.Mod = o.Mod
82+
}
83+
}
84+
85+
// BuildOption are options to apply to BuildOptions.
86+
type BuildOption interface {
87+
// ApplyToBuild applies the option to the BuildOptions.
88+
ApplyToBuild(o *BuildOptions)
89+
}
90+
4891
// Build runs `go build` with the target output and name.
4992
// If BuilderOptions.Tidy was set, it runs `go mod tidy` beforehand.
50-
func (b *Builder) Build(name, filename string) error {
93+
func (b *Builder) Build(name, filename string, opts ...BuildOption) error {
94+
o := &BuildOptions{}
95+
o.ApplyOptions(opts)
96+
5197
if b.tidy {
5298
if err := b.execCommand("go", "mod", "tidy"); err != nil {
5399
return fmt.Errorf("error tidying: %w", err)
54100
}
55101
}
56102

57-
if err := b.execCommand("go", "build", "-o", filename, name); err != nil {
103+
args := []string{"build", "-o", filename}
104+
105+
if mod := o.Mod; mod != nil {
106+
args = append(args, "-mod", string(*mod))
107+
}
108+
if o.ForceRebuild {
109+
args = append(args, "-a")
110+
}
111+
112+
args = append(args, name)
113+
114+
if err := b.execCommand("go", args...); err != nil {
58115
return fmt.Errorf("error building: %w", err)
59116
}
60117
return nil

tools.go renamed to hack/tools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
//go:build tools
1616

17-
package utils
17+
package hack
1818

1919
import (
2020
// Use mockgen for generating mocks.

0 commit comments

Comments
 (0)