use std::collections::HashMap;
use std::io;
use std::process::ExitStatus;
use crate::config_models::Process;
use crate::log;
pub fn exec(process: &Process, env: Option<&HashMap<String, String>>) -> io::Result<ExitStatus> {
let mut proc_command = std::process::Command::new(process.command.clone());
proc_command.args(process.args.clone());
if !process.cwd.is_empty() {
proc_command.current_dir(process.cwd.clone());
}
if let Some(env) = env {
proc_command.envs(env.clone());
}
let proc_result = proc_command.status();
if let Ok(output) = &proc_result {
log::print(format!(
"Command '{}' {:?} executed{}. ({})",
process.command,
process.args,
if !process.cwd.is_empty() {
format!(" in directory '{}'", process.cwd)
} else {
String::new()
},
output
));
} else {
log::error(format!(
"Could not execute command '{}' {:?}{}: {:?}",
process.command,
process.args,
if !process.cwd.is_empty() {
format!(" in directory '{}'", process.cwd)
} else {
String::new()
},
proc_result
));
}
proc_result
}