config icon indicating copy to clipboard operation
config copied to clipboard

LoadOSEnvs not work

Open vipcxj opened this issue 2 years ago • 4 comments

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.

vipcxj avatar Oct 27 '23 05:10 vipcxj

What is your config package version?

inhere avatar Nov 08 '23 07:11 inhere

CONFIGURE.AuthServerEnable == true because the default:"true"

inhere avatar Nov 08 '23 07:11 inhere

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 avatar Nov 11 '23 12:11 ndopj

@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}"`
}

vipcxj avatar Feb 02 '24 13:02 vipcxj