package bazel import ( "flag" "strings" "edge-infra.dev/pkg/lib/cli/rags" ) // Configs are Bazel configuration profiles (https://docs.bazel.build/versions/master/guide.html#--config) // that are passed to Bazel commands executed by [Bazel] which implements // behavior for binding a flag that allows binaries invoking Bazel to easily // pass one or more config profiles through (see [Bazel.RegisterFlags]). type Configs []string // String returns all configs joined by a comma. func (c Configs) String() string { return strings.Join(c, ",") } // ToArgs produces the flags to pass to Bazel in order to activate the config // profiles. func (c Configs) ToArgs() []string { args := make([]string, 0, len(c)*2) for _, cfg := range c { args = append(args, "--config", cfg) } return args } // Set implements flag.Value. It accepts comma separated strings and accumulates // values from repeated occurrences of the flag. It also handles edge cases // like trailing / leading commas and duplicate values. func (c *Configs) Set(v string) error { tokens := strings.Split(strings.Trim(v, ","), ",") nc := append(*c, tokens...) j := 0 for i := 1; i < len(nc); i++ { if nc[j] == nc[i] { continue } j++ nc[j] = nc[i] } *c = nc[:j+1] return nil } // Get implements [flag.Getter] func (c *Configs) Get() any { return []string(*c) } // Type implements [edge-infra.dev/pkg/lib/cli/rags.TypedValue] func (c *Configs) Type() string { return "config(s)" } // RegisterConfigsFlag registers a flag for setting Bazel config profiles func RegisterConfigsFlag(fs *flag.FlagSet) *Configs { cfg := &Configs{} fs.Var(cfg, "bazel-configs", "comma separated list of values to pass to Bazel via --config. "+ "https://docs.bazel.build/versions/master/guide.html#--config", ) return cfg } func RegisterConfigsRag(fs *rags.RagSet) *Configs { cfg := &Configs{} fs.Var(cfg, "bazel-configs", "comma separated list of values to pass to Bazel via --config. "+ "https://docs.bazel.build/versions/master/guide.html#--config", rags.WithCategory("bazel"), ) return cfg }