diff --git a/src/executor.rs b/src/executor.rs new file mode 100644 index 00000000..5cb2bcfb --- /dev/null +++ b/src/executor.rs @@ -0,0 +1,121 @@ +use super::utils::Check; +use failure; +use std::ffi::{OsStr, OsString}; +use std::io; +use std::path::Path; +use std::process::{Child, Command, ExitStatus}; + +pub enum Executor { + Wet(Command), + Dry(DryCommand), +} + +impl Executor { + pub fn new>(program: S, dry: bool) -> Self { + match dry { + false => Executor::Wet(Command::new(program)), + true => Executor::Dry(DryCommand { + program: program.as_ref().into(), + ..Default::default() + }), + } + } + + pub fn arg>(&mut self, arg: S) -> &mut Executor { + match self { + Executor::Wet(c) => { + c.arg(arg); + } + Executor::Dry(c) => { + c.args.push(arg.as_ref().into()); + } + } + + self + } + + pub fn args(&mut self, args: I) -> &mut Executor + where + I: IntoIterator, + S: AsRef, + { + match self { + Executor::Wet(c) => { + c.args(args); + } + Executor::Dry(c) => { + c.args.extend(args.into_iter().map(|arg| arg.as_ref().into())); + } + } + + self + } + + pub fn current_dir>(&mut self, dir: P) -> &mut Executor { + match self { + Executor::Wet(c) => { + c.current_dir(dir); + } + Executor::Dry(c) => c.directory = Some(dir.as_ref().into()), + } + + self + } + + pub fn spawn(&mut self) -> Result { + match self { + Executor::Wet(c) => c.spawn().map(|c| ExecutorChild::Wet(c)), + Executor::Dry(c) => { + print!( + "Dry running: {} {}", + c.program.to_string_lossy(), + c.args + .iter() + .map(|a| String::from(a.to_string_lossy())) + .collect::>() + .join(" ") + ); + match &c.directory { + Some(dir) => println!(" in {}", dir.to_string_lossy()), + None => println!(), + }; + Ok(ExecutorChild::Dry) + } + } + } +} + +#[derive(Default)] +pub struct DryCommand { + program: OsString, + args: Vec, + directory: Option, +} + +pub enum ExecutorChild { + Wet(Child), + Dry, +} + +impl ExecutorChild { + pub fn wait(&mut self) -> Result { + match self { + ExecutorChild::Wet(c) => c.wait().map(|s| ExecutorExitStatus::Wet(s)), + ExecutorChild::Dry => Ok(ExecutorExitStatus::Dry), + } + } +} + +pub enum ExecutorExitStatus { + Wet(ExitStatus), + Dry, +} + +impl Check for ExecutorExitStatus { + fn check(self) -> Result<(), failure::Error> { + match self { + ExecutorExitStatus::Wet(e) => e.check(), + ExecutorExitStatus::Dry => Ok(()), + } + } +} diff --git a/src/generic.rs b/src/generic.rs index 7c7a424c..55a56e42 100644 --- a/src/generic.rs +++ b/src/generic.rs @@ -1,19 +1,18 @@ +use super::executor::Executor; use super::terminal::Terminal; -use super::utils; -use super::utils::{Check, PathExt}; +use super::utils::{self, Check, PathExt}; use directories::BaseDirs; use failure::Error; -use std::process::Command; const EMACS_UPGRADE: &str = include_str!("emacs.el"); #[must_use] -pub fn run_cargo_update(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_cargo_update(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(cargo_update) = base_dirs.home_dir().join(".cargo/bin/cargo-install-update").if_exists() { terminal.print_separator("Cargo"); let success = || -> Result<(), Error> { - Command::new(cargo_update) + Executor::new(cargo_update, dry_run) .args(&["install-update", "--git", "--all"]) .spawn()? .wait()? @@ -29,13 +28,13 @@ pub fn run_cargo_update(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option } #[must_use] -pub fn run_emacs(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_emacs(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(emacs) = utils::which("emacs") { if let Some(init_file) = base_dirs.home_dir().join(".emacs.d/init.el").if_exists() { terminal.print_separator("Emacs"); let success = || -> Result<(), Error> { - Command::new(&emacs) + Executor::new(&emacs, dry_run) .args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE]) .spawn()? .wait()? @@ -51,12 +50,12 @@ pub fn run_emacs(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'sta } #[must_use] -pub fn run_apm(terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_apm(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(apm) = utils::which("apm") { terminal.print_separator("Atom Package Manager"); let success = || -> Result<(), Error> { - Command::new(&apm) + Executor::new(&apm, dry_run) .args(&["upgrade", "--confirm=false"]) .spawn()? .wait()? @@ -72,20 +71,20 @@ pub fn run_apm(terminal: &mut Terminal) -> Option<(&'static str, bool)> { } #[must_use] -pub fn run_rustup(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_rustup(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(rustup) = utils::which("rustup") { terminal.print_separator("rustup"); let success = || -> Result<(), Error> { if rustup.is_descendant_of(base_dirs.home_dir()) { - Command::new(&rustup) + Executor::new(&rustup, dry_run) .args(&["self", "update"]) .spawn()? .wait()? .check()?; } - Command::new(&rustup).arg("update").spawn()?.wait()?.check()?; + Executor::new(&rustup, dry_run).arg("update").spawn()?.wait()?.check()?; Ok(()) }().is_ok(); @@ -96,9 +95,14 @@ pub fn run_rustup(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'st } #[must_use] -pub fn run_custom_command(name: &str, command: &str, terminal: &mut Terminal) -> Result<(), Error> { +pub fn run_custom_command(name: &str, command: &str, terminal: &mut Terminal, dry_run: bool) -> Result<(), Error> { terminal.print_separator(name); - Command::new("sh").arg("-c").arg(command).spawn()?.wait()?.check()?; + Executor::new("sh", dry_run) + .arg("-c") + .arg(command) + .spawn()? + .wait()? + .check()?; Ok(()) } diff --git a/src/git.rs b/src/git.rs index e222e440..cc6df9d8 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,3 +1,4 @@ +use super::executor::Executor; use super::terminal::Terminal; use super::utils::{which, Check}; use failure::Error; @@ -34,8 +35,7 @@ impl Git { if let Some(git) = &self.git { let output = Command::new(&git) - .arg("rev-parse") - .arg("--show-toplevel") + .args(&["rev-parse", "--show-toplevel"]) .current_dir(path) .output(); @@ -57,7 +57,7 @@ impl Git { None } - pub fn pull>(&self, path: P, terminal: &mut Terminal) -> Option<(String, bool)> { + pub fn pull>(&self, path: P, terminal: &mut Terminal, dry_run: bool) -> Option<(String, bool)> { let path = path.as_ref(); terminal.print_separator(format!("Pulling {}", path.display())); @@ -65,20 +65,15 @@ impl Git { let git = self.git.as_ref().unwrap(); let success = || -> Result<(), Error> { - Command::new(git) - .arg("pull") - .arg("--rebase") - .arg("--autostash") + Executor::new(git, dry_run) + .args(&["pull", "--rebase", "--autostash"]) .current_dir(&path) .spawn()? .wait()? .check()?; - Command::new(git) - .arg("submodule") - .arg("update") - .arg("--init") - .arg("--recursive") + Executor::new(git, dry_run) + .args(&["submodule", "update", "--init", "--recursive"]) .current_dir(&path) .spawn()? .wait()? diff --git a/src/linux.rs b/src/linux.rs index 6398331d..be3aac64 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -1,9 +1,9 @@ +use super::executor::Executor; use super::terminal::Terminal; use super::utils::{which, Check}; use failure; use std::fs; use std::path::PathBuf; -use std::process::Command; #[derive(Copy, Clone, Debug)] pub enum Distribution { @@ -50,7 +50,7 @@ impl Distribution { } } -fn upgrade_arch_linux(sudo: &Option, terminal: &mut Terminal) -> Result<(), failure::Error> { +fn upgrade_arch_linux(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> { if let Some(yay) = which("yay") { if let Some(python) = which("python") { if python != PathBuf::from("/usr/bin/python") { @@ -63,9 +63,9 @@ It's dangerous to run yay since Python based AUR packages will be installed in t } } - Command::new(yay).spawn()?.wait()?.check()?; + Executor::new(yay, dry_run).spawn()?.wait()?.check()?; } else if let Some(sudo) = &sudo { - Command::new(&sudo) + Executor::new(&sudo, dry_run) .args(&["/usr/bin/pacman", "-Syu"]) .spawn()? .wait()? @@ -77,9 +77,9 @@ It's dangerous to run yay since Python based AUR packages will be installed in t Ok(()) } -fn upgrade_redhat(sudo: &Option, terminal: &mut Terminal) -> Result<(), failure::Error> { +fn upgrade_redhat(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> { if let Some(sudo) = &sudo { - Command::new(&sudo) + Executor::new(&sudo, dry_run) .args(&["/usr/bin/yum", "upgrade"]) .spawn()? .wait()? @@ -91,9 +91,9 @@ fn upgrade_redhat(sudo: &Option, terminal: &mut Terminal) -> Result<(), Ok(()) } -fn upgrade_fedora(sudo: &Option, terminal: &mut Terminal) -> Result<(), failure::Error> { +fn upgrade_fedora(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> { if let Some(sudo) = &sudo { - Command::new(&sudo) + Executor::new(&sudo, dry_run) .args(&["/usr/bin/dnf", "upgrade"]) .spawn()? .wait()? @@ -105,15 +105,15 @@ fn upgrade_fedora(sudo: &Option, terminal: &mut Terminal) -> Result<(), Ok(()) } -fn upgrade_debian(sudo: &Option, terminal: &mut Terminal) -> Result<(), failure::Error> { +fn upgrade_debian(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> { if let Some(sudo) = &sudo { - Command::new(&sudo) + Executor::new(&sudo, dry_run) .args(&["/usr/bin/apt", "update"]) .spawn()? .wait()? .check()?; - Command::new(&sudo) + Executor::new(&sudo, dry_run) .args(&["/usr/bin/apt", "dist-upgrade"]) .spawn()? .wait()? @@ -126,15 +126,15 @@ fn upgrade_debian(sudo: &Option, terminal: &mut Terminal) -> Result<(), } #[must_use] -pub fn upgrade(sudo: &Option, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn upgrade(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { terminal.print_separator("System update"); let success = match Distribution::detect() { Ok(distribution) => match distribution { - Distribution::Arch => upgrade_arch_linux(&sudo, terminal), - Distribution::CentOS => upgrade_redhat(&sudo, terminal), - Distribution::Fedora => upgrade_fedora(&sudo, terminal), - Distribution::Ubuntu | Distribution::Debian => upgrade_debian(&sudo, terminal), + Distribution::Arch => upgrade_arch_linux(&sudo, terminal, dry_run), + Distribution::CentOS => upgrade_redhat(&sudo, terminal, dry_run), + Distribution::Fedora => upgrade_fedora(&sudo, terminal, dry_run), + Distribution::Ubuntu | Distribution::Debian => upgrade_debian(&sudo, terminal, dry_run), }.is_ok(), Err(e) => { println!("Error detecting current distribution: {}", e); @@ -146,13 +146,13 @@ pub fn upgrade(sudo: &Option, terminal: &mut Terminal) -> Option<(&'sta } #[must_use] -pub fn run_needrestart(sudo: &Option, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_needrestart(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(sudo) = sudo { if let Some(needrestart) = which("needrestart") { terminal.print_separator("Check for needed restarts"); let success = || -> Result<(), failure::Error> { - Command::new(&sudo).arg(needrestart).spawn()?.wait()?.check()?; + Executor::new(&sudo, dry_run).arg(needrestart).spawn()?.wait()?.check()?; Ok(()) }().is_ok(); @@ -165,13 +165,21 @@ pub fn run_needrestart(sudo: &Option, terminal: &mut Terminal) -> Optio } #[must_use] -pub fn run_fwupdmgr(terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_fwupdmgr(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(fwupdmgr) = which("fwupdmgr") { terminal.print_separator("Firmware upgrades"); let success = || -> Result<(), failure::Error> { - Command::new(&fwupdmgr).arg("refresh").spawn()?.wait()?.check()?; - Command::new(&fwupdmgr).arg("get-updates").spawn()?.wait()?.check()?; + Executor::new(&fwupdmgr, dry_run) + .arg("refresh") + .spawn()? + .wait()? + .check()?; + Executor::new(&fwupdmgr, dry_run) + .arg("get-updates") + .spawn()? + .wait()? + .check()?; Ok(()) }().is_ok(); @@ -182,12 +190,16 @@ pub fn run_fwupdmgr(terminal: &mut Terminal) -> Option<(&'static str, bool)> { } #[must_use] -pub fn run_flatpak(terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_flatpak(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(flatpak) = which("flatpak") { terminal.print_separator("Flatpak"); let success = || -> Result<(), failure::Error> { - Command::new(&flatpak).args(&["update", "-y"]).spawn()?.wait()?.check()?; + Executor::new(&flatpak, dry_run) + .args(&["update", "-y"]) + .spawn()? + .wait()? + .check()?; Ok(()) }().is_ok(); @@ -198,14 +210,14 @@ pub fn run_flatpak(terminal: &mut Terminal) -> Option<(&'static str, bool)> { } #[must_use] -pub fn run_snap(sudo: &Option, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_snap(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(sudo) = sudo { if let Some(snap) = which("snap") { if PathBuf::from("/var/snapd.socket").exists() { terminal.print_separator("snap"); let success = || -> Result<(), failure::Error> { - Command::new(&sudo) + Executor::new(&sudo, dry_run) .args(&[snap.to_str().unwrap(), "refresh"]) .spawn()? .wait()? diff --git a/src/macos.rs b/src/macos.rs index 72f836a5..f30f017b 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -1,14 +1,14 @@ +use super::executor::Executor; use super::terminal::Terminal; use super::utils::Check; use failure; -use std::process::Command; #[must_use] -pub fn upgrade_macos(terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn upgrade_macos(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { terminal.print_separator("App Store"); let success = || -> Result<(), failure::Error> { - Command::new("softwareupdate") + Executor::new("softwareupdate", dry_run) .args(&["--install", "--all"]) .spawn()? .wait()? diff --git a/src/main.rs b/src/main.rs index 861eaf81..4c7e2cc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ mod unix; mod windows; mod config; +mod executor; mod generic; mod git; mod node; @@ -84,6 +85,12 @@ fn run() -> Result<(), Error> { .help("Don't perform system upgrade") .long("no-system"), ) + .arg( + Arg::with_name("dry_run") + .help("Print what would be done") + .short("n") + .long("dry-run"), + ) .get_matches(); if matches.is_present("tmux") && env::var("TMUX").is_err() { @@ -100,13 +107,14 @@ fn run() -> Result<(), Error> { let mut terminal = Terminal::new(); let config = Config::read(&base_dirs)?; let mut report = Report::new(); + let dry_run = matches.is_present("dry_run"); #[cfg(target_os = "linux")] let sudo = utils::which("sudo"); if let Some(commands) = config.pre_commands() { for (name, command) in commands { - generic::run_custom_command(&name, &command, &mut terminal)?; + generic::run_custom_command(&name, &command, &mut terminal, dry_run)?; } } @@ -114,20 +122,29 @@ fn run() -> Result<(), Error> { let powershell = windows::Powershell::new(); #[cfg(windows)] - report.push_result(execute(|terminal| powershell.update_modules(terminal), &mut terminal)); + report.push_result(execute( + |terminal| powershell.update_modules(terminal, dry_run), + &mut terminal, + )); #[cfg(target_os = "linux")] { if !(matches.is_present("no_system")) { - report.push_result(execute(|terminal| linux::upgrade(&sudo, terminal), &mut terminal)); + report.push_result(execute( + |terminal| linux::upgrade(&sudo, terminal, dry_run), + &mut terminal, + )); } } #[cfg(windows)] - report.push_result(execute(|terminal| windows::run_chocolatey(terminal), &mut terminal)); + report.push_result(execute( + |terminal| windows::run_chocolatey(terminal, dry_run), + &mut terminal, + )); #[cfg(unix)] - report.push_result(execute(|terminal| unix::run_homebrew(terminal), &mut terminal)); + report.push_result(execute(|terminal| unix::run_homebrew(terminal, dry_run), &mut terminal)); git_repos.insert(base_dirs.home_dir().join(".emacs.d")); git_repos.insert(base_dirs.home_dir().join(".vim")); @@ -155,56 +172,73 @@ fn run() -> Result<(), Error> { } for repo in git_repos.repositories() { - report.push_result(execute(|terminal| git.pull(&repo, terminal), &mut terminal)); + report.push_result(execute(|terminal| git.pull(&repo, terminal, dry_run), &mut terminal)); } #[cfg(unix)] { - report.push_result(execute(|terminal| unix::run_zplug(&base_dirs, terminal), &mut terminal)); report.push_result(execute( - |terminal| unix::run_fisherman(&base_dirs, terminal), + |terminal| unix::run_zplug(&base_dirs, terminal, dry_run), + &mut terminal, + )); + report.push_result(execute( + |terminal| unix::run_fisherman(&base_dirs, terminal, dry_run), + &mut terminal, + )); + report.push_result(execute( + |terminal| unix::run_tpm(&base_dirs, terminal, dry_run), &mut terminal, )); - report.push_result(execute(|terminal| unix::run_tpm(&base_dirs, terminal), &mut terminal)); } report.push_result(execute( - |terminal| generic::run_rustup(&base_dirs, terminal), + |terminal| generic::run_rustup(&base_dirs, terminal, dry_run), &mut terminal, )); report.push_result(execute( - |terminal| generic::run_cargo_update(&base_dirs, terminal), + |terminal| generic::run_cargo_update(&base_dirs, terminal, dry_run), &mut terminal, )); report.push_result(execute( - |terminal| generic::run_emacs(&base_dirs, terminal), + |terminal| generic::run_emacs(&base_dirs, terminal, dry_run), &mut terminal, )); report.push_result(execute( - |terminal| vim::upgrade_vim(&base_dirs, terminal), + |terminal| vim::upgrade_vim(&base_dirs, terminal, dry_run), &mut terminal, )); report.push_result(execute( - |terminal| vim::upgrade_neovim(&base_dirs, terminal), + |terminal| vim::upgrade_neovim(&base_dirs, terminal, dry_run), &mut terminal, )); report.push_result(execute( - |terminal| node::run_npm_upgrade(&base_dirs, terminal), + |terminal| node::run_npm_upgrade(&base_dirs, terminal, dry_run), &mut terminal, )); - report.push_result(execute(|terminal| node::yarn_global_update(terminal), &mut terminal)); - report.push_result(execute(|terminal| generic::run_apm(terminal), &mut terminal)); + report.push_result(execute( + |terminal| node::yarn_global_update(terminal, dry_run), + &mut terminal, + )); + report.push_result(execute(|terminal| generic::run_apm(terminal, dry_run), &mut terminal)); #[cfg(target_os = "linux")] { - report.push_result(execute(|terminal| linux::run_flatpak(terminal), &mut terminal)); - report.push_result(execute(|terminal| linux::run_snap(&sudo, terminal), &mut terminal)); + report.push_result(execute(|terminal| linux::run_flatpak(terminal, dry_run), &mut terminal)); + report.push_result(execute( + |terminal| linux::run_snap(&sudo, terminal, dry_run), + &mut terminal, + )); } if let Some(commands) = config.commands() { for (name, command) in commands { report.push_result(execute( - |terminal| Some((name, generic::run_custom_command(&name, &command, terminal).is_ok())), + |terminal| { + Some(( + name, + generic::run_custom_command(&name, &command, terminal, dry_run).is_ok(), + )) + }, &mut terminal, )); } @@ -212,9 +246,12 @@ fn run() -> Result<(), Error> { #[cfg(target_os = "linux")] { - report.push_result(execute(|terminal| linux::run_fwupdmgr(terminal), &mut terminal)); report.push_result(execute( - |terminal| linux::run_needrestart(&sudo, terminal), + |terminal| linux::run_fwupdmgr(terminal, dry_run), + &mut terminal, + )); + report.push_result(execute( + |terminal| linux::run_needrestart(&sudo, terminal, dry_run), &mut terminal, )); } @@ -222,14 +259,20 @@ fn run() -> Result<(), Error> { #[cfg(target_os = "macos")] { if !(matches.is_present("no_system")) { - report.push_result(execute(|terminal| macos::upgrade_macos(terminal), &mut terminal)); + report.push_result(execute( + |terminal| macos::upgrade_macos(terminal, dry_run), + &mut terminal, + )); } } #[cfg(windows)] { if !(matches.is_present("no_system")) { - report.push_result(execute(|terminal| powershell.windows_update(terminal), &mut terminal)); + report.push_result(execute( + |terminal| powershell.windows_update(terminal, dry_run), + &mut terminal, + )); } } diff --git a/src/node.rs b/src/node.rs index 634884ac..7fe58613 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,3 +1,4 @@ +use super::executor::Executor; use super::terminal::Terminal; use super::utils::{which, Check, PathExt}; use directories::BaseDirs; @@ -22,8 +23,8 @@ impl NPM { Ok(PathBuf::from(&String::from_utf8(output.stdout)?)) } - fn upgrade(&self) -> Result<(), failure::Error> { - Command::new(&self.command) + fn upgrade(&self, dry_run: bool) -> Result<(), failure::Error> { + Executor::new(&self.command, dry_run) .args(&["update", "-g"]) .spawn()? .wait()? @@ -34,12 +35,12 @@ impl NPM { } #[must_use] -pub fn run_npm_upgrade(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_npm_upgrade(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(npm) = which("npm").map(NPM::new) { if let Ok(npm_root) = npm.root() { if npm_root.is_descendant_of(base_dirs.home_dir()) { terminal.print_separator("Node Package Manager"); - let success = npm.upgrade().is_ok(); + let success = npm.upgrade(dry_run).is_ok(); return Some(("NPM", success)); } } @@ -48,12 +49,12 @@ pub fn run_npm_upgrade(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option< } #[must_use] -pub fn yarn_global_update(terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn yarn_global_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(yarn) = which("yarn") { terminal.print_separator("Yarn"); let success = || -> Result<(), failure::Error> { - Command::new(&yarn) + Executor::new(&yarn, dry_run) .args(&["global", "upgrade", "-s"]) .spawn()? .wait()? diff --git a/src/unix.rs b/src/unix.rs index 076e1850..da16284d 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,3 +1,4 @@ +use super::executor::Executor; use super::terminal::Terminal; use super::utils::{which, Check, PathExt}; use directories::BaseDirs; @@ -6,13 +7,13 @@ use std::env; use std::os::unix::process::CommandExt; use std::process::Command; -pub fn run_zplug(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_zplug(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(zsh) = which("zsh") { if base_dirs.home_dir().join(".zplug").exists() { terminal.print_separator("zplug"); let success = || -> Result<(), Error> { - Command::new(zsh) + Executor::new(zsh, dry_run) .args(&["-c", "source ~/.zshrc && zplug update"]) .spawn()? .wait()? @@ -27,13 +28,13 @@ pub fn run_zplug(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'sta None } -pub fn run_fisherman(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_fisherman(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(fish) = which("fish") { if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() { terminal.print_separator("fisherman"); let success = || -> Result<(), Error> { - Command::new(fish) + Executor::new(fish, dry_run) .args(&["-c", "fisher update"]) .spawn()? .wait()? @@ -48,7 +49,7 @@ pub fn run_fisherman(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(& None } -pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(tpm) = base_dirs .home_dir() .join(".tmux/plugins/tpm/bin/update_plugins") @@ -57,7 +58,7 @@ pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'stati terminal.print_separator("tmux plugins"); let success = || -> Result<(), Error> { - Command::new(&tpm).arg("all").spawn()?.wait()?.check()?; + Executor::new(&tpm, dry_run).arg("all").spawn()?.wait()?.check()?; Ok(()) }().is_ok(); @@ -87,13 +88,13 @@ pub fn run_in_tmux() -> ! { } #[must_use] -pub fn run_homebrew(terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_homebrew(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(brew) = which("brew") { terminal.print_separator("Brew"); let inner = || -> Result<(), Error> { - Command::new(&brew).arg("update").spawn()?.wait()?.check()?; - Command::new(&brew).arg("upgrade").spawn()?.wait()?.check()?; + Executor::new(&brew, dry_run).arg("update").spawn()?.wait()?.check()?; + Executor::new(&brew, dry_run).arg("upgrade").spawn()?.wait()?.check()?; Ok(()) }; diff --git a/src/vim.rs b/src/vim.rs index e0abb1b0..98d7e818 100644 --- a/src/vim.rs +++ b/src/vim.rs @@ -1,10 +1,10 @@ +use super::executor::Executor; use super::terminal::Terminal; use super::utils::{which, Check, PathExt}; use directories::BaseDirs; use failure; use std::fs; use std::path::PathBuf; -use std::process::Command; #[derive(Debug, Clone, Copy)] pub enum PluginFramework { @@ -54,8 +54,13 @@ fn nvimrc(base_dirs: &BaseDirs) -> Option { } #[must_use] -fn upgrade(vim: &PathBuf, vimrc: &PathBuf, plugin_framework: PluginFramework) -> Result<(), failure::Error> { - Command::new(&vim) +fn upgrade( + vim: &PathBuf, + vimrc: &PathBuf, + plugin_framework: PluginFramework, + dry_run: bool, +) -> Result<(), failure::Error> { + Executor::new(&vim, dry_run) .args(&[ "-N", "-u", @@ -78,12 +83,12 @@ fn upgrade(vim: &PathBuf, vimrc: &PathBuf, plugin_framework: PluginFramework) -> } #[must_use] -pub fn upgrade_vim(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn upgrade_vim(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(vim) = which("vim") { if let Some(vimrc) = vimrc(&base_dirs) { if let Some(plugin_framework) = PluginFramework::detect(&vimrc) { terminal.print_separator(&format!("Vim ({:?})", plugin_framework)); - let success = upgrade(&vim, &vimrc, plugin_framework).is_ok(); + let success = upgrade(&vim, &vimrc, plugin_framework, dry_run).is_ok(); return Some(("vim", success)); } } @@ -93,12 +98,12 @@ pub fn upgrade_vim(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'s } #[must_use] -pub fn upgrade_neovim(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn upgrade_neovim(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(nvim) = which("nvim") { if let Some(nvimrc) = nvimrc(&base_dirs) { if let Some(plugin_framework) = PluginFramework::detect(&nvimrc) { terminal.print_separator(&format!("Neovim ({:?})", plugin_framework)); - let success = upgrade(&nvim, &nvimrc, plugin_framework).is_ok(); + let success = upgrade(&nvim, &nvimrc, plugin_framework, dry_run).is_ok(); return Some(("Neovim", success)); } } diff --git a/src/windows.rs b/src/windows.rs index b38609ba..4a7d9041 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,3 +1,4 @@ +use super::executor::Executor; use super::terminal::Terminal; use super::utils::{self, which, Check}; use failure; @@ -5,12 +6,16 @@ use std::path::PathBuf; use std::process::Command; #[must_use] -pub fn run_chocolatey(terminal: &mut Terminal) -> Option<(&'static str, bool)> { +pub fn run_chocolatey(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(choco) = utils::which("choco") { terminal.print_separator("Chocolatey"); let success = || -> Result<(), failure::Error> { - Command::new(&choco).args(&["upgrade", "all"]).spawn()?.wait()?.check()?; + Executor::new(&choco, dry_run) + .args(&["upgrade", "all"]) + .spawn()? + .wait()? + .check()?; Ok(()) }().is_ok(); @@ -60,12 +65,16 @@ impl Powershell { } #[must_use] - pub fn update_modules(&self, terminal: &mut Terminal) -> Option<(&'static str, bool)> { + pub fn update_modules(&self, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(powershell) = &self.path { terminal.print_separator("Powershell Modules Update"); let success = || -> Result<(), failure::Error> { - Command::new(&powershell).arg("Update-Module").spawn()?.wait()?.check()?; + Executor::new(&powershell, dry_run) + .arg("Update-Module") + .spawn()? + .wait()? + .check()?; Ok(()) }().is_ok(); @@ -76,13 +85,13 @@ impl Powershell { } #[must_use] - pub fn windows_update(&self, terminal: &mut Terminal) -> Option<(&'static str, bool)> { + pub fn windows_update(&self, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { if let Some(powershell) = &self.path { if Self::has_command(&powershell, "Install-WindowsUpdate") { terminal.print_separator("Windows Update"); let success = || -> Result<(), failure::Error> { - Command::new(&powershell) + Executor::new(&powershell, dry_run) .args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"]) .spawn()? .wait()?