Step refactoring

This commit is contained in:
Roey Darwish Dror
2019-01-13 23:20:32 +02:00
parent 4c2f4171dc
commit a8379fdda2
4 changed files with 243 additions and 210 deletions

View File

@@ -41,6 +41,9 @@ pub enum ErrorKind {
#[fail(display = "Self-update failure")] #[fail(display = "Self-update failure")]
#[cfg(feature = "self-update")] #[cfg(feature = "self-update")]
SelfUpdate, SelfUpdate,
#[fail(display = "A step should be skipped")]
SkipStep,
} }
impl Fail for Error { impl Fail for Error {

View File

@@ -15,14 +15,16 @@ use self::report::Report;
use self::steps::*; use self::steps::*;
use self::terminal::*; use self::terminal::*;
use failure::{Fail, ResultExt}; use failure::{Fail, ResultExt};
use log::debug;
use std::borrow::Cow; use std::borrow::Cow;
use std::env; use std::env;
use std::fmt::Debug;
use std::io; use std::io;
#[cfg(windows)] #[cfg(windows)]
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
fn execute<'a, F, M>(func: F, no_retry: bool) -> Result<Option<(M, bool)>, Error> fn execute_legacy<'a, F, M>(func: F, no_retry: bool) -> Result<Option<(M, bool)>, Error>
where where
M: Into<Cow<'a, str>>, M: Into<Cow<'a, str>>,
F: Fn() -> Option<(M, bool)>, F: Fn() -> Option<(M, bool)>,
@@ -48,6 +50,42 @@ where
Ok(None) Ok(None)
} }
fn execute<'a, F, M>(report: &mut Report<'a>, key: M, func: F, no_retry: bool) -> Result<(), Error>
where
F: Fn() -> Result<(), Error>,
M: Into<Cow<'a, str>> + Debug,
{
debug!("Executing {:?}", key);
loop {
match func() {
Ok(()) => {
report.push_result(Some((key, true)));
break;
}
Err(ref e) if e.kind() == ErrorKind::SkipStep => {
break;
}
Err(_) => {
let interrupted = ctrlc::interrupted();
if interrupted {
ctrlc::unset_interrupted();
}
let should_ask = interrupted || !no_retry;
let should_retry = should_ask && should_retry(interrupted).context(ErrorKind::Retry)?;
if !should_retry {
report.push_result(Some((key, false)));
break;
}
}
}
}
Ok(())
}
fn run() -> Result<(), Error> { fn run() -> Result<(), Error> {
ctrlc::set_handler(); ctrlc::set_handler();
@@ -96,7 +134,10 @@ fn run() -> Result<(), Error> {
#[cfg(windows)] #[cfg(windows)]
{ {
if powershell.profile().is_some() && config.should_run(Step::Powershell) { if powershell.profile().is_some() && config.should_run(Step::Powershell) {
report.push_result(execute(|| powershell.update_modules(run_type), config.no_retry())?); report.push_result(execute_legacy(
|| powershell.update_modules(run_type),
config.no_retry(),
)?);
} }
} }
@@ -108,7 +149,7 @@ fn run() -> Result<(), Error> {
if config.should_run(Step::System) { if config.should_run(Step::System) {
match &distribution { match &distribution {
Ok(distribution) => { Ok(distribution) => {
report.push_result(execute( report.push_result(execute_legacy(
|| distribution.upgrade(&sudo, config.cleanup(), run_type), || distribution.upgrade(&sudo, config.cleanup(), run_type),
config.no_retry(), config.no_retry(),
)?); )?);
@@ -117,28 +158,31 @@ fn run() -> Result<(), Error> {
println!("Error detecting current distribution: {}", e); println!("Error detecting current distribution: {}", e);
} }
} }
report.push_result(execute(|| linux::run_etc_update(&sudo, run_type), config.no_retry())?); report.push_result(execute_legacy(
|| linux::run_etc_update(&sudo, run_type),
config.no_retry(),
)?);
} }
} }
#[cfg(windows)] #[cfg(windows)]
report.push_result(execute(|| windows::run_chocolatey(run_type), config.no_retry())?); report.push_result(execute_legacy(|| windows::run_chocolatey(run_type), config.no_retry())?);
#[cfg(windows)] #[cfg(windows)]
report.push_result(execute(|| windows::run_scoop(run_type), config.no_retry())?); report.push_result(execute_legacy(|| windows::run_scoop(run_type), config.no_retry())?);
#[cfg(unix)] #[cfg(unix)]
report.push_result(execute( report.push_result(execute_legacy(
|| unix::run_homebrew(config.cleanup(), run_type), || unix::run_homebrew(config.cleanup(), run_type),
config.no_retry(), config.no_retry(),
)?); )?);
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
report.push_result(execute( report.push_result(execute_legacy(
|| freebsd::upgrade_packages(&sudo, run_type), || freebsd::upgrade_packages(&sudo, run_type),
config.no_retry(), config.no_retry(),
)?); )?);
#[cfg(unix)] #[cfg(unix)]
report.push_result(execute(|| unix::run_nix(run_type), config.no_retry())?); report.push_result(execute_legacy(|| unix::run_nix(run_type), config.no_retry())?);
if config.should_run(Step::Emacs) { if config.should_run(Step::Emacs) {
#[cfg(unix)] #[cfg(unix)]
@@ -182,48 +226,97 @@ fn run() -> Result<(), Error> {
} }
} }
for repo in git_repos.repositories() { for repo in git_repos.repositories() {
report.push_result(execute(|| git.pull(&repo, run_type), config.no_retry())?); report.push_result(execute_legacy(|| git.pull(&repo, run_type), config.no_retry())?);
} }
#[cfg(unix)] #[cfg(unix)]
{ {
report.push_result(execute(|| unix::run_zplug(&base_dirs, run_type), config.no_retry())?); report.push_result(execute_legacy(
report.push_result(execute(|| unix::run_fisher(&base_dirs, run_type), config.no_retry())?); || unix::run_zplug(&base_dirs, run_type),
report.push_result(execute(|| tmux::run_tpm(&base_dirs, run_type), config.no_retry())?); config.no_retry(),
)?);
report.push_result(execute_legacy(
|| unix::run_fisher(&base_dirs, run_type),
config.no_retry(),
)?);
report.push_result(execute_legacy(
|| tmux::run_tpm(&base_dirs, run_type),
config.no_retry(),
)?);
} }
report.push_result(execute( execute(
&mut report,
"rustup",
|| generic::run_rustup(&base_dirs, run_type), || generic::run_rustup(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
report.push_result(execute(|| generic::run_cargo_update(run_type), config.no_retry())?); execute(
&mut report,
"cargo",
|| generic::run_cargo_update(run_type),
config.no_retry(),
)?;
if config.should_run(Step::Emacs) { if config.should_run(Step::Emacs) {
report.push_result(execute(|| generic::run_emacs(&base_dirs, run_type), config.no_retry())?); execute(
&mut report,
"Emacs",
|| generic::run_emacs(&base_dirs, run_type),
config.no_retry(),
)?;
} }
report.push_result(execute(|| generic::run_opam_update(run_type), config.no_retry())?); execute(
report.push_result(execute(|| generic::run_vcpkg_update(run_type), config.no_retry())?); &mut report,
report.push_result(execute(|| generic::run_pipx_update(run_type), config.no_retry())?); "opam",
report.push_result(execute(|| generic::run_jetpack(run_type), config.no_retry())?); || generic::run_opam_update(run_type),
config.no_retry(),
)?;
execute(
&mut report,
"vcpkg",
|| generic::run_vcpkg_update(run_type),
config.no_retry(),
)?;
execute(
&mut report,
"pipx",
|| generic::run_pipx_update(run_type),
config.no_retry(),
)?;
execute(
&mut report,
"jetpak",
|| generic::run_jetpack(run_type),
config.no_retry(),
)?;
if config.should_run(Step::Vim) { if config.should_run(Step::Vim) {
report.push_result(execute(|| vim::upgrade_vim(&base_dirs, run_type), config.no_retry())?); report.push_result(execute_legacy(
report.push_result(execute( || vim::upgrade_vim(&base_dirs, run_type),
config.no_retry(),
)?);
report.push_result(execute_legacy(
|| vim::upgrade_neovim(&base_dirs, run_type), || vim::upgrade_neovim(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?);
} }
report.push_result(execute( report.push_result(execute_legacy(
|| node::run_npm_upgrade(&base_dirs, run_type), || node::run_npm_upgrade(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?);
report.push_result(execute( execute(
&mut report,
"composer",
|| generic::run_composer_update(&base_dirs, run_type), || generic::run_composer_update(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?;
report.push_result(execute_legacy(
|| node::yarn_global_update(run_type),
config.no_retry(),
)?); )?);
report.push_result(execute(|| node::yarn_global_update(run_type), config.no_retry())?);
#[cfg(not(any( #[cfg(not(any(
target_os = "freebsd", target_os = "freebsd",
@@ -231,21 +324,26 @@ fn run() -> Result<(), Error> {
target_os = "netbsd", target_os = "netbsd",
target_os = "dragonfly" target_os = "dragonfly"
)))] )))]
report.push_result(execute(|| generic::run_apm(run_type), config.no_retry())?); execute(&mut report, "apm", || generic::run_apm(run_type), config.no_retry())?;
if config.should_run(Step::Gem) { if config.should_run(Step::Gem) {
report.push_result(execute(|| generic::run_gem(&base_dirs, run_type), config.no_retry())?); execute(
&mut report,
"gem",
|| generic::run_gem(&base_dirs, run_type),
config.no_retry(),
)?;
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
report.push_result(execute(|| linux::flatpak_update(run_type), config.no_retry())?); report.push_result(execute_legacy(|| linux::flatpak_update(run_type), config.no_retry())?);
report.push_result(execute(|| linux::run_snap(&sudo, run_type), config.no_retry())?); report.push_result(execute_legacy(|| linux::run_snap(&sudo, run_type), config.no_retry())?);
} }
if let Some(commands) = config.commands() { if let Some(commands) = config.commands() {
for (name, command) in commands { for (name, command) in commands {
report.push_result(execute( report.push_result(execute_legacy(
|| Some((name, generic::run_custom_command(&name, &command, run_type).is_ok())), || Some((name, generic::run_custom_command(&name, &command, run_type).is_ok())),
config.no_retry(), config.no_retry(),
)?); )?);
@@ -254,21 +352,24 @@ fn run() -> Result<(), Error> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
report.push_result(execute(|| linux::run_fwupdmgr(run_type), config.no_retry())?); report.push_result(execute_legacy(|| linux::run_fwupdmgr(run_type), config.no_retry())?);
report.push_result(execute(|| linux::run_needrestart(&sudo, run_type), config.no_retry())?); report.push_result(execute_legacy(
|| linux::run_needrestart(&sudo, run_type),
config.no_retry(),
)?);
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
if config.should_run(Step::System) { if config.should_run(Step::System) {
report.push_result(execute(|| macos::upgrade_macos(run_type), config.no_retry())?); report.push_result(execute_legacy(|| macos::upgrade_macos(run_type), config.no_retry())?);
} }
} }
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
{ {
if config.should_run(Step::System) { if config.should_run(Step::System) {
report.push_result(execute( report.push_result(execute_legacy(
|| freebsd::upgrade_freebsd(&sudo, run_type), || freebsd::upgrade_freebsd(&sudo, run_type),
config.no_retry(), config.no_retry(),
)?); )?);
@@ -278,7 +379,10 @@ fn run() -> Result<(), Error> {
#[cfg(windows)] #[cfg(windows)]
{ {
if config.should_run(Step::System) { if config.should_run(Step::System) {
report.push_result(execute(|| powershell.windows_update(run_type), config.no_retry())?); report.push_result(execute_legacy(
|| powershell.windows_update(run_type),
config.no_retry(),
)?);
} }
} }

View File

@@ -1,4 +1,4 @@
use crate::error::Error; use crate::error::{Error, ErrorKind};
use crate::executor::{CommandExt, RunType}; use crate::executor::{CommandExt, RunType};
use crate::terminal::print_separator; use crate::terminal::print_separator;
use crate::utils::{self, PathExt}; use crate::utils::{self, PathExt};
@@ -8,223 +8,120 @@ use std::process::Command;
const EMACS_UPGRADE: &str = include_str!("emacs.el"); const EMACS_UPGRADE: &str = include_str!("emacs.el");
#[must_use] pub fn run_cargo_update(run_type: RunType) -> Result<(), Error> {
pub fn run_cargo_update(run_type: RunType) -> Option<(&'static str, bool)> { let cargo_update = utils::require("cargo-install-update")?;
if let Some(cargo_update) = utils::which("cargo-install-update") {
print_separator("Cargo");
let success = || -> Result<(), Error> { print_separator("Cargo");
run_type
.execute(cargo_update)
.args(&["install-update", "--git", "--all"])
.check_run()?;
Ok(()) run_type
}() .execute(cargo_update)
.is_ok(); .args(&["install-update", "--git", "--all"])
.check_run()
return Some(("Cargo", success));
}
None
} }
#[must_use] pub fn run_gem(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
pub fn run_gem(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { let gem = utils::require("gem")?;
if let Some(gem) = utils::which("gem") { base_dirs.home_dir().join(".gem").require()?;
if base_dirs.home_dir().join(".gem").exists() {
print_separator("RubyGems");
let success = || -> Result<(), Error> { print_separator("RubyGems");
run_type.execute(&gem).args(&["update", "--user-install"]).check_run()?;
Ok(()) run_type.execute(&gem).args(&["update", "--user-install"]).check_run()
}()
.is_ok();
return Some(("RubyGems", success));
}
}
None
} }
#[must_use] pub fn run_emacs(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
pub fn run_emacs(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { let emacs = utils::require("emacs")?;
if let Some(emacs) = utils::which("emacs") { let init_file = base_dirs.home_dir().join(".emacs.d/init.el").require()?;
if let Some(init_file) = base_dirs.home_dir().join(".emacs.d/init.el").if_exists() {
print_separator("Emacs");
let success = || -> Result<(), Error> { print_separator("Emacs");
run_type
.execute(&emacs)
.args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE])
.check_run()?;
Ok(()) run_type
}() .execute(&emacs)
.is_ok(); .args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE])
.check_run()
return Some(("Emacs", success));
}
}
None
} }
#[must_use]
#[cfg(not(any( #[cfg(not(any(
target_os = "freebsd", target_os = "freebsd",
target_os = "openbsd", target_os = "openbsd",
target_os = "netbsd", target_os = "netbsd",
target_os = "dragonfly" target_os = "dragonfly"
)))] )))]
pub fn run_apm(run_type: RunType) -> Option<(&'static str, bool)> { pub fn run_apm(run_type: RunType) -> Result<(), Error> {
if let Some(apm) = utils::which("apm") { let apm = utils::require("apm")?;
print_separator("Atom Package Manager");
let success = || -> Result<(), Error> { print_separator("Atom Package Manager");
run_type
.execute(&apm)
.args(&["upgrade", "--confirm=false"])
.check_run()?;
Ok(()) run_type.execute(&apm).args(&["upgrade", "--confirm=false"]).check_run()
}()
.is_ok();
return Some(("apm", success));
}
None
} }
#[must_use] pub fn run_rustup(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
pub fn run_rustup(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { let rustup = utils::require("rustup")?;
if let Some(rustup) = utils::which("rustup") {
print_separator("rustup");
let success = || -> Result<(), Error> { print_separator("rustup");
if rustup.is_descendant_of(base_dirs.home_dir()) {
run_type.execute(&rustup).args(&["self", "update"]).check_run()?;
}
run_type.execute(&rustup).arg("update").check_run()?; if rustup.is_descendant_of(base_dirs.home_dir()) {
Ok(()) run_type.execute(&rustup).args(&["self", "update"]).check_run()?;
}()
.is_ok();
return Some(("rustup", success));
} }
None run_type.execute(&rustup).arg("update").check_run()
} }
#[must_use] pub fn run_jetpack(run_type: RunType) -> Result<(), Error> {
pub fn run_jetpack(run_type: RunType) -> Option<(&'static str, bool)> { let jetpack = utils::require("jetpack")?;
if let Some(jetpack) = utils::which("jetpack") {
print_separator("Jetpack");
let success = || -> Result<(), Error> { print_separator("Jetpack");
run_type.execute(&jetpack).args(&["global", "update"]).check_run()?;
Ok(())
}()
.is_ok();
return Some(("Jetpack", success)); run_type.execute(&jetpack).args(&["global", "update"]).check_run()
}
None
} }
#[must_use] pub fn run_opam_update(run_type: RunType) -> Result<(), Error> {
pub fn run_opam_update(run_type: RunType) -> Option<(&'static str, bool)> { let opam = utils::require("opam")?;
if let Some(opam) = utils::which("opam") {
print_separator("OCaml Package Manager");
let success = || -> Result<(), Error> { print_separator("OCaml Package Manager");
run_type.execute(&opam).arg("update").check_run()?;
run_type.execute(&opam).arg("upgrade").check_run()?;
Ok(())
}()
.is_ok();
return Some(("OPAM", success)); run_type.execute(&opam).arg("update").check_run()?;
} run_type.execute(&opam).arg("upgrade").check_run()
None
} }
#[must_use] pub fn run_vcpkg_update(run_type: RunType) -> Result<(), Error> {
pub fn run_vcpkg_update(run_type: RunType) -> Option<(&'static str, bool)> { let vcpkg = utils::require("vcpkg")?;
if let Some(vcpkg) = utils::which("vcpkg") { print_separator("vcpkg");
print_separator("vcpkg");
let success = || -> Result<(), Error> { run_type.execute(&vcpkg).args(&["upgrade", "--no-dry-run"]).check_run()
run_type
.execute(&vcpkg)
.args(&["upgrade", "--no-dry-run"])
.check_run()?;
Ok(())
}()
.is_ok();
return Some(("vcpkg", success));
}
None
} }
#[must_use] pub fn run_pipx_update(run_type: RunType) -> Result<(), Error> {
pub fn run_pipx_update(run_type: RunType) -> Option<(&'static str, bool)> { let pipx = utils::require("pipx")?;
if let Some(pipx) = utils::which("pipx") { print_separator("pipx");
print_separator("pipx");
let success = || -> Result<(), Error> { run_type.execute(&pipx).arg("upgrade-all").check_run()
run_type.execute(&pipx).arg("upgrade-all").check_run()?;
Ok(())
}()
.is_ok();
return Some(("pipx", success));
}
None
} }
#[must_use]
pub fn run_custom_command(name: &str, command: &str, run_type: RunType) -> Result<(), Error> { pub fn run_custom_command(name: &str, command: &str, run_type: RunType) -> Result<(), Error> {
print_separator(name); print_separator(name);
run_type.execute("sh").arg("-c").arg(command).check_run()?; run_type.execute("sh").arg("-c").arg(command).check_run()
}
pub fn run_composer_update(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
let composer = utils::require("composer")?;
let composer_home = Command::new(&composer)
.args(&["global", "config", "--absolute", "home"])
.check_output()
.map_err(|_| Error::from(ErrorKind::SkipStep))
.map(PathBuf::from)
.and_then(|p| p.require())?;
if !composer_home.is_descendant_of(base_dirs.home_dir()) {
Err(ErrorKind::SkipStep)?;
}
print_separator("Composer");
run_type.execute(&composer).args(&["global", "update"]).check_run()?;
if let Some(valet) = utils::which("valet") {
run_type.execute(&valet).arg("install").check_run()?;
}
Ok(()) Ok(())
} }
#[must_use]
pub fn run_composer_update(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> {
if let Some(composer) = utils::which("composer") {
let composer_home = Command::new(&composer)
.args(&["global", "config", "--absolute", "home"])
.check_output()
.map(PathBuf::from);
if let Ok(composer_home) = composer_home {
if composer_home.is_descendant_of(base_dirs.home_dir()) {
print_separator("Composer");
let success = || -> Result<(), Error> {
run_type.execute(&composer).args(&["global", "update"]).check_run()?;
if let Some(valet) = utils::which("valet") {
run_type.execute(&valet).arg("install").check_run()?;
}
Ok(())
}()
.is_ok();
return Some(("Composer", success));
}
}
}
None
}

View File

@@ -32,6 +32,9 @@ where
{ {
fn if_exists(self) -> Option<Self>; fn if_exists(self) -> Option<Self>;
fn is_descendant_of(&self, ancestor: &Path) -> bool; fn is_descendant_of(&self, ancestor: &Path) -> bool;
/// Returns the path if it exists or ErrorKind::SkipStep otherwise
fn require(self) -> Result<Self, Error>;
} }
impl PathExt for PathBuf { impl PathExt for PathBuf {
@@ -46,6 +49,14 @@ impl PathExt for PathBuf {
fn is_descendant_of(&self, ancestor: &Path) -> bool { fn is_descendant_of(&self, ancestor: &Path) -> bool {
self.iter().zip(ancestor.iter()).all(|(a, b)| a == b) self.iter().zip(ancestor.iter()).all(|(a, b)| a == b)
} }
fn require(self) -> Result<Self, Error> {
if self.exists() {
Ok(self)
} else {
Err(ErrorKind::SkipStep)?
}
}
} }
pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> { pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> {
@@ -151,3 +162,21 @@ mod tests {
assert_eq!("C:\\hi", humanize("//?/C:/hi")); assert_eq!("C:\\hi", humanize("//?/C:/hi"));
} }
} }
pub fn require<T: AsRef<OsStr> + Debug>(binary_name: T) -> Result<PathBuf, Error> {
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);
Err(ErrorKind::SkipStep)?
}
_ => {
panic!("Detecting {:?} failed: {}", &binary_name, e);
}
},
}
}