|
@ -3,12 +3,14 @@ use once_cell::unsync::Lazy; |
|
|
use regex::Regex;
|
|
|
use regex::Regex;
|
|
|
use std::env;
|
|
|
use std::env;
|
|
|
|
|
|
|
|
|
const ENV_SET: Lazy<Regex> = Lazy::new(|| Regex::new(r#"(?P<key>\w+)=(?P<value>\w*)"#).unwrap());
|
|
|
|
|
|
|
|
|
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());
|
|
|
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(Some(...))` if a command should be triggered after calling this functions. If it
|
|
|
/// returns `Ok(None)`, do not execute a command.
|
|
|
/// returns `Ok(None)`, do not execute a command.
|
|
|
pub fn preprocess(mut line: String) -> Result<Option<String>, ShellError> {
|
|
|
pub fn preprocess(mut line: String) -> Result<Option<String>, 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) {
|
|
|
if let Some(capture) = ENV_SET.captures(&line) {
|
|
|
let key = capture.name("key").unwrap().as_str();
|
|
|
let key = capture.name("key").unwrap().as_str();
|
|
|
let value = capture
|
|
|
let value = capture
|
|
@ -19,6 +21,9 @@ pub fn preprocess(mut line: String) -> Result<Option<String>, ShellError> { |
|
|
return Ok(None);
|
|
|
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()) {
|
|
|
for capture in ENV_VARIABLE.captures_iter(&line.clone()) {
|
|
|
let variable_name = capture.name("env").unwrap().as_str();
|
|
|
let variable_name = capture.name("env").unwrap().as_str();
|
|
|
if variable_name.is_empty() {
|
|
|
if variable_name.is_empty() {
|
|
|