use machine_check_common::ExecResult;
use std::{
io::Write,
path::Path,
process::{Command, Stdio},
};
use crate::{util::log_process_output, Error};
use super::Config;
pub(super) fn execute(config: &Config, artifact_path: &Path) -> Result<ExecResult, Error> {
let mut command = Command::new(artifact_path);
if config.gui {
command.arg("--gui");
} else {
command.arg("--batch");
}
if let Some(property) = &config.property {
command.arg("--property").arg(property);
} else {
command.arg("--inherent");
}
for _ in 0..config.verbose {
command.arg("--verbose");
}
if config.use_decay {
command.arg("--use-decay");
}
command.stdout(Stdio::piped()).stderr(Stdio::inherit());
let exec_output = command.output().map_err(Error::ExecRun)?;
if config.gui {
return Err(Error::Gui);
}
if config.batch {
std::io::stdout()
.write_all(&exec_output.stdout)
.map_err(Error::WriteStdout)?;
}
if !exec_output.status.success() {
return Err(Error::ExecStatus(exec_output.status));
}
log_process_output("Execution", &exec_output);
let exec_result: ExecResult = serde_json::from_slice(&exec_output.stdout)?;
Ok(exec_result)
}