...

Source file src/edge-infra.dev/pkg/lib/build/bazel/config.go

Documentation: edge-infra.dev/pkg/lib/build/bazel

     1  package bazel
     2  
     3  import (
     4  	"flag"
     5  	"strings"
     6  
     7  	"edge-infra.dev/pkg/lib/cli/rags"
     8  )
     9  
    10  // Configs are Bazel configuration profiles (https://docs.bazel.build/versions/master/guide.html#--config)
    11  // that are passed to Bazel commands executed by [Bazel] which implements
    12  // behavior for binding a flag that allows binaries invoking Bazel to easily
    13  // pass one or more config profiles through (see [Bazel.RegisterFlags]).
    14  type Configs []string
    15  
    16  // String returns all configs joined by a comma.
    17  func (c Configs) String() string {
    18  	return strings.Join(c, ",")
    19  }
    20  
    21  // ToArgs produces the flags to pass to Bazel in order to activate the config
    22  // profiles.
    23  func (c Configs) ToArgs() []string {
    24  	args := make([]string, 0, len(c)*2)
    25  	for _, cfg := range c {
    26  		args = append(args, "--config", cfg)
    27  	}
    28  	return args
    29  }
    30  
    31  // Set implements flag.Value. It accepts comma separated strings and accumulates
    32  // values from repeated occurrences of the flag. It also handles edge cases
    33  // like trailing / leading commas and duplicate values.
    34  func (c *Configs) Set(v string) error {
    35  	tokens := strings.Split(strings.Trim(v, ","), ",")
    36  	nc := append(*c, tokens...)
    37  	j := 0
    38  	for i := 1; i < len(nc); i++ {
    39  		if nc[j] == nc[i] {
    40  			continue
    41  		}
    42  		j++
    43  		nc[j] = nc[i]
    44  	}
    45  
    46  	*c = nc[:j+1]
    47  	return nil
    48  }
    49  
    50  // Get implements [flag.Getter]
    51  func (c *Configs) Get() any {
    52  	return []string(*c)
    53  }
    54  
    55  // Type implements [edge-infra.dev/pkg/lib/cli/rags.TypedValue]
    56  func (c *Configs) Type() string {
    57  	return "config(s)"
    58  }
    59  
    60  // RegisterConfigsFlag registers a flag for setting Bazel config profiles
    61  func RegisterConfigsFlag(fs *flag.FlagSet) *Configs {
    62  	cfg := &Configs{}
    63  	fs.Var(cfg, "bazel-configs",
    64  		"comma separated list of values to pass to Bazel via --config. "+
    65  			"https://docs.bazel.build/versions/master/guide.html#--config",
    66  	)
    67  	return cfg
    68  }
    69  
    70  func RegisterConfigsRag(fs *rags.RagSet) *Configs {
    71  	cfg := &Configs{}
    72  	fs.Var(cfg, "bazel-configs",
    73  		"comma separated list of values to pass to Bazel via --config. "+
    74  			"https://docs.bazel.build/versions/master/guide.html#--config",
    75  		rags.WithCategory("bazel"),
    76  	)
    77  	return cfg
    78  }
    79  

View as plain text