Skip to content

Commit

Permalink
Add support for curly brackets when resolving envs (foundry-rs#2868)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes foundry-rs#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 <45050160+ddoktorski@users.noreply.github.com>
Co-authored-by: Franciszek Job <54181625+franciszekjob@users.noreply.github.com>
  • Loading branch information
3 people authored and Nemezjusz committed Feb 2, 2025
1 parent 6030e6e commit 0e204d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
14 changes: 13 additions & 1 deletion crates/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ fn resolve_env_variables(config: serde_json::Value) -> Result<serde_json::Value>

fn resolve_env_variable(var: &str) -> Result<serde_json::Value> {
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::<Number>() {
return Ok(serde_json::Value::Number(value));
}
Expand Down Expand Up @@ -308,6 +313,8 @@ mod tests {
list_example: Vec<bool>,
#[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 {
Expand Down Expand Up @@ -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");
Expand All @@ -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")
);
}
}
1 change: 1 addition & 0 deletions crates/configuration/tests/data/stubtool_snfoundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
2 changes: 1 addition & 1 deletion docs/src/projects/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 0e204d6

Please sign in to comment.