Closed callebjorkell closed 5 months ago
I was able to work around this by passing a callback to the load, rewriting the key value to use dots instead to align with what has already been loaded into the koanf map:
err = k.Load(posflag.ProviderWithValue(cmd.Flags(), ".", k, func(key string, value string) (string, interface{}) {
return strings.ReplaceAll(key, "-", "."), value
}), nil)
In this way, the overrides work as expected. I assume that wrapping the koanf instance passed to replace the key before checking if it exists would also work, but I haven't tried that.
Describe the bug When loading flags from a cobra application, if using
-
as the delimiter, existing loaded values are overridden by empty values.To Reproduce Given a specified, but unused, pflag
s3-bucket
and an environment variableMYAPP_S3_BUCKET="five"
, if I have the following config loading:k.String("s3.bucket")
will return""
when I have not set thes3-bucket
flag for the application, even though the environment variable has a value.Expected behavior
k.String("s3.bucket")
should return"five"
Please provide the following information):
Additional context I think this is because of the delimiter replacement happening only after the "existence check", so the provider is checking if "s3-bucket" exists as a key already in my koanf instance (which it doesn't) and then goes on to apply the delimiter (so "s3.bucket") before merging.