2019-12-11 23:05:38 +02:00
|
|
|
use crate::error::{SkipStep, TopgradeError};
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
2018-11-18 11:52:37 +02:00
|
|
|
use log::{debug, error};
|
2019-08-22 22:29:31 +03:00
|
|
|
use std::env;
|
2018-06-17 14:17:36 +03:00
|
|
|
use std::ffi::OsStr;
|
2019-12-29 23:18:59 +02:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
use std::path::{Path, PathBuf};
|
2018-08-22 22:18:48 +03:00
|
|
|
use std::process::{ExitStatus, Output};
|
2018-06-17 11:43:25 +03:00
|
|
|
|
|
|
|
|
pub trait Check {
|
2019-12-11 23:05:38 +02:00
|
|
|
fn check(self) -> Result<()>;
|
2018-06-17 11:43:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Check for ExitStatus {
|
2019-12-11 23:05:38 +02:00
|
|
|
fn check(self) -> Result<()> {
|
2018-06-17 11:43:25 +03:00
|
|
|
if self.success() {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
2019-12-11 23:05:38 +02:00
|
|
|
Err(TopgradeError::ProcessFailed(self).into())
|
2018-06-17 11:43:25 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 22:18:48 +03:00
|
|
|
impl Check for Output {
|
2019-12-11 23:05:38 +02:00
|
|
|
fn check(self) -> Result<()> {
|
2018-08-22 22:18:48 +03:00
|
|
|
self.status.check()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 09:18:53 +03:00
|
|
|
pub trait PathExt
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
fn if_exists(self) -> Option<Self>;
|
2018-08-22 10:43:32 +03:00
|
|
|
fn is_descendant_of(&self, ancestor: &Path) -> bool;
|
2019-01-13 23:20:32 +02:00
|
|
|
|
|
|
|
|
/// Returns the path if it exists or ErrorKind::SkipStep otherwise
|
2019-12-11 23:05:38 +02:00
|
|
|
fn require(self) -> Result<Self>;
|
2018-07-07 09:18:53 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-28 20:26:03 +03:00
|
|
|
impl<T> PathExt for T
|
|
|
|
|
where
|
|
|
|
|
T: AsRef<Path>,
|
|
|
|
|
{
|
2018-07-07 09:18:53 +03:00
|
|
|
fn if_exists(self) -> Option<Self> {
|
2019-09-28 20:26:03 +03:00
|
|
|
if self.as_ref().exists() {
|
2018-07-07 09:18:53 +03:00
|
|
|
Some(self)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2018-06-17 11:43:25 +03:00
|
|
|
}
|
|
|
|
|
|
2018-07-07 09:18:53 +03:00
|
|
|
fn is_descendant_of(&self, ancestor: &Path) -> bool {
|
2019-09-28 20:26:03 +03:00
|
|
|
self.as_ref().iter().zip(ancestor.iter()).all(|(a, b)| a == b)
|
2018-07-07 09:18:53 +03:00
|
|
|
}
|
2019-01-13 23:20:32 +02:00
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
fn require(self) -> Result<Self> {
|
2019-09-28 20:26:03 +03:00
|
|
|
if self.as_ref().exists() {
|
2019-12-08 20:56:03 +02:00
|
|
|
debug!("Path {:?} exists", self.as_ref());
|
2019-01-13 23:20:32 +02:00
|
|
|
Ok(self)
|
|
|
|
|
} else {
|
2019-12-08 20:56:03 +02:00
|
|
|
debug!("Path {:?} doesn't exist", self.as_ref());
|
2019-12-11 23:05:38 +02:00
|
|
|
Err(SkipStep.into())
|
2019-01-13 23:20:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-17 11:43:25 +03:00
|
|
|
}
|
2018-06-17 14:17:36 +03:00
|
|
|
|
|
|
|
|
pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> {
|
2018-12-09 10:30:41 +02:00
|
|
|
match which_crate::which(&binary_name) {
|
2018-06-17 14:17:36 +03:00
|
|
|
Ok(path) => {
|
|
|
|
|
debug!("Detected {:?} as {:?}", &path, &binary_name);
|
|
|
|
|
Some(path)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
match e.kind() {
|
2018-12-09 10:30:41 +02:00
|
|
|
which_crate::ErrorKind::CannotFindBinaryPath => {
|
2018-06-17 14:17:36 +03:00
|
|
|
debug!("Cannot find {:?}", &binary_name);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
error!("Detecting {:?} failed: {}", &binary_name, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-30 16:00:10 +02:00
|
|
|
|
2019-12-12 20:24:22 +02:00
|
|
|
#[cfg(unix)]
|
2019-08-20 14:02:13 +02:00
|
|
|
pub fn sudo() -> Option<PathBuf> {
|
|
|
|
|
which("sudo").or_else(|| which("pkexec"))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 22:29:31 +03:00
|
|
|
pub fn editor() -> String {
|
|
|
|
|
env::var("EDITOR").unwrap_or_else(|_| String::from(if cfg!(windows) { "notepad" } else { "vi" }))
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn require<T: AsRef<OsStr> + Debug>(binary_name: T) -> Result<PathBuf> {
|
2019-01-13 23:20:32 +02:00
|
|
|
match which_crate::which(&binary_name) {
|
|
|
|
|
Ok(path) => {
|
|
|
|
|
debug!("Detected {:?} as {:?}", &path, &binary_name);
|
|
|
|
|
Ok(path)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
|
which_crate::ErrorKind::CannotFindBinaryPath => {
|
|
|
|
|
debug!("Cannot find {:?}", &binary_name);
|
2019-12-11 23:05:38 +02:00
|
|
|
Err(SkipStep.into())
|
2019-01-13 23:20:32 +02:00
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
panic!("Detecting {:?} failed: {}", &binary_name, e);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-11 14:10:06 +02:00
|
|
|
|
2019-02-11 20:38:51 +02:00
|
|
|
#[allow(dead_code)]
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn require_option<T>(option: Option<T>) -> Result<T> {
|
2019-02-11 14:10:06 +02:00
|
|
|
if let Some(value) = option {
|
|
|
|
|
Ok(value)
|
|
|
|
|
} else {
|
2019-12-11 23:05:38 +02:00
|
|
|
Err(SkipStep.into())
|
2019-02-11 14:10:06 +02:00
|
|
|
}
|
|
|
|
|
}
|