Utils module

This commit is contained in:
Roey Darwish Dror
2018-06-17 11:43:25 +03:00
parent b23fe9dfea
commit 0e2fe028e6
6 changed files with 49 additions and 43 deletions

View File

@@ -1,4 +1,4 @@
use super::Check;
use super::utils::Check;
use failure::Error;
use std::collections::HashSet;
use std::path::{Path, PathBuf};

View File

@@ -1,5 +1,5 @@
use super::terminal::Terminal;
use super::Check;
use super::utils::Check;
use failure;
use std::fs;
use std::path::PathBuf;

View File

@@ -19,6 +19,7 @@ mod npm;
mod report;
mod steps;
mod terminal;
mod utils;
mod vim;
use clap::App;
@@ -27,53 +28,17 @@ use failure::Error;
use git::{Git, Repositories};
use report::{Report, Reporter};
use std::env::home_dir;
use std::path::{Path, PathBuf};
use std::process::{exit, ExitStatus};
use std::path::PathBuf;
use std::process::exit;
use steps::*;
use terminal::Terminal;
use utils::{home_path, is_ancestor};
use which::which;
#[derive(Fail, Debug)]
#[fail(display = "Process failed")]
struct ProcessFailed;
#[derive(Fail, Debug)]
#[fail(display = "A step failed")]
struct StepFailed;
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 {}))
}
}
}
fn home_path(p: &str) -> PathBuf {
let mut path = home_dir().unwrap();
path.push(p);
path
}
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
}
#[cfg(unix)]
fn tpm() -> Option<PathBuf> {
let mut path = home_dir().unwrap();

View File

@@ -1,4 +1,4 @@
use super::Check;
use super::utils::Check;
use failure;
use std::path::PathBuf;
use std::process::Command;

View File

@@ -1,4 +1,4 @@
use super::Check;
use super::utils::Check;
use failure;
use std::path::PathBuf;
use std::process::Command;

41
src/utils.rs Normal file
View File

@@ -0,0 +1,41 @@
use failure::Error;
use std::env::home_dir;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
#[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 home_path(p: &str) -> PathBuf {
let mut path = home_dir().unwrap();
path.push(p);
path
}
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
}