use std::process::{Command, ExitStatus, Stdio};
fn bash_command(command: &str, live_output: bool) -> Command {
let mut cmd = Command::new("bash");
cmd.arg("-c").arg(command);
if live_output {
cmd
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
}
cmd
}
pub fn execute_cmd_stdio(command: &str, live_output: bool) -> String {
if live_output {
let status = bash_command(command, true).status();
match status {
Ok(s) if s.success() => "".to_string(),
Ok(s) => format!("process exited with status: {}", s),
Err(e) => format!("error executing command: {}", e),
}
} else {
match bash_command(command, false).output() {
Ok(output) => {
if output.status.success() {
String::from_utf8_lossy(&output.stdout).to_string()
} else {
String::from_utf8_lossy(&output.stderr).to_string()
}
}
Err(e) => format!("error executing command: {}", e),
}
}
}
pub fn execute_cmd_rc(command: &str, live_output: bool) -> i32 {
if live_output {
match bash_command(command, true).status() {
Ok(s) => s.code().unwrap_or(-1),
Err(_) => -1,
}
} else {
match bash_command(command, false).output() {
Ok(output) => output.status.code().unwrap_or(-1),
Err(_) => -1,
}
}
}
pub fn spawn_bash_shell() -> ExitStatus {
let err = Command::new("/bin/bash")
.spawn()
.expect("failed to spawn bash")
.wait()
.expect("failed to wait on bash");
return err;
}