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

Weird behavior when mixing backends #77

Open
waltton opened this issue Sep 30, 2019 · 4 comments
Open

Weird behavior when mixing backends #77

waltton opened this issue Sep 30, 2019 · 4 comments
Labels

Comments

@waltton
Copy link

waltton commented Sep 30, 2019

Some examples of the weird behavior when mixing flag and envvar:

❌ bar default overwrite bar value from envvar

$ BAR=bar-envvar ./test-confita -foo=foo-flag
2019/09/29 10:24:37 cfg.Foo: 'foo-flag'
2019/09/29 10:24:37 cfg.Bar: 'bar-default'

✅ foo value from envvar it's overwritten by flag as expected

$ FOO=foo-envvar ./test-confita -foo=foo-flag
2019/09/29 10:29:22 cfg.Foo: 'foo-flag'
2019/09/29 10:29:22 cfg.Bar: 'bar-default'

❌ foo keeps the value from envvar ignoring the flag

$ BAR=bar-envvar FOO=foo-envvar ./test-confita -foo=foo-flag
2019/09/29 10:25:33 cfg.Foo: 'foo-envvar'
2019/09/29 10:25:33 cfg.Bar: 'bar-envvar'

go code:

package main

import (
	"context"
	"log"

	"github.com/heetch/confita"
	"github.com/heetch/confita/backend/env"
	"github.com/heetch/confita/backend/flags"
)

type Config struct {
	Bar string `config:"bar"`
	Foo string `config:"foo"`
}

func main() {
	cfg := Config{
		Bar: "bar-default",
	}

	loader := confita.NewLoader(
		env.NewBackend(),
		flags.NewBackend(),
	)

	err := loader.Load(context.Background(), &cfg)
	if err != nil {
		panic(err)
	}

	log.Printf("cfg.Foo: '%s'\n", cfg.Foo)
	log.Printf("cfg.Bar: '%s'\n", cfg.Bar)
}
@steamrolla
Copy link
Contributor

Can you post the version of confita you are using from go.mod?

@waltton
Copy link
Author

waltton commented Oct 1, 2019

I'm using v0.8.0

@tealeg tealeg added the bug label Oct 1, 2019
@tealeg
Copy link
Contributor

tealeg commented Oct 1, 2019

We're slowly working on a bit of a redesign of confita that might address some of these issues.

@zoido
Copy link

zoido commented Oct 30, 2019

It seems like it caused by

func isFlagSet(config *confita.FieldConfig) bool {
flagset := make(map[*confita.FieldConfig]bool)
flag.Visit(func(f *flag.Flag) { flagset[config] = true })
_, ok := flagset[config]
return ok
}

If I read it well, the implementation causes that when one flag is set isFlagSet is true for any input and therefore for all of the config values.

@waltton's 1st case is then cased by the fact that default values are used as defaults for the flag definitions

@tealeg Is it worth to try to fix this in the current state in the context of the mentioned upcoming redesign?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants