use std::{env::current_dir, fmt::Display, path::PathBuf};
use crate::error::Error;
use std::process::{Command, Output, Stdio};
pub fn get_current_dir() -> Result<PathBuf, Error> {
match current_dir() {
Ok(d) => Ok(d),
Err(_) => Err(Error::CurrentDir),
}
}
pub struct CommandParams {
pub cmd_str: String,
pub params: Vec<String>,
}
impl Display for CommandParams {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.params.is_empty() {
write!(f, "{}", self.cmd_str)
} else {
write!(f, "{} {}", self.cmd_str, self.params.join(" "))
}
}
}
pub fn exec_command(cmd_params: CommandParams) -> Result<Output, Error> {
match Command::new("sh")
.arg("-c")
.arg(format!("{}", cmd_params))
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
{
Ok(e) => Ok(e),
Err(e) => Err(Error::Command(e)),
}
}