Skip to content

Commit 0236649

Browse files
committed
Add --build-tags flag
1 parent 06ca07b commit 0236649

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

cmd/controller-gen/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"github.com/spf13/cobra"
27+
"golang.org/x/tools/go/packages"
2728

2829
"sigs.k8s.io/controller-tools/pkg/applyconfiguration"
2930
"sigs.k8s.io/controller-tools/pkg/crd"
@@ -129,6 +130,7 @@ func main() {
129130
helpLevel := 0
130131
whichLevel := 0
131132
showVersion := false
133+
var buildTags []string
132134

133135
cmd := &cobra.Command{
134136
Use: "controller-gen",
@@ -173,7 +175,8 @@ func main() {
173175
}
174176

175177
// otherwise, set up the runtime for actually running the generators
176-
rt, err := genall.FromOptions(optionsRegistry, rawOpts)
178+
tagsFlag := fmt.Sprintf("-tags=%s", strings.Join(buildTags, ","))
179+
rt, err := genall.FromOptionsWithConfig(&packages.Config{BuildFlags: []string{tagsFlag}}, optionsRegistry, rawOpts)
177180
if err != nil {
178181
return err
179182
}
@@ -192,6 +195,7 @@ func main() {
192195
cmd.Flags().CountVarP(&whichLevel, "which-markers", "w", "print out all markers available with the requested generators\n(up to -www for the most detailed output, or -wwww for json output)")
193196
cmd.Flags().CountVarP(&helpLevel, "detailed-help", "h", "print out more detailed help\n(up to -hhh for the most detailed output, or -hhhh for json output)")
194197
cmd.Flags().BoolVar(&showVersion, "version", false, "show version")
198+
cmd.Flags().StringSliceVar(&buildTags, "build-tags", nil, "build tags to use when loading Go packages")
195199
cmd.Flags().Bool("help", false, "print out usage and a summary of options")
196200
oldUsage := cmd.UsageFunc()
197201
cmd.SetUsageFunc(func(c *cobra.Command) error {

pkg/genall/genall.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ func (g GenerationContext) ReadFile(path string) ([]byte, error) {
218218
// ForRoots produces a Runtime to run the given generators against the
219219
// given packages. It outputs to /dev/null by default.
220220
func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
221-
roots, err := loader.LoadRoots(rootPaths...)
221+
return g.ForRootsWithConfig(&packages.Config{}, rootPaths...)
222+
}
223+
224+
func (g Generators) ForRootsWithConfig(cfg *packages.Config, rootPaths ...string) (*Runtime, error) {
225+
roots, err := loader.LoadRootsWithConfig(cfg, rootPaths...)
222226
if err != nil {
223227
return nil, err
224228
}

pkg/genall/options.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"strings"
2222

23+
"golang.org/x/tools/go/packages"
2324
"sigs.k8s.io/controller-tools/pkg/markers"
2425
)
2526

@@ -74,13 +75,17 @@ func RegistryFromOptions(optionsRegistry *markers.Registry, options []string) (*
7475
// further modified. Not default generators are used if none are specified -- you can check
7576
// the output and rerun for that.
7677
func FromOptions(optionsRegistry *markers.Registry, options []string) (*Runtime, error) {
78+
return FromOptionsWithConfig(&packages.Config{}, optionsRegistry, options)
79+
}
80+
81+
func FromOptionsWithConfig(cfg *packages.Config, optionsRegistry *markers.Registry, options []string) (*Runtime, error) {
7782
protoRt, err := protoFromOptions(optionsRegistry, options)
7883
if err != nil {
7984
return nil, err
8085
}
8186

8287
// make the runtime
83-
genRuntime, err := protoRt.Generators.ForRoots(protoRt.Paths...)
88+
genRuntime, err := protoRt.Generators.ForRootsWithConfig(cfg, protoRt.Paths...)
8489
if err != nil {
8590
return nil, err
8691
}

0 commit comments

Comments
 (0)