* Adjust environment variable regex to ${VAR} * Add replace_env_var_references test
This commit is contained in:
parent
0d2ee78f91
commit
8aa0e8c74d
1 changed files with 18 additions and 2 deletions
20
src/util.rs
20
src/util.rs
|
@ -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: ®ex::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())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue