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 { impl Shell {
fn execute_cmd(&self, cmd: &str, vars: &HashMap<String, String>) -> std::io::Result<Output> { fn execute_cmd(&self, cmd: &str, vars: &HashMap<String, String>) -> std::io::Result<Output> {
let mut is_wsl = false;
let mut command = match self { let mut command = match self {
Shell::Cmd => { Shell::Cmd => {
let mut command = Command::new("cmd"); let mut command = Command::new("cmd");
@ -55,11 +57,13 @@ impl Shell {
command command
} }
Shell::WSL => { Shell::WSL => {
is_wsl = true;
let mut command = Command::new("bash"); let mut command = Command::new("bash");
command.args(&["-c", &cmd]); command.args(&["-c", &cmd]);
command command
} }
Shell::WSL2 => { Shell::WSL2 => {
is_wsl = true;
let mut command = Command::new("wsl"); let mut command = Command::new("wsl");
command.args(&["bash", "-c", &cmd]); command.args(&["bash", "-c", &cmd]);
command command
@ -84,6 +88,22 @@ impl Shell {
command.env(key, value); 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() command.output()
} }