diff --git a/src/preprocess.rs b/src/preprocess.rs index 6ea4127..3a6a0cd 100644 --- a/src/preprocess.rs +++ b/src/preprocess.rs @@ -3,12 +3,14 @@ use once_cell::unsync::Lazy; use regex::Regex; use std::env; -const ENV_SET: Lazy = Lazy::new(|| Regex::new(r#"(?P\w+)=(?P\w*)"#).unwrap()); +const ENV_SET: Lazy = Lazy::new(|| Regex::new(r#"^(?P\w+)=(?P\w*)$"#).unwrap()); const ENV_VARIABLE: Lazy = Lazy::new(|| Regex::new(r"(\\\$|\$(?P\w+))").unwrap()); /// Returns `Ok(Some(...))` if a command should be triggered after calling this functions. If it /// returns `Ok(None)`, do not execute a command. pub fn preprocess(mut line: String) -> Result, ShellError> { + // check if the input line has `key=value` syntax. if so, set an env variable with `key` as key + // and `value` as value if let Some(capture) = ENV_SET.captures(&line) { let key = capture.name("key").unwrap().as_str(); let value = capture @@ -19,6 +21,9 @@ pub fn preprocess(mut line: String) -> Result, ShellError> { return Ok(None); } + // resolve env variables. if the input line contains `$key` and a env variable with the name + // `key` exists too, resolve `$key` to the variable value. if `key` env variable does not exist, + // simply resolve it with an empty string for capture in ENV_VARIABLE.captures_iter(&line.clone()) { let variable_name = capture.name("env").unwrap().as_str(); if variable_name.is_empty() {