LoadOSEnvs not work
System (please complete the following information):
- OS:
linux[e.g. linux, macOS] - GO Version:
1.13[e.g.1.13] - Pkg Version:
1.1.1[e.g.1.1.1]
Describe the bug
type ConferenceConfigure struct {
AuthServerEnable bool `mapstructure:"authServerEnable" default:"true"`
}
var ENVS = map[string]string{
"CONF_AUTH_SERVER_ENABLE": "authServerEnable",
}
config.WithOptions(config.ParseEnv, config.ParseTime, config.ParseDefault)
config.LoadOSEnvs(ENVS)
print(config.Bool("authServerEnable")) // false
err = config.LoadExists(XXX) // XXX not exists
print(config.Bool("authServerEnable")) // false
config.Decode(CONFIGURE)
print(config.Bool("authServerEnable")) // false
// but CONFIGURE.AuthServerEnable == true
A clear and concise description of what you expected to happen.
Screenshots
If applicable, add screenshots to help explain your problem.
Additional context
Add any other context about the problem here.
What is your config package version?
CONFIGURE.AuthServerEnable == true because the default:"true"
I had the same issue but was able to find what my problem was.
It seems that using config.ParseEnv expects some kind of environment variable to set environment for application (e.g. devel, staging, prod etc ...). When this environment variable is not set and while using environment variables names normally, configure.LoadOSEnvs() will not load environment variables at all. I think this behaviour is just poorly documented.
@vipcxj remove the configure.ParseEnv, run export CONF_AUTH_SERVER_ENABLE=false and after
config.LoadOSEnvs(ENVS)
configuration := ConferenceConfigure{}
configure.Decode(&configuration)
print(configuration.AuthServerEnable)
you should obtain false on the output. Note that print(config.Bool("authServerEnable")) // false was printing false to you because of default boolean value, not because it was loaded correctly from the env. variables.
@inhere I think the functionality is working correctly, just the documentation might be misleading and it would be beneficial if it would be explained more deeply what configure.ParseEnv does because lot of users might think it enables parsing of environment variables as I did as well.
@inhere Also I've found out that using configure.Readonly in the options prevents loading of environment variables via confgure.LoadOsEnvs(). I don't know whether it is intended behaviour but if it is I do not see any purpose in Readonly flag as it can be configured only before the data is loaded.
@ndopj Sorry for replying so late, I gave up using config.ParseEnv and now I read the environment variables via default tag like this:
type ConferenceConfigure struct {
AuthServerEnable bool `mapstructure:"authServerEnable" default:"${CONF_AUTH_SERVER_ENABLE | true}"`
}