fix: allow overridding default string array (#6873)

* fix: allow overridding default string array

* Cleanup code

* fixup! Cleanup code

* fixup! Cleanup code

* fixup! Cleanup code

* fixup! Cleanup code
This commit is contained in:
Ammar Bandukwala
2023-03-29 20:09:20 -05:00
committed by GitHub
parent 1c7adc0ebd
commit 58d650c2bb
6 changed files with 91 additions and 107 deletions

View File

@ -46,6 +46,8 @@ type Option struct {
UseInstead []Option `json:"use_instead,omitempty"`
Hidden bool `json:"hidden,omitempty"`
envChanged bool
}
// OptionSet is a group of options that can be applied to a command.
@ -133,6 +135,7 @@ func (s *OptionSet) ParseEnv(vs []EnvVar) error {
continue
}
opt.envChanged = true
if err := opt.Value.Set(envVal); err != nil {
merr = multierror.Append(
merr, xerrors.Errorf("parse %q: %w", opt.Name, err),
@ -143,19 +146,27 @@ func (s *OptionSet) ParseEnv(vs []EnvVar) error {
return merr.ErrorOrNil()
}
// SetDefaults sets the default values for each Option.
// It should be called before all parsing (e.g. ParseFlags, ParseEnv).
func (s *OptionSet) SetDefaults() error {
// SetDefaults sets the default values for each Option, skipping values
// that have already been set as indicated by the skip map.
func (s *OptionSet) SetDefaults(skip map[int]struct{}) error {
if s == nil {
return nil
}
var merr *multierror.Error
for _, opt := range *s {
for i, opt := range *s {
// Skip values that may have already been set by the user.
if len(skip) > 0 {
if _, ok := skip[i]; ok {
continue
}
}
if opt.Default == "" {
continue
}
if opt.Value == nil {
merr = multierror.Append(
merr,