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

Picking up values from environment not working #47

Closed
algermissen opened this issue Oct 11, 2017 · 3 comments · Fixed by #59
Closed

Picking up values from environment not working #47

algermissen opened this issue Oct 11, 2017 · 3 comments · Fixed by #59

Comments

@algermissen
Copy link

algermissen commented Oct 11, 2017

Hi,

I am trying to use the environment only, to grep the configuration. Because that did not work, I also tried with an additional Settings.toml. Then the value from file is picked up, but not the environment.

Code:

let mut settings = config::Config::default();
settings
   .merge(config::File::with_name("Settings"))
    .unwrap()
    .merge(config::Environment::with_prefix("APP"))
    .unwrap();

let x = settings.get::<String>("foo").expect("ok");
println!("CFG: {:?}", x);

Settings.toml:
foo = "20"

Calling the program with APP_FOO=10 target/debug/prog. Only the value from toml is picked up.

I must be doing something very stupid, but I cannot get this to work. Can you spot my error?

UPDATE Also tried the simple example, with APP_DEBUG=FOO without success. The toml file wins.

The crazy thing is that to me the code in Environment looks fine, I am just not getting what is going on.

UPDATE
See new comment #47 (comment) below.

@algermissen
Copy link
Author

algermissen commented Oct 11, 2017

It works with the latest version, which I did not quite have - I had 0,.6 from the github README.

I also checked with the 0.7.0 via crates.io. Also works.

One thing though, that caused trouble for my brain at least: The simple example is broken, because the "APP" prefix[1] used there will not match because it is not lowercased before matching with the already lc-ed key.

[1]

.merge(config::Environment::with_prefix("APP")).unwrap();

Lowercasing should be here

prefix: Some(s.into()),

@algermissen
Copy link
Author

I found one more issue with reading the environment. The separator (default '_') is not split once, but several times. For example, if you have APP_CACHE_SIZE in the environment, you do not get a config var "cache_size", but two vars, 'cache' and 'size' (I guess in a hierarchy, but I did not check that).

I found this counter-intuitive when working with env vars.

If this is not a bug, but intended behaviour, a special note in the docs would help avoid this interpretation.

@mehcode
Copy link
Owner

mehcode commented Jan 26, 2018

It was intended. A key like APP_CACHE_SIZE would become cache.size. The idea was to override structured configuration you might have.

I don't want to remove this entirely (as done in #56) but I'd be okay with disabling it by default and having perhaps:

Environment::with_prefix("APP").separator("_")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants