-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Binding with cobra flag results on IsSet always true #276
Comments
I just ran into this as well. My temporary solution is to not find the particular value I need to check IsSet on, and did a custom function that first checks if the flag is set, then checks if the viper.IsSet is set on it. IMO, viper.IsSet() is not doing the right thing here. |
I saw this problem too! I used My workaround is to substitute |
I believe you are correct. It looks like e3bc06f added the behaviour you would expect, then ec4eb2f broke it again. @benoitmasson Is there a reason IsSet() behaviour changed with this commit? Or was it an unintended side affect? |
Hi, Sorry about that, this is clearly a side effect of my changes. And there should be a test for this specific case (or However, the old code if val == nil {
source := v.find(strings.ToLower(path[0]))
if source != nil {
if reflect.TypeOf(source).Kind() == reflect.Map {
val = v.searchMap(cast.ToStringMap(source), path[1:])
}
}
} cannot be restored as such, because it does not work properly with nested values (the I'll have a look and see if I can do something. |
I added some tests to v.ReadConfig(bytes.NewBuffer(yamlExample)) // defines "eyes"
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.BindEnv("eyes")
v.BindEnv("foo")
v.BindEnv("clothing.hat")
v.BindEnv("clothing.hats")
os.Setenv("FOO", "bar")
os.Setenv("CLOTHING_HAT", "bowler")
assert.True(t, v.IsSet("eyes")) // set in the config file
assert.True(t, v.IsSet("foo")) // set in the environment
assert.True(t, v.IsSet("clothing.hat")) // set in the environment
assert.False(t, v.IsSet("clothing.hats")) // bound but not set ok github.com/spf13/viper 0.014s Did I get something wrong? Or could you be more precise about the issue you are facing? |
How about this:
It seems to be tied to binding a flag. |
Ooops, I must be tired, I read “flags” and tested “environment” :-( Thanks for the precision. I see where this comes from now. If a flag is bound to Viper, a default value is automatically assigned to it, hence I will look for a fix (probably inside the |
@benoitmasson thank you for fixing this, it is causing me to use some workarounds that I can get rid of. |
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
#331 Still not accepted? |
Any idea when #331 will get accepted? |
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
The IsSet function returns true even if the value that's available comes from the default. Rather than changing the behavior of IsSet, adding a new IsExplicit function allows checking for non-default values without breaking existing behavior. This is intended to address issue spf13#276 among other things.
The IsSet function returns true even if the value that's available comes from the default. Rather than changing the behavior of IsSet, adding a new IsExplicit function allows checking for non-default values without breaking existing behavior. This is intended to address issue spf13#276 among other things.
The IsSet function returns true even if the value that's available comes from the default. Rather than changing the behavior of IsSet, adding a new IsExplicit function allows checking for non-default values without breaking existing behavior. This is intended to address issue spf13#276 among other things.
The IsSet function returns true even if the value that's available comes from the default. Rather than changing the behavior of IsSet, adding a new IsExplicit function allows checking for non-default values without breaking existing behavior. This is intended to address issue spf13#276 among other things.
The IsSet function returns true even if the value that's available comes from the default. Rather than changing the behavior of IsSet, adding a new IsExplicit function allows checking for non-default values without breaking existing behavior. This is intended to address issue spf13#276 among other things.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue spf13#276.
Default value should be looked for by Get(), but not by IsSet(). This logic should remain inside find(), to make sure that shadowing of keys is handled properly. Fixes Issue #276.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
In some places we need to know whether a flag has been explicitly set. When it was just flags, `pflag.Changed` served this purpose. But `viper.IsSet`, which is supposed to do the same thing over flags, config files, and environment entries, is broken: spf13/viper#276. Therefore: I've made my own isSet predicate, which checks the flags, config and environment individually. Since we can't actually check the environment separately, it has to construct a viper value that _only_ looks at the env, and check that.
When I bind a key to a cobra flag, when I ask IsSet I always get true, even if the flag was not set in the command line flags as well.
Do I miss something or this is a real issue?
The text was updated successfully, but these errors were encountered: