diff --git a/crates/configuration/src/lib.rs b/crates/configuration/src/lib.rs index 3b163d3593..c552e57e8e 100644 --- a/crates/configuration/src/lib.rs +++ b/crates/configuration/src/lib.rs @@ -122,7 +122,12 @@ fn resolve_env_variables(config: serde_json::Value) -> Result fn resolve_env_variable(var: &str) -> Result { assert!(var.starts_with('$')); - let value = env::var(&var[1..])?; + let mut initial_value = &var[1..]; + if initial_value.starts_with('{') && initial_value.ends_with('}') { + initial_value = &initial_value[1..initial_value.len() - 1]; + } + let value = env::var(initial_value)?; + if let Ok(value) = value.parse::() { return Ok(serde_json::Value::Number(value)); } @@ -308,6 +313,8 @@ mod tests { list_example: Vec, #[serde(default, rename(serialize = "url-nested", deserialize = "url-nested"))] url_nested: f32, + #[serde(default, rename(serialize = "url-alt", deserialize = "url-alt"))] + url_alt: String, } impl Config for StubComplexConfig { @@ -355,6 +362,7 @@ mod tests { // present env variables env::set_var("VALUE_STRING123132", "nfsaufbnsailfbsbksdabfnkl"); + env::set_var("VALUE_STRING123142", "nfsasnsidnnsailfbsbksdabdkdkl"); env::set_var("VALUE_INT123132", "321312"); env::set_var("VALUE_FLOAT123132", "321.312"); env::set_var("VALUE_BOOL1231321", "true"); @@ -368,5 +376,9 @@ mod tests { assert_eq!(config.account, 321_312); assert_eq!(config.nested.list_example, vec![true, false]); assert_eq!(config.nested.url_nested, 321.312); + assert_eq!( + config.nested.url_alt, + String::from("nfsasnsidnnsailfbsbksdabdkdkl") + ); } } diff --git a/crates/configuration/tests/data/stubtool_snfoundry.toml b/crates/configuration/tests/data/stubtool_snfoundry.toml index 0cc19a3dc1..fee0f6a55e 100644 --- a/crates/configuration/tests/data/stubtool_snfoundry.toml +++ b/crates/configuration/tests/data/stubtool_snfoundry.toml @@ -32,3 +32,4 @@ account = "$VALUE_INT123132" [stubtool.with-envs.nested] list-example = [ "$VALUE_BOOL1231321", "$VALUE_BOOL1231322" ] url-nested = "$VALUE_FLOAT123132" +url-alt = "${VALUE_STRING123142}" diff --git a/docs/src/projects/configuration.md b/docs/src/projects/configuration.md index 0b435e8540..7fd5596b17 100644 --- a/docs/src/projects/configuration.md +++ b/docs/src/projects/configuration.md @@ -160,7 +160,7 @@ the configuration from the local file will be used to override the global `defau ## Environmental Variables -Programmers can use environmental variables in both `Scarb.toml::tool::snforge` and in `snfoundry.toml`. To use an environmental variable as a value, use its name prefixed with `$`. +Programmers can use environmental variables in both `Scarb.toml::tool::snforge` and in `snfoundry.toml`. To use an environmental variable as a value, use its name either with or without curly braces, prefixed with `$` (e.g. `${MY_ENV}` or `$MY_ENV`). This might be useful, for example, to hide node urls in the public repositories. As an example: