fd/src/exec/command.rs

57 lines
1.6 KiB
Rust
Raw Normal View History

use std::io;
2017-12-09 02:46:08 +01:00
use std::io::Write;
2018-04-13 22:46:17 +02:00
use std::process::Command;
use std::sync::Mutex;
2020-04-03 21:18:54 +02:00
use crate::error::print_error;
use crate::exit_codes::ExitCode;
/// Executes a command.
pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {
// Spawn the supplied command.
/* let output = cmd.output(); */
// This sort of works:
let child = cmd.spawn().expect("failed to wait on child");
let output = child
.wait_with_output()
.expect("failed to wait on child");
ExitCode::Success
/* let child_handle = cmd.spawn();
match child_handle {
Ok(output) => {
let exit_code = output.wait();
}
Err() => {
}
} */
// Then wait for the command to exit, if it was spawned.
/* match output {
Ok(output) => {
// While this lock is active, this thread will be the only thread allowed
2017-12-09 02:46:08 +01:00
// to write its outputs.
let _lock = out_perm.lock().unwrap();
let stdout = io::stdout();
let stderr = io::stderr();
//let _ = stdout.lock().write_all(&output.stdout);
//let _ = stderr.lock().write_all(&output.stderr);
if output.status.code() == Some(0) {
ExitCode::Success
} else {
ExitCode::GeneralError
}
}
Err(ref why) if why.kind() == io::ErrorKind::NotFound => {
2020-04-03 21:18:54 +02:00
print_error(format!("Command not found: {:?}", cmd));
ExitCode::GeneralError
}
Err(why) => {
2020-04-03 21:18:54 +02:00
print_error(format!("Problem while executing command: {}", why));
ExitCode::GeneralError
}
} */
}