use std::ffi::OsStr;
use std::process::{Command, Output};
use std::path::PathBuf;
use crate::common::PASSWORD;
pub struct DataboxerCommand {
command: Command,
}
impl DataboxerCommand {
pub fn new(subcommand: &str, needs_password: bool) -> Self {
let path = PathBuf::from("target/debug").join({
if cfg!(target_os = "windows") {
"databoxer.exe"
} else {
"databoxer"
}
});
let mut command = Command::new(path);
command
.arg("--verbose")
.args(subcommand.split_ascii_whitespace());
if needs_password {
command.arg("--password").arg(PASSWORD);
}
Self { command }
}
pub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
self.command.arg(arg);
self
}
pub fn execute(mut self) -> Output {
println!("Executing: {:?}", &self.command);
match self.command.output() {
Ok(output) => output,
Err(err) => panic!("Failed to execute {:?}: {:?}", &self.command, err)
}
}
}
pub fn print_output(output: &Output) {
println!("Exit code ({})", output.status);
let stdout = &output.stdout;
if !stdout.is_empty() {
println!("Stdout:\n{}", std::str::from_utf8(&stdout).unwrap());
}
let stderr = &output.stderr;
if !stderr.is_empty() {
println!("Stderr:\n{}", std::str::from_utf8(&stderr).unwrap());
}
}