diff --git a/src/executor.rs b/src/executor.rs index 2370483e..2807eae5 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -124,6 +124,13 @@ impl Executor { Ok(result) } + + /// A convinence method for `spawn().wait().check()`. + /// Returns an error if something went wrong during the execution or if the + /// process exited with failure. + pub fn check_run(&mut self) -> Result<(), Error> { + self.spawn()?.wait()?.check() + } } /// A struct represending a command. Trying to execute it will just print its arguments. diff --git a/src/steps/generic.rs b/src/steps/generic.rs index 2aef7da6..c43eeedd 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -18,9 +18,7 @@ pub fn run_cargo_update(run_type: RunType) -> Option<(&'static str, bool)> { run_type .execute(cargo_update) .args(&["install-update", "--git", "--all"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() @@ -39,12 +37,7 @@ pub fn run_gem(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, print_separator("RubyGems"); let success = || -> Result<(), Error> { - run_type - .execute(&gem) - .args(&["update", "--user-install"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&gem).args(&["update", "--user-install"]).check_run()?; Ok(()) }() @@ -66,9 +59,7 @@ pub fn run_emacs(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static st run_type .execute(&emacs) .args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() @@ -95,9 +86,7 @@ pub fn run_apm(run_type: RunType) -> Option<(&'static str, bool)> { run_type .execute(&apm) .args(&["upgrade", "--confirm=false"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() @@ -116,15 +105,10 @@ pub fn run_rustup(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static s let success = || -> Result<(), Error> { if rustup.is_descendant_of(base_dirs.home_dir()) { - run_type - .execute(&rustup) - .args(&["self", "update"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&rustup).args(&["self", "update"]).check_run()?; } - run_type.execute(&rustup).arg("update").spawn()?.wait()?; + run_type.execute(&rustup).arg("update").check_run()?; Ok(()) }() .is_ok(); @@ -141,12 +125,7 @@ pub fn run_jetpack(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("Jetpack"); let success = || -> Result<(), Error> { - run_type - .execute(&jetpack) - .args(&["global", "update"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&jetpack).args(&["global", "update"]).check_run()?; Ok(()) }() .is_ok(); @@ -163,8 +142,8 @@ pub fn run_opam_update(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("OCaml Package Manager"); let success = || -> Result<(), Error> { - run_type.execute(&opam).arg("update").spawn()?.wait()?; - run_type.execute(&opam).arg("upgrade").spawn()?.wait()?; + run_type.execute(&opam).arg("update").check_run()?; + run_type.execute(&opam).arg("upgrade").check_run()?; Ok(()) }() .is_ok(); @@ -184,9 +163,7 @@ pub fn run_vcpkg_update(run_type: RunType) -> Option<(&'static str, bool)> { run_type .execute(&vcpkg) .args(&["upgrade", "--no-dry-run"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() .is_ok(); @@ -203,7 +180,7 @@ pub fn run_pipx_update(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("pipx"); let success = || -> Result<(), Error> { - run_type.execute(&pipx).arg("upgrade-all").spawn()?.wait()?.check()?; + run_type.execute(&pipx).arg("upgrade-all").check_run()?; Ok(()) }() .is_ok(); @@ -217,7 +194,7 @@ pub fn run_pipx_update(run_type: RunType) -> Option<(&'static str, bool)> { #[must_use] pub fn run_custom_command(name: &str, command: &str, run_type: RunType) -> Result<(), Error> { print_separator(name); - run_type.execute("sh").arg("-c").arg(command).spawn()?.wait()?.check()?; + run_type.execute("sh").arg("-c").arg(command).check_run()?; Ok(()) } @@ -241,15 +218,10 @@ pub fn run_composer_update(base_dirs: &BaseDirs, run_type: RunType) -> Option<(& print_separator("Composer"); let success = || -> Result<(), Error> { - run_type - .execute(&composer) - .args(&["global", "update"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&composer).args(&["global", "update"]).check_run()?; if let Some(valet) = utils::which("valet") { - run_type.execute(&valet).arg("install").spawn()?.wait()?; + run_type.execute(&valet).arg("install").check_run()?; } Ok(()) diff --git a/src/steps/git.rs b/src/steps/git.rs index 6dcb055a..bd08adac 100644 --- a/src/steps/git.rs +++ b/src/steps/git.rs @@ -1,7 +1,7 @@ use crate::error::Error; use crate::executor::RunType; use crate::terminal::print_separator; -use crate::utils::{which, Check}; +use crate::utils::which; use log::{debug, error}; use std::collections::HashSet; use std::io; @@ -70,17 +70,13 @@ impl Git { .execute(git) .args(&["pull", "--rebase", "--autostash"]) .current_dir(&path) - .spawn()? - .wait()? - .check()?; + .check_run()?; run_type .execute(git) .args(&["submodule", "update", "--init", "--recursive"]) .current_dir(&path) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() diff --git a/src/steps/node.rs b/src/steps/node.rs index 1d5edc27..be98b559 100644 --- a/src/steps/node.rs +++ b/src/steps/node.rs @@ -30,12 +30,7 @@ impl NPM { } fn upgrade(&self, run_type: RunType) -> Result<(), Error> { - run_type - .execute(&self.command) - .args(&["update", "-g"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&self.command).args(&["update", "-g"]).check_run()?; Ok(()) } @@ -61,12 +56,7 @@ pub fn yarn_global_update(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("Yarn"); let success = || -> Result<(), Error> { - run_type - .execute(&yarn) - .args(&["global", "upgrade", "-s"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&yarn).args(&["global", "upgrade", "-s"]).check_run()?; Ok(()) }() .is_ok(); diff --git a/src/steps/os/freebsd.rs b/src/steps/os/freebsd.rs index d4e40eea..8c2136dc 100644 --- a/src/steps/os/freebsd.rs +++ b/src/steps/os/freebsd.rs @@ -15,9 +15,7 @@ pub fn upgrade_freebsd(sudo: &Option, run_type: RunType) -> Option<(&'s run_type .execute(sudo) .args(&["/usr/sbin/freebsd-update", "fetch", "install"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() .is_ok(); @@ -35,12 +33,7 @@ pub fn upgrade_packages(sudo: &Option, run_type: RunType) -> Option<(&' if let Some(sudo) = sudo { let success = || -> Result<(), Error> { - run_type - .execute(sudo) - .args(&["/usr/sbin/pkg", "upgrade"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(sudo).args(&["/usr/sbin/pkg", "upgrade"]).check_run()?; Ok(()) }() .is_ok(); diff --git a/src/steps/os/linux.rs b/src/steps/os/linux.rs index 36c39a3e..3bd99ebc 100644 --- a/src/steps/os/linux.rs +++ b/src/steps/os/linux.rs @@ -1,7 +1,7 @@ use crate::error::{Error, ErrorKind}; use crate::executor::RunType; use crate::terminal::{print_separator, print_warning}; -use crate::utils::{which, Check}; +use crate::utils::which; use failure::ResultExt; use std::fs; use std::path::PathBuf; @@ -115,26 +115,16 @@ It's dangerous to run yay since Python based AUR packages will be installed in t return Err(ErrorKind::NotSystemPython)?; } } - run_type.execute(yay).spawn()?.wait()?.check()?; + run_type.execute(yay).check_run()?; } else if let Some(sudo) = &sudo { - run_type - .execute(&sudo) - .args(&["/usr/bin/pacman", "-Syu"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).args(&["/usr/bin/pacman", "-Syu"]).check_run()?; } else { print_warning("No sudo or yay detected. Skipping system upgrade"); } if cleanup { if let Some(sudo) = &sudo { - run_type - .execute(&sudo) - .args(&["/usr/bin/pacman", "-Scc"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).args(&["/usr/bin/pacman", "-Scc"]).check_run()?; } } @@ -143,12 +133,7 @@ It's dangerous to run yay since Python based AUR packages will be installed in t fn upgrade_redhat(sudo: &Option, run_type: RunType) -> Result<(), Error> { if let Some(sudo) = &sudo { - run_type - .execute(&sudo) - .args(&["/usr/bin/yum", "upgrade"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).args(&["/usr/bin/yum", "upgrade"]).check_run()?; } else { print_warning("No sudo detected. Skipping system upgrade"); } @@ -161,16 +146,12 @@ fn upgrade_opensuse(sudo: &Option, run_type: RunType) -> Result<(), Err run_type .execute(&sudo) .args(&["/usr/bin/zypper", "refresh"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; run_type .execute(&sudo) .args(&["/usr/bin/zypper", "dist-upgrade"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; } else { print_warning("No sudo detected. Skipping system upgrade"); } @@ -183,9 +164,7 @@ fn upgrade_void(sudo: &Option, run_type: RunType) -> Result<(), Error> run_type .execute(&sudo) .args(&["/usr/bin/xbps-install", "-Su"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; } else { print_warning("No sudo detected. Skipping system upgrade"); } @@ -195,12 +174,7 @@ fn upgrade_void(sudo: &Option, run_type: RunType) -> Result<(), Error> fn upgrade_fedora(sudo: &Option, run_type: RunType) -> Result<(), Error> { if let Some(sudo) = &sudo { - run_type - .execute(&sudo) - .args(&["/usr/bin/dnf", "upgrade"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).args(&["/usr/bin/dnf", "upgrade"]).check_run()?; } else { print_warning("No sudo detected. Skipping system upgrade"); } @@ -211,13 +185,7 @@ fn upgrade_fedora(sudo: &Option, run_type: RunType) -> Result<(), Error fn upgrade_gentoo(sudo: &Option, run_type: RunType) -> Result<(), Error> { if let Some(sudo) = &sudo { if let Some(layman) = which("layman") { - run_type - .execute(&sudo) - .arg(layman) - .args(&["-s", "ALL"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).arg(layman).args(&["-s", "ALL"]).check_run()?; } println!("Syncing portage"); @@ -225,21 +193,17 @@ fn upgrade_gentoo(sudo: &Option, run_type: RunType) -> Result<(), Error .execute(&sudo) .arg("/usr/bin/emerge") .args(&["-q", "--sync"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; if let Some(eix_update) = which("eix-update") { - run_type.execute(&sudo).arg(eix_update).spawn()?.wait()?; + run_type.execute(&sudo).arg(eix_update).check_run()?; } run_type .execute(&sudo) .arg("/usr/bin/emerge") .args(&["-uDNa", "world"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; } else { print_warning("No sudo detected. Skipping system upgrade"); } @@ -249,34 +213,20 @@ fn upgrade_gentoo(sudo: &Option, run_type: RunType) -> Result<(), Error fn upgrade_debian(sudo: &Option, cleanup: bool, run_type: RunType) -> Result<(), Error> { if let Some(sudo) = &sudo { - run_type - .execute(&sudo) - .args(&["/usr/bin/apt", "update"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).args(&["/usr/bin/apt", "update"]).check_run()?; run_type .execute(&sudo) .args(&["/usr/bin/apt", "dist-upgrade"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; if cleanup { - run_type - .execute(&sudo) - .args(&["/usr/bin/apt", "clean"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).args(&["/usr/bin/apt", "clean"]).check_run()?; run_type .execute(&sudo) .args(&["/usr/bin/apt", "autoremove"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; } } else { print_warning("No sudo detected. Skipping system upgrade"); @@ -292,7 +242,7 @@ pub fn run_needrestart(sudo: &Option, run_type: RunType) -> Option<(&'s print_separator("Check for needed restarts"); let success = || -> Result<(), Error> { - run_type.execute(&sudo).arg(needrestart).spawn()?.wait()?.check()?; + run_type.execute(&sudo).arg(needrestart).check_run()?; Ok(()) }() @@ -311,13 +261,8 @@ pub fn run_fwupdmgr(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("Firmware upgrades"); let success = || -> Result<(), Error> { - run_type.execute(&fwupdmgr).arg("refresh").spawn()?.wait()?.check()?; - run_type - .execute(&fwupdmgr) - .arg("get-updates") - .spawn()? - .wait()? - .check()?; + run_type.execute(&fwupdmgr).arg("refresh").check_run()?; + run_type.execute(&fwupdmgr).arg("get-updates").check_run()?; Ok(()) }() .is_ok(); @@ -337,9 +282,7 @@ pub fn flatpak_user_update(run_type: RunType) -> Option<(&'static str, bool)> { run_type .execute(&flatpak) .args(&["update", "--user", "-y"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() .is_ok(); @@ -360,9 +303,7 @@ pub fn flatpak_global_update(sudo: &Option, run_type: RunType) -> Optio run_type .execute(&sudo) .args(&[flatpak.to_str().unwrap(), "update", "-y"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() .is_ok(); @@ -385,9 +326,7 @@ pub fn run_snap(sudo: &Option, run_type: RunType) -> Option<(&'static s run_type .execute(&sudo) .args(&[snap.to_str().unwrap(), "refresh"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() @@ -408,12 +347,7 @@ pub fn run_etc_update(sudo: &Option, run_type: RunType) -> Option<(&'st print_separator("etc-update"); let success = || -> Result<(), Error> { - run_type - .execute(&sudo) - .arg(&etc_update.to_str().unwrap()) - .spawn()? - .wait()? - .check()?; + run_type.execute(&sudo).arg(&etc_update.to_str().unwrap()).check_run()?; Ok(()) }() diff --git a/src/steps/os/macos.rs b/src/steps/os/macos.rs index ddf7eed3..b8b5dc8f 100644 --- a/src/steps/os/macos.rs +++ b/src/steps/os/macos.rs @@ -1,7 +1,6 @@ use crate::error::Error; use crate::executor::RunType; use crate::terminal::print_separator; -use crate::utils::Check; #[must_use] pub fn upgrade_macos(run_type: RunType) -> Option<(&'static str, bool)> { @@ -11,9 +10,7 @@ pub fn upgrade_macos(run_type: RunType) -> Option<(&'static str, bool)> { run_type .execute("softwareupdate") .args(&["--install", "--all"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() .is_ok(); diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index b0e80f30..32f51836 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -1,7 +1,7 @@ use crate::error::Error; use crate::executor::RunType; use crate::terminal::print_separator; -use crate::utils::{which, Check}; +use crate::utils::which; use directories::BaseDirs; pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { @@ -13,9 +13,7 @@ pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static st run_type .execute(zsh) .args(&["-c", "source ~/.zshrc && zplug update"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() .is_ok(); @@ -36,15 +34,8 @@ pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static s run_type .execute(&fish) .args(&["-c", "fisher self-update"]) - .spawn()? - .wait()? - .check()?; - run_type - .execute(&fish) - .args(&["-c", "fisher"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; + run_type.execute(&fish).args(&["-c", "fisher"]).check_run()?; Ok(()) }() .is_ok(); @@ -62,16 +53,11 @@ pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Option<(&'static str, b print_separator("Brew"); let inner = || -> Result<(), Error> { - run_type.execute(&brew).arg("update").spawn()?.wait()?; - run_type.execute(&brew).arg("upgrade").spawn()?.wait()?; - run_type - .execute(&brew) - .args(&["cask", "upgrade"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&brew).arg("update").check_run()?; + run_type.execute(&brew).arg("upgrade").check_run()?; + run_type.execute(&brew).args(&["cask", "upgrade"]).check_run()?; if cleanup { - run_type.execute(&brew).arg("cleanup").spawn()?.wait()?; + run_type.execute(&brew).arg("cleanup").check_run()?; } Ok(()) }; @@ -89,8 +75,8 @@ pub fn run_nix(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("Nix"); let inner = || -> Result<(), Error> { - run_type.execute(&nix).arg("upgrade-nix").spawn()?.wait()?.check()?; - run_type.execute(&nix_env).arg("--upgrade").spawn()?.wait()?.check()?; + run_type.execute(&nix).arg("upgrade-nix").check_run()?; + run_type.execute(&nix_env).arg("--upgrade").check_run()?; Ok(()) }; diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index 23ae52e5..6a209656 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -13,12 +13,7 @@ pub fn run_chocolatey(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("Chocolatey"); let success = || -> Result<(), Error> { - run_type - .execute(&choco) - .args(&["upgrade", "all"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&choco).args(&["upgrade", "all"]).check_run()?; Ok(()) }() .is_ok(); @@ -35,13 +30,8 @@ pub fn run_scoop(run_type: RunType) -> Option<(&'static str, bool)> { print_separator("Scoop"); let success = || -> Result<(), Error> { - run_type.execute(&scoop).args(&["update"]).spawn()?.wait()?.check()?; - run_type - .execute(&scoop) - .args(&["update", "*"]) - .spawn()? - .wait()? - .check()?; + run_type.execute(&scoop).args(&["update"]).check_run()?; + run_type.execute(&scoop).args(&["update", "*"]).check_run()?; Ok(()) }() .is_ok(); @@ -102,12 +92,7 @@ impl Powershell { print_separator("Powershell Modules Update"); let success = || -> Result<(), Error> { - run_type - .execute(&powershell) - .arg("Update-Module") - .spawn()? - .wait()? - .check()?; + run_type.execute(&powershell).arg("Update-Module").check_run()?; Ok(()) }() .is_ok(); @@ -128,9 +113,7 @@ impl Powershell { run_type .execute(&powershell) .args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"]) - .spawn()? - .wait()? - .check()?; + .check_run()?; Ok(()) }() .is_ok(); diff --git a/src/steps/tmux.rs b/src/steps/tmux.rs index 93c2f0ab..60dff9b6 100644 --- a/src/steps/tmux.rs +++ b/src/steps/tmux.rs @@ -19,7 +19,7 @@ pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, print_separator("tmux plugins"); let success = || -> Result<(), Error> { - run_type.execute(&tpm).arg("all").spawn()?.wait()?; + run_type.execute(&tpm).arg("all").check_run()?; Ok(()) }() .is_ok(); diff --git a/src/steps/vim.rs b/src/steps/vim.rs index b617e696..045541f9 100644 --- a/src/steps/vim.rs +++ b/src/steps/vim.rs @@ -1,7 +1,7 @@ use crate::error::Error; use crate::executor::RunType; use crate::terminal::print_separator; -use crate::utils::{which, Check, PathExt}; +use crate::utils::{which, PathExt}; use directories::BaseDirs; use std::fs; use std::path::PathBuf; @@ -69,9 +69,7 @@ fn upgrade(vim: &PathBuf, vimrc: &PathBuf, plugin_framework: PluginFramework, ru "-s", "-V1", ]) - .spawn()? - .wait()? - .check()?; + .check_run()?; println!();