From b54276863b8693e928ed88cab7e21e3a981cf96f Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Mon, 11 Jun 2018 08:29:40 +0300 Subject: [PATCH] Move the package manager step to the beginning (fixes #19) --- README.md | 18 +++++++-------- src/main.rs | 64 +++++++++++++++++++++++++++++----------------------- src/steps.rs | 25 +++++++------------- 3 files changed, 53 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 105d9bfc..81b76baf 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,17 @@ Other systems users can either use `cargo install` or use the compiled binaries ## Usage Just invoke `topgrade`. It will invoke the following steps: +* Invoke the system package manager: + * *Arch*: Invoke [yay](https://github.com/Jguer/yay) or fall back to pacman + * *CentOS/RHEL*: Invoke `yum upgrade` + * *Fedora* - Invoke `dnf upgrade` + * *Debian/Ubuntu*: Invoke `apt update && apt dist-upgrade` + * *macOS*: Invoke `brew update && brew upgrade` * Check if the following paths are tracked by Git. If so, pull them: * ~/.emacs.d (Should work whether you use [Spacemacs](http://spacemacs.org/) or a custom configuration) * ~/.zshrc * [~/.oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh) * ~/.tmux - * *Unix*: Invoke [zplug](https://github.com/zplug/zplug) update * *Unix*: Upgrade tmux plugins with [TPM](https://github.com/tmux-plugins/tpm) * Invoke Cargo [install-update](https://github.com/nabijaczleweli/cargo-update) @@ -30,12 +35,7 @@ Just invoke `topgrade`. It will invoke the following steps: * [Plug](https://github.com/junegunn/vim-plug) * Upgrade NPM globally installed packages * Upgrade Atom packages -* *Linux*: Invoke the system package manager: - * *Arch*: Invoke [yay](https://github.com/Jguer/yay) or fall back to pacman - * *CentOS/RHEL*: Invoke `yum upgrade` - * *Fedora* - Invoke `dnf upgrade` - * *Debian/Ubuntu*: Invoke `apt update && apt dist-upgrade` * *Linux*: Invoke [fwupdmgr](https://github.com/hughsie/fwupd) to show firmware upgrade. (View only. No upgrades will actually be performed) -* *Linux*: Run [needrestart](https://github.com/liske/needrestart) -* *macOS*: Upgrade [Homebrew](https://brew.sh/) packages -* *macOS*: Upgrade App Store applications +* Final stage + * *Linux*: Run [needrestart](https://github.com/liske/needrestart) + * *macOS*: Upgrade App Store applications diff --git a/src/main.rs b/src/main.rs index 81ecf87c..3ece0e12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,39 @@ fn main() -> Result<(), Error> { let mut reports = Report::new(); let config = Config::read()?; + let sudo = if cfg!(target_os = "linux") { + which("sudo").ok() + } else { + None + }; + + if cfg!(target_os = "linux") { + terminal.print_separator("System update"); + match linux::Distribution::detect() { + Ok(distribution) => { + match distribution { + linux::Distribution::Arch => upgrade_arch_linux(&sudo, &terminal), + linux::Distribution::CentOS => upgrade_redhat(&sudo, &terminal), + linux::Distribution::Fedora => upgrade_fedora(&sudo, &terminal), + linux::Distribution::Ubuntu | linux::Distribution::Debian => { + upgrade_debian(&sudo, &terminal) + } + }.report("System upgrade", &mut reports); + } + + Err(e) => { + println!("Error detecting current distribution: {}", e); + } + } + } + + if cfg!(target_os = "macos") { + if let Ok(brew) = which("brew") { + terminal.print_separator("Homebrew"); + run_homebrew(&brew).report("Homebrew", &mut reports); + } + } + git_repos.insert(home_path(".emacs.d")); if cfg!(unix) { @@ -163,32 +196,12 @@ fn main() -> Result<(), Error> { } if cfg!(target_os = "linux") { - let sudo = which("sudo"); - - terminal.print_separator("System update"); - match linux::Distribution::detect() { - Ok(distribution) => { - match distribution { - linux::Distribution::Arch => upgrade_arch_linux(&sudo, &terminal), - linux::Distribution::CentOS => upgrade_redhat(&sudo, &terminal), - linux::Distribution::Fedora => upgrade_fedora(&sudo, &terminal), - linux::Distribution::Ubuntu | linux::Distribution::Debian => { - upgrade_debian(&sudo, &terminal) - } - }.report("System upgrade", &mut reports); - } - - Err(e) => { - println!("Error detecting current distribution: {}", e); - } - } - if let Ok(fwupdmgr) = which("fwupdmgr") { terminal.print_separator("Firmware upgrades"); run_fwupdmgr(&fwupdmgr).report("Firmware upgrade", &mut reports); } - if let Ok(sudo) = &sudo { + if let Some(sudo) = &sudo { if let Ok(_) = which("needrestart") { terminal.print_separator("Check for needed restarts"); run_needrestart(&sudo).report("Restarts", &mut reports); @@ -197,13 +210,8 @@ fn main() -> Result<(), Error> { } if cfg!(target_os = "macos") { - if let Ok(brew) = which("brew") { - terminal.print_separator("Homebrew"); - run_homebrew(&brew).report("Homebrew", &mut reports); - } - - terminal.print_separator("System update"); - upgrade_macos().report("System upgrade", &mut reports);; + terminal.print_separator("App Store"); + upgrade_macos().report("App Store", &mut reports);; } if let Some(commands) = config.commands() { diff --git a/src/steps.rs b/src/steps.rs index 96ac224e..117de49c 100644 --- a/src/steps.rs +++ b/src/steps.rs @@ -144,13 +144,13 @@ pub fn run_homebrew(homebrew: &PathBuf) -> Result<(), failure::Error> { } pub fn upgrade_arch_linux( - sudo: &Result, + sudo: &Option, terminal: &Terminal, ) -> Result<(), failure::Error> { if let Ok(yay) = which("yay") { Command::new(yay).spawn()?.wait()?.check()?; } else { - if let Ok(sudo) = &sudo { + if let Some(sudo) = &sudo { Command::new(&sudo) .args(&["pacman", "-Syu"]) .spawn()? @@ -164,11 +164,8 @@ pub fn upgrade_arch_linux( Ok(()) } -pub fn upgrade_redhat( - sudo: &Result, - terminal: &Terminal, -) -> Result<(), failure::Error> { - if let Ok(sudo) = &sudo { +pub fn upgrade_redhat(sudo: &Option, terminal: &Terminal) -> Result<(), failure::Error> { + if let Some(sudo) = &sudo { Command::new(&sudo) .args(&["yum", "upgrade"]) .spawn()? @@ -181,11 +178,8 @@ pub fn upgrade_redhat( Ok(()) } -pub fn upgrade_fedora( - sudo: &Result, - terminal: &Terminal, -) -> Result<(), failure::Error> { - if let Ok(sudo) = &sudo { +pub fn upgrade_fedora(sudo: &Option, terminal: &Terminal) -> Result<(), failure::Error> { + if let Some(sudo) = &sudo { Command::new(&sudo) .args(&["dnf", "upgrade"]) .spawn()? @@ -198,11 +192,8 @@ pub fn upgrade_fedora( Ok(()) } -pub fn upgrade_debian( - sudo: &Result, - terminal: &Terminal, -) -> Result<(), failure::Error> { - if let Ok(sudo) = &sudo { +pub fn upgrade_debian(sudo: &Option, terminal: &Terminal) -> Result<(), failure::Error> { + if let Some(sudo) = &sudo { Command::new(&sudo) .args(&["apt", "update"]) .spawn()?