use anyhow::{Context, Result};
use std::{
env::current_dir,
path::{Path, PathBuf},
process::Command,
};
use subprocess::Exec;
pub struct RemoveFile(pub PathBuf);
impl Drop for RemoveFile {
fn drop(&mut self) {
std::fs::remove_file(&self.0)
.map_err(|err| eprintln!("{err}"))
.unwrap_or_default();
}
}
pub fn exec_from_command(command: &Command) -> Exec {
let mut exec = Exec::cmd(command.get_program()).args(command.get_args().collect::<Vec<_>>());
for (key, val) in command.get_envs() {
if let Some(val) = val {
exec = exec.env(key, val);
} else {
exec = exec.env_remove(key);
}
}
if let Some(path) = command.get_current_dir() {
exec = exec.cwd(path);
}
exec
}
#[must_use]
pub fn strip_current_dir(path: &Path) -> &Path {
current_dir()
.ok()
.and_then(|dir| strip_prefix(path, &dir).ok())
.unwrap_or(path)
}
pub fn strip_prefix<'a>(path: &'a Path, base: &Path) -> Result<&'a Path> {
#[allow(clippy::disallowed_methods)]
path.strip_prefix(base).with_context(|| {
format!(
"\
`base` is not a prefix of `path`
base: `{}`
path: `{}`",
base.display(),
path.display()
)
})
}