Add logging (fix #8)

This commit is contained in:
Roey Darwish Dror
2018-06-17 14:17:36 +03:00
parent 0e2fe028e6
commit b3c8044ad3
6 changed files with 187 additions and 22 deletions

View File

@@ -1,9 +1,8 @@
use super::utils::Check;
use super::utils::{which, Check};
use failure::Error;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use which::which;
pub struct Git {
git: Option<PathBuf>,
@@ -16,9 +15,7 @@ pub struct Repositories<'a> {
impl Git {
pub fn new() -> Self {
Self {
git: which("git").ok(),
}
Self { git: which("git") }
}
pub fn get_repo_root<P: AsRef<Path>>(&self, path: P) -> Option<String> {

View File

@@ -1,10 +1,9 @@
use super::terminal::Terminal;
use super::utils::Check;
use super::utils::{which, Check};
use failure;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use which::which;
#[derive(Copy, Clone, Debug)]
pub enum Distribution {
@@ -55,8 +54,8 @@ pub fn upgrade_arch_linux(
sudo: &Option<PathBuf>,
terminal: &Terminal,
) -> Result<(), failure::Error> {
if let Ok(yay) = which("yay") {
if let Ok(python) = which("python") {
if let Some(yay) = which("yay") {
if let Some(python) = which("python") {
if python != PathBuf::from("/usr/bin/python") {
terminal.print_warning(format!(
"Python detected at {:?}, which is probably not the system Python.

View File

@@ -11,6 +11,9 @@ extern crate serde_derive;
extern crate clap;
extern crate serde;
extern crate shellexpand;
#[macro_use]
extern crate log;
extern crate env_logger;
mod config;
mod git;
@@ -33,7 +36,6 @@ use std::process::exit;
use steps::*;
use terminal::Terminal;
use utils::{home_path, is_ancestor};
use which::which;
#[derive(Fail, Debug)]
#[fail(display = "A step failed")]
@@ -56,6 +58,7 @@ fn run() -> Result<(), Error> {
.about("Upgrade all the things")
.get_matches();
env_logger::init();
let git = Git::new();
let mut git_repos = Repositories::new(&git);
let terminal = Terminal::new();
@@ -63,7 +66,7 @@ fn run() -> Result<(), Error> {
let config = Config::read()?;
let sudo = if cfg!(target_os = "linux") {
which("sudo").ok()
utils::which("sudo")
} else {
None
};
@@ -89,7 +92,7 @@ fn run() -> Result<(), Error> {
}
if cfg!(target_os = "macos") {
if let Ok(brew) = which("brew") {
if let Some(brew) = utils::which("brew") {
terminal.print_separator("Homebrew");
run_homebrew(&brew).report("Homebrew", &mut reports);
}
@@ -118,7 +121,7 @@ fn run() -> Result<(), Error> {
}
if cfg!(unix) {
if let Ok(zsh) = which("zsh") {
if let Some(zsh) = utils::which("zsh") {
if home_path(".zplug").exists() {
terminal.print_separator("zplug");
run_zplug(&zsh).report("zplug", &mut reports);
@@ -131,7 +134,7 @@ fn run() -> Result<(), Error> {
}
}
if let Ok(rustup) = which("rustup") {
if let Some(rustup) = utils::which("rustup") {
terminal.print_separator("rustup");
run_rustup(&rustup).report("rustup", &mut reports);
}
@@ -142,7 +145,7 @@ fn run() -> Result<(), Error> {
run_cargo_update(&cargo_upgrade).report("Cargo", &mut reports);
}
if let Ok(emacs) = which("emacs") {
if let Some(emacs) = utils::which("emacs") {
let init_file = home_path(".emacs.d/init.el");
if init_file.exists() {
terminal.print_separator("Emacs");
@@ -150,7 +153,7 @@ fn run() -> Result<(), Error> {
}
}
if let Ok(vim) = which("vim") {
if let Some(vim) = utils::which("vim") {
if let Some(vimrc) = vim::vimrc() {
if let Some(plugin_framework) = vim::PluginFramework::detect(&vimrc) {
terminal.print_separator(&format!("vim ({:?})", plugin_framework));
@@ -160,7 +163,7 @@ fn run() -> Result<(), Error> {
}
}
if let Ok(npm) = which("npm").map(npm::NPM::new) {
if let Some(npm) = utils::which("npm").map(npm::NPM::new) {
if let Ok(npm_root) = npm.root() {
if is_ancestor(&home_dir().unwrap(), &npm_root) {
terminal.print_separator("Node Package Manager");
@@ -169,19 +172,19 @@ fn run() -> Result<(), Error> {
}
}
if let Ok(apm) = which("apm") {
if let Some(apm) = utils::which("apm") {
terminal.print_separator("Atom Package Manager");
run_apm(&apm).report("Atom Package Manager", &mut reports);
}
if cfg!(target_os = "linux") {
if let Ok(flatpak) = which("flatpak") {
if let Some(flatpak) = utils::which("flatpak") {
terminal.print_separator("Flatpak");
run_flatpak(&flatpak).report("Flatpak", &mut reports);
}
if let Some(sudo) = &sudo {
if let Ok(snap) = which("snap") {
if let Some(snap) = utils::which("snap") {
terminal.print_separator("snap");
run_snap(&sudo, &snap).report("snap", &mut reports);
}
@@ -196,13 +199,13 @@ fn run() -> Result<(), Error> {
}
if cfg!(target_os = "linux") {
if let Ok(fwupdmgr) = which("fwupdmgr") {
if let Some(fwupdmgr) = utils::which("fwupdmgr") {
terminal.print_separator("Firmware upgrades");
run_fwupdmgr(&fwupdmgr).report("Firmware upgrade", &mut reports);
}
if let Some(sudo) = &sudo {
if let Ok(_) = which("needrestart") {
if let Some(_) = utils::which("needrestart") {
terminal.print_separator("Check for needed restarts");
run_needrestart(&sudo).report("Restarts", &mut reports);
}

View File

@@ -1,7 +1,10 @@
use failure::Error;
use std::env::home_dir;
use std::ffi::OsStr;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use which as which_mod;
#[derive(Fail, Debug)]
#[fail(display = "Process failed")]
@@ -39,3 +42,24 @@ pub fn is_ancestor(ancestor: &Path, path: &Path) -> bool {
false
}
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
}
}
}