Skip to content

Commit 0077504

Browse files
codebestiaddoktorskifranciszekjob
authored
Add support for curly brackets when resolving envs (#2868)
<!-- Reference any GitHub issues resolved by this PR --> Closes #2851 ## Introduced changes <!-- A brief description of the changes --> - Added implementation for resolving environmental variables with curly braces ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md` --------- Co-authored-by: ddoktorski <[email protected]> Co-authored-by: Franciszek Job <[email protected]>
1 parent 0ef8c41 commit 0077504

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

crates/configuration/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ fn resolve_env_variables(config: serde_json::Value) -> Result<serde_json::Value>
122122

123123
fn resolve_env_variable(var: &str) -> Result<serde_json::Value> {
124124
assert!(var.starts_with('$'));
125-
let value = env::var(&var[1..])?;
125+
let mut initial_value = &var[1..];
126+
if initial_value.starts_with('{') && initial_value.ends_with('}') {
127+
initial_value = &initial_value[1..initial_value.len() - 1];
128+
}
129+
let value = env::var(initial_value)?;
130+
126131
if let Ok(value) = value.parse::<Number>() {
127132
return Ok(serde_json::Value::Number(value));
128133
}
@@ -308,6 +313,8 @@ mod tests {
308313
list_example: Vec<bool>,
309314
#[serde(default, rename(serialize = "url-nested", deserialize = "url-nested"))]
310315
url_nested: f32,
316+
#[serde(default, rename(serialize = "url-alt", deserialize = "url-alt"))]
317+
url_alt: String,
311318
}
312319

313320
impl Config for StubComplexConfig {
@@ -355,6 +362,7 @@ mod tests {
355362

356363
// present env variables
357364
env::set_var("VALUE_STRING123132", "nfsaufbnsailfbsbksdabfnkl");
365+
env::set_var("VALUE_STRING123142", "nfsasnsidnnsailfbsbksdabdkdkl");
358366
env::set_var("VALUE_INT123132", "321312");
359367
env::set_var("VALUE_FLOAT123132", "321.312");
360368
env::set_var("VALUE_BOOL1231321", "true");
@@ -368,5 +376,9 @@ mod tests {
368376
assert_eq!(config.account, 321_312);
369377
assert_eq!(config.nested.list_example, vec![true, false]);
370378
assert_eq!(config.nested.url_nested, 321.312);
379+
assert_eq!(
380+
config.nested.url_alt,
381+
String::from("nfsasnsidnnsailfbsbksdabdkdkl")
382+
);
371383
}
372384
}

crates/configuration/tests/data/stubtool_snfoundry.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ account = "$VALUE_INT123132"
3232
[stubtool.with-envs.nested]
3333
list-example = [ "$VALUE_BOOL1231321", "$VALUE_BOOL1231322" ]
3434
url-nested = "$VALUE_FLOAT123132"
35+
url-alt = "${VALUE_STRING123142}"

docs/src/projects/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ the configuration from the local file will be used to override the global `defau
160160

161161
## Environmental Variables
162162

163-
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 `$`.
163+
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`).
164164
This might be useful, for example, to hide node urls in the public repositories.
165165
As an example:
166166

0 commit comments

Comments
 (0)