Change environment variable syntax to ${VAR} - Fix for #81 (#83)

* Adjust environment variable regex to ${VAR}

* Add replace_env_var_references test
This commit is contained in:
Jack Michaud 2021-01-02 08:49:32 -08:00 committed by GitHub
parent 0d2ee78f91
commit 8aa0e8c74d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -76,12 +76,12 @@ pub fn parse_duration(s: &str) -> Result<std::time::Duration> {
}
}
/// Replace all env-var references of the format `"something $foo"` in a string
/// Replace all env-var references of the format `"something ${foo}"` in a string
/// by the actual env-variables. If the env-var isn't found, will replace the
/// reference with an empty string.
pub fn replace_env_var_references(input: String) -> String {
lazy_static::lazy_static! {
static ref ENV_VAR_PATTERN: regex::Regex = regex::Regex::new(r"\$([^\s]*)").unwrap();
static ref ENV_VAR_PATTERN: regex::Regex = regex::Regex::new(r"\$\{([^\s]*)\}").unwrap();
}
ENV_VAR_PATTERN
.replace_all(&input, |var_name: &regex::Captures| {
@ -89,3 +89,19 @@ pub fn replace_env_var_references(input: String) -> String {
})
.into_owned()
}
#[cfg(test)]
mod test {
use super::replace_env_var_references;
use std;
#[test]
fn test_replace_env_var_references() {
let scss = "$test: ${USER};";
assert_eq!(
replace_env_var_references(String::from(scss)),
format!("$test: {};", std::env::var("USER").unwrap_or_default())
)
}
}