From a57f8286527d3a5de2dc3874305feab3f8727d8c Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Fri, 29 May 2020 22:07:00 +0200 Subject: [PATCH] Inject CONFIG env variable when executing Shell and Script extensions. Fix #277 --- src/extension/script.rs | 9 +++++++-- src/extension/shell.rs | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/extension/script.rs b/src/extension/script.rs index 875abe6..e25a6b1 100644 --- a/src/extension/script.rs +++ b/src/extension/script.rs @@ -75,10 +75,15 @@ impl super::Extension for ScriptExtension { } }); + let mut command = Command::new(&str_args[0]); + + // Inject the $CONFIG variable + command.env("CONFIG", crate::context::get_config_dir()); + let output = if str_args.len() > 1 { - Command::new(&str_args[0]).args(&str_args[1..]).output() + command.args(&str_args[1..]).output() } else { - Command::new(&str_args[0]).output() + command.output() }; match output { diff --git a/src/extension/shell.rs b/src/extension/shell.rs index b06c1ff..7ffcffa 100644 --- a/src/extension/shell.rs +++ b/src/extension/shell.rs @@ -40,15 +40,38 @@ pub enum Shell { impl Shell { fn execute_cmd(&self, cmd: &str) -> std::io::Result { - match self { - Shell::Cmd => Command::new("cmd").args(&["/C", &cmd]).output(), - Shell::Powershell => Command::new("powershell") - .args(&["-Command", &cmd]) - .output(), - Shell::WSL => Command::new("wsl").args(&["bash", "-c", &cmd]).output(), - Shell::Bash => Command::new("bash").args(&["-c", &cmd]).output(), - Shell::Sh => Command::new("sh").args(&["-c", &cmd]).output(), - } + let mut command = match self { + Shell::Cmd => { + let mut command = Command::new("cmd"); + command.args(&["/C", &cmd]); + command + }, + Shell::Powershell => { + let mut command = Command::new("powershell"); + command.args(&["-Command", &cmd]); + command + }, + Shell::WSL => { + let mut command = Command::new("wsl"); + command.args(&["bash", "-c", &cmd]); + command + }, + Shell::Bash => { + let mut command = Command::new("bash"); + command.args(&["-c", &cmd]); + command + }, + Shell::Sh => { + let mut command = Command::new("sh"); + command.args(&["-c", &cmd]); + command + }, + }; + + // Inject the $CONFIG variable + command.env("CONFIG", crate::context::get_config_dir()); + + command.output() } fn from_string(shell: &str) -> Option {