use std::num::TryFromIntError;
use std::path::PathBuf;
use std::process::ExitStatus;
#[must_use]
#[derive(Copy, Clone, Debug)]
pub struct ExitCode(pub isize);
impl ExitCode {
pub fn success() -> Self {
Self(0)
}
pub fn is_success(&self) -> bool {
match self {
ExitCode(0) => true,
ExitCode(_) => false,
}
}
}
impl TryFrom<ExitStatus> for ExitCode {
type Error = TryFromIntError;
fn try_from(status: ExitStatus) -> Result<Self, Self::Error> {
let exit_code = status.code().unwrap_or(1);
Ok(Self(exit_code.try_into()?))
}
}
pub type EyreExitOr<T> = eyre::Result<Result<T, ExitCode>>;
#[macro_export]
macro_rules! try_exit_code {
($e:expr) => {
match $e {
Ok(value) => value,
Err(exit_code) => {
return Ok(Err(exit_code));
}
}
};
}
pub fn get_from_path(exe_name: &str) -> Option<PathBuf> {
std::env::var_os("PATH").and_then(|paths| {
std::env::split_paths(&paths).find_map(|dir| {
let bash_path = dir.join(exe_name);
if bash_path.is_file() {
Some(bash_path)
} else {
None
}
})
})
}
pub fn get_sh() -> Option<PathBuf> {
let exe_name = if cfg!(target_os = "windows") {
"bash.exe"
} else {
"sh"
};
if cfg!(target_os = "windows") {
let git_path = get_from_path("git.exe").expect("Couldn't find git.exe");
let git_dir = git_path.parent().unwrap().parent().unwrap();
let git_bash = git_dir.join("bin").join(exe_name);
if git_bash.is_file() {
return Some(git_bash);
}
}
get_from_path(exe_name)
}