2018-06-17 11:43:25 +03:00
|
|
|
use failure::Error;
|
2018-06-17 14:17:36 +03:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
use std::fmt::Debug;
|
2018-06-17 11:43:25 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::process::ExitStatus;
|
2018-06-17 14:17:36 +03:00
|
|
|
use which as which_mod;
|
2018-06-17 11:43:25 +03:00
|
|
|
|
|
|
|
|
#[derive(Fail, Debug)]
|
|
|
|
|
#[fail(display = "Process failed")]
|
|
|
|
|
pub struct ProcessFailed;
|
|
|
|
|
|
|
|
|
|
pub trait Check {
|
|
|
|
|
fn check(self) -> Result<(), Error>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Check for ExitStatus {
|
|
|
|
|
fn check(self) -> Result<(), Error> {
|
|
|
|
|
if self.success() {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::from(ProcessFailed {}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn is_ancestor(ancestor: &Path, path: &Path) -> bool {
|
|
|
|
|
let mut p = path;
|
|
|
|
|
while let Some(parent) = p.parent() {
|
|
|
|
|
if parent == ancestor {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
}
|
2018-06-17 14:17:36 +03:00
|
|
|
|
|
|
|
|
pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> {
|
|
|
|
|
match which_mod::which(&binary_name) {
|
|
|
|
|
Ok(path) => {
|
|
|
|
|
debug!("Detected {:?} as {:?}", &path, &binary_name);
|
|
|
|
|
Some(path)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
match e.kind() {
|
|
|
|
|
which_mod::ErrorKind::CannotFindBinaryPath => {
|
|
|
|
|
debug!("Cannot find {:?}", &binary_name);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
error!("Detecting {:?} failed: {}", &binary_name, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|