Weird behavior when mixing backends
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)
}
Can you post the version of confita you are using from go.mod?
I'm using v0.8.0
We're slowly working on a bit of a redesign of confita that might address some of these issues.
It seems like it caused by https://github.com/heetch/confita/blob/86d03df7797f4103127132b69565dcaf0e6478e3/backend/flags/flags.go#L143-L149
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?