|
|
@ -4,10 +4,11 @@ use regex::Regex; |
|
|
|
use std::env;
|
|
|
|
|
|
|
|
const ENV_SET: Lazy<Regex> = Lazy::new(|| Regex::new(r#"(?P<key>\w+)=(?P<value>\w*)"#).unwrap());
|
|
|
|
const ENV_VARIABLE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\\\$|\$(?P<env>\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(line: String) -> Result<Option<String>, ShellError> {
|
|
|
|
pub fn preprocess(mut line: String) -> Result<Option<String>, ShellError> {
|
|
|
|
if let Some(capture) = ENV_SET.captures(&line) {
|
|
|
|
let key = capture.name("key").unwrap().as_str();
|
|
|
|
let value = capture
|
|
|
@ -18,6 +19,16 @@ pub fn preprocess(line: String) -> Result<Option<String>, ShellError> { |
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
for capture in ENV_VARIABLE.captures_iter(&line.clone()) {
|
|
|
|
let variable_name = capture.name("env").unwrap().as_str();
|
|
|
|
if variable_name.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let value = env::var(variable_name).unwrap_or_default();
|
|
|
|
|
|
|
|
line = ENV_VARIABLE.replacen(&line, 1, &value).to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(line))
|
|
|
|
}
|
|
|
|
|
|
|
|