Inject Env Variables in WSL shell command

This commit is contained in:
Federico Terzi 2020-08-09 19:20:33 +02:00
parent 45f90c87ed
commit 316ebbb502

View File

@ -43,6 +43,8 @@ pub enum Shell {
impl Shell {
fn execute_cmd(&self, cmd: &str, vars: &HashMap<String, String>) -> std::io::Result<Output> {
let mut is_wsl = false;
let mut command = match self {
Shell::Cmd => {
let mut command = Command::new("cmd");
@ -55,11 +57,13 @@ impl Shell {
command
}
Shell::WSL => {
is_wsl = true;
let mut command = Command::new("bash");
command.args(&["-c", &cmd]);
command
}
Shell::WSL2 => {
is_wsl = true;
let mut command = Command::new("wsl");
command.args(&["bash", "-c", &cmd]);
command
@ -84,6 +88,22 @@ impl Shell {
command.env(key, value);
}
// In WSL environment, we have to specify which ENV variables
// should be passed to linux.
// For more information: https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/
if is_wsl {
let mut tokens: Vec<&str> = Vec::new();
tokens.push("CONFIG/p");
// Add all the previous variables
for (key, _) in vars.iter() {
tokens.push(key);
}
let wsl_env = tokens.join(":");
command.env("WSLENV", wsl_env);
}
command.output()
}