diff --git a/src/main.rs b/src/main.rs index 33971643..a6183008 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,10 +134,12 @@ fn run() -> Result<(), Error> { #[cfg(windows)] { if powershell.profile().is_some() && config.should_run(Step::Powershell) { - report.push_result(execute_legacy( + execute( + &mut report, + "Powershell Modules Update", || powershell.update_modules(run_type), config.no_retry(), - )?); + )?; } } @@ -168,10 +170,15 @@ fn run() -> Result<(), Error> { } #[cfg(windows)] - report.push_result(execute_legacy(|| windows::run_chocolatey(run_type), config.no_retry())?); + execute( + &mut report, + "Chocolatey", + || windows::run_chocolatey(run_type), + config.no_retry(), + )?; #[cfg(windows)] - report.push_result(execute_legacy(|| windows::run_scoop(run_type), config.no_retry())?); + execute(&mut report, "Scoop", || windows::run_scoop(run_type), config.no_retry())?; #[cfg(unix)] report.push_result(execute_legacy( @@ -383,10 +390,12 @@ fn run() -> Result<(), Error> { #[cfg(windows)] { if config.should_run(Step::System) { - report.push_result(execute_legacy( + execute( + &mut report, + "Windows update", || powershell.windows_update(run_type), config.no_retry(), - )?); + )?; } } diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index 5157f1b0..c9115ee3 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -1,43 +1,24 @@ -use crate::error::Error; +use crate::error::{Error, ErrorKind}; use crate::executor::{CommandExt, RunType}; use crate::terminal::{is_dumb, print_separator}; -use crate::utils::{self, which}; +use crate::utils::{require, require_option, which}; use std::path::PathBuf; use std::process::Command; -#[must_use] -pub fn run_chocolatey(run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(choco) = utils::which("choco") { - print_separator("Chocolatey"); +pub fn run_chocolatey(run_type: RunType) -> Result<(), Error> { + let choco = require("choco")?; - let success = || -> Result<(), Error> { - run_type.execute(&choco).args(&["upgrade", "all"]).check_run()?; - Ok(()) - }() - .is_ok(); - - return Some(("Chocolatey", success)); - } - - None + print_separator("Chocolatey"); + run_type.execute(&choco).args(&["upgrade", "all"]).check_run() } -#[must_use] -pub fn run_scoop(run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(scoop) = utils::which("scoop") { - print_separator("Scoop"); +pub fn run_scoop(run_type: RunType) -> Result<(), Error> { + let scoop = require("scoop")?; - let success = || -> Result<(), Error> { - run_type.execute(&scoop).args(&["update"]).check_run()?; - run_type.execute(&scoop).args(&["update", "*"]).check_run()?; - Ok(()) - }() - .is_ok(); + print_separator("Scoop"); - return Some(("Scoop", success)); - } - - None + run_type.execute(&scoop).args(&["update"]).check_run()?; + run_type.execute(&scoop).args(&["update", "*"]).check_run() } pub struct Powershell { @@ -78,45 +59,24 @@ impl Powershell { self.profile.as_ref() } - #[must_use] - pub fn update_modules(&self, run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(powershell) = &self.path { - print_separator("Powershell Modules Update"); + pub fn update_modules(&self, run_type: RunType) -> Result<(), Error> { + let powershell = require_option(self.path.as_ref())?; - let success = || -> Result<(), Error> { - run_type - .execute(&powershell) - .args(&["Update-Module", "-v"]) - .check_run()?; - Ok(()) - }() - .is_ok(); - - return Some(("Powershell Modules Update", success)); - } - - None + print_separator("Powershell Modules Update"); + run_type.execute(&powershell).args(&["Update-Module", "-v"]).check_run() } - #[must_use] - pub fn windows_update(&self, run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(powershell) = &self.path { - if Self::has_command(&powershell, "Install-WindowsUpdate") { - print_separator("Windows Update"); + pub fn windows_update(&self, run_type: RunType) -> Result<(), Error> { + let powershell = require_option(self.path.as_ref())?; - let success = || -> Result<(), Error> { - run_type - .execute(&powershell) - .args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"]) - .check_run()?; - Ok(()) - }() - .is_ok(); - - return Some(("Windows Update", success)); - } + if !Self::has_command(&powershell, "Install-WindowsUpdate") { + Err(ErrorKind::SkipStep)?; } + print_separator("Windows Update"); - None + run_type + .execute(&powershell) + .args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"]) + .check_run() } } diff --git a/src/utils.rs b/src/utils.rs index e7e88030..ceaf3842 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -181,7 +181,7 @@ pub fn require + Debug>(binary_name: T) -> Result(option: Option) -> Result { if let Some(value) = option { Ok(value)