diff --git a/coffee_lib/src/macros.rs b/coffee_lib/src/macros.rs index e19911de..989f1335 100644 --- a/coffee_lib/src/macros.rs +++ b/coffee_lib/src/macros.rs @@ -15,31 +15,27 @@ macro_rules! error { macro_rules! sh { ($root: expr, $script:expr, $verbose:expr) => {{ let script = $script.trim(); - let cmds = script.split("\n"); // Check if the script contains `\` - log::debug!("cmds: {:?}", cmds); - for cmd in cmds { - log::debug!("cmd {:?}", cmd); - let cmd_tok: Vec<&str> = cmd.split(" ").collect(); - let command = cmd_tok.first().unwrap().to_string(); - let mut cmd = Command::new(command); - cmd.args(&cmd_tok[1..cmd_tok.len()]); - cmd.current_dir($root); - let command = if $verbose { - cmd.spawn() - .expect("Unable to run the command") - .wait_with_output() - .await? - } else { - cmd.output().await? - }; + log::debug!("script: {:?}", script); - if !command.status.success() { - let mut content = String::from_utf8(command.stderr).unwrap(); - if content.trim().is_empty() { - content = String::from_utf8(command.stdout).unwrap(); - } - return Err(CoffeeError::new(2, &content)); + let mut cmd = Command::new("sh"); + cmd.args(&["-c", &script]); + cmd.current_dir($root); + + let command = if $verbose { + cmd.spawn() + .map_err(|_| error!("Unable to run the command"))? + .wait_with_output() + .await? + } else { + cmd.output().await? + }; + + if !command.status.success() { + let mut content = String::from_utf8(command.stderr).unwrap(); + if content.trim().is_empty() { + content = String::from_utf8(command.stdout).unwrap(); } + return Err(CoffeeError::new(2, &content)); } }};