@@ -45,16 +45,73 @@ func (b *Builder) execCommand(name string, args ...string) error {
45
45
return nil
46
46
}
47
47
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
+
48
91
// Build runs `go build` with the target output and name.
49
92
// 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
+
51
97
if b .tidy {
52
98
if err := b .execCommand ("go" , "mod" , "tidy" ); err != nil {
53
99
return fmt .Errorf ("error tidying: %w" , err )
54
100
}
55
101
}
56
102
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 {
58
115
return fmt .Errorf ("error building: %w" , err )
59
116
}
60
117
return nil
0 commit comments