Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check duplicate flag #98

Merged
merged 3 commits into from Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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