Skip to content

Commit

Permalink
Check duplicate flag (#98)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kovalov <oleg@hey.com>
  • Loading branch information
cristaloleg committed Aug 15, 2021
1 parent 4599e98 commit de23397
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
22 changes: 16 additions & 6 deletions aconfig.go
Expand Up @@ -25,6 +25,7 @@ type Loader struct {
dst interface{}
fields []*fieldData
flagSet *flag.FlagSet
errInit error
}

// Config to configure configuration loader.
Expand Down Expand Up @@ -156,11 +157,17 @@ func (l *Loader) init() {

l.flagSet = flag.NewFlagSet(l.config.FlagPrefix, flag.ContinueOnError)
if !l.config.SkipFlags {
names := make(map[string]bool, len(l.fields))
for _, field := range l.fields {
flagName := l.fullTag(l.config.FlagPrefix, field, flagNameTag)
if flagName == "" {
continue
}
if names[flagName] && !l.config.AllowDuplicates {
l.errInit = fmt.Errorf("duplicate flag %q", flagName)
return
}
names[flagName] = true
l.flagSet.String(flagName, field.Tag(defaultValueTag), field.Tag(usageTag))
}
}
Expand Down Expand Up @@ -188,6 +195,9 @@ func (l *Loader) WalkFields(fn func(f Field) bool) {

// Load configuration into a given param.
func (l *Loader) Load() error {
if l.errInit != nil {
return fmt.Errorf("aconfig: cannot init loader: %w", l.errInit)
}
if err := l.loadConfig(); err != nil {
return fmt.Errorf("aconfig: cannot load config: %w", err)
}
Expand Down Expand Up @@ -370,12 +380,12 @@ func (l *Loader) loadEnvironment() error {
}

func (l *Loader) postEnvCheck(values map[string]interface{}, dupls map[string]struct{}) error {
for name := range dupls {
delete(values, name)
}
if l.config.AllowUnknownEnvs || l.config.EnvPrefix == "" {
return nil
}
for name := range dupls {
delete(values, name)
}
for env, value := range values {
if strings.HasPrefix(env, l.config.EnvPrefix) {
return fmt.Errorf("unknown environment var %s=%v (see AllowUnknownEnvs config param)", env, value)
Expand All @@ -402,12 +412,12 @@ func (l *Loader) loadFlags() error {
}

func (l *Loader) postFlagCheck(values map[string]interface{}, dupls map[string]struct{}) error {
for name := range dupls {
delete(values, name)
}
if l.config.AllowUnknownFlags || l.config.FlagPrefix == "" {
return nil
}
for name := range dupls {
delete(values, name)
}
for flag, value := range values {
if strings.HasPrefix(flag, l.config.EnvPrefix) {
return fmt.Errorf("unknown flag %s=%v (see AllowUnknownFlags config param)", flag, value)
Expand Down
14 changes: 14 additions & 0 deletions aconfig_test.go
Expand Up @@ -544,6 +544,20 @@ func TestFailOnDuplicatedName(t *testing.T) {
}
}

func TestFailOnDuplicatedFlag(t *testing.T) {
type Foo struct {
Bar string `flag:"yes"`
Baz string `flag:"yes"`
}

err := LoaderFor(&Foo{}, Config{}).Load()

want := `aconfig: cannot init loader: duplicate flag "yes"`
if got := err.Error(); got != want {
t.Fatalf("got %s want %s", got, want)
}
}

func TestUsage(t *testing.T) {
loader := LoaderFor(&EmbeddedConfig{}, Config{})

Expand Down

0 comments on commit de23397

Please sign in to comment.