Skip to content
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

LoadOSEnvs not work #178

Open
vipcxj opened this issue Oct 27, 2023 · 4 comments
Open

LoadOSEnvs not work #178

vipcxj opened this issue Oct 27, 2023 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@vipcxj
Copy link
Contributor

vipcxj commented Oct 27, 2023

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.

@inhere
Copy link
Member

inhere commented Nov 8, 2023

What is your config package version?

@inhere
Copy link
Member

inhere commented Nov 8, 2023

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

@inhere inhere added the question Further information is requested label Nov 8, 2023
@ndopj
Copy link

ndopj commented Nov 11, 2023

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.

@vipcxj
Copy link
Contributor Author

vipcxj commented Feb 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants