Windows step refactoring

This commit is contained in:
Roey Darwish Dror
2019-02-11 20:38:51 +02:00
parent e2d42b77a7
commit a85311608e
3 changed files with 40 additions and 71 deletions

View File

@@ -134,10 +134,12 @@ 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_legacy( execute(
&mut report,
"Powershell Modules Update",
|| powershell.update_modules(run_type), || powershell.update_modules(run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
} }
@@ -168,10 +170,15 @@ fn run() -> Result<(), Error> {
} }
#[cfg(windows)] #[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)] #[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)] #[cfg(unix)]
report.push_result(execute_legacy( report.push_result(execute_legacy(
@@ -383,10 +390,12 @@ fn run() -> Result<(), Error> {
#[cfg(windows)] #[cfg(windows)]
{ {
if config.should_run(Step::System) { if config.should_run(Step::System) {
report.push_result(execute_legacy( execute(
&mut report,
"Windows update",
|| powershell.windows_update(run_type), || powershell.windows_update(run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
} }

View File

@@ -1,43 +1,24 @@
use crate::error::Error; use crate::error::{Error, ErrorKind};
use crate::executor::{CommandExt, RunType}; use crate::executor::{CommandExt, RunType};
use crate::terminal::{is_dumb, print_separator}; 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::path::PathBuf;
use std::process::Command; use std::process::Command;
#[must_use] pub fn run_chocolatey(run_type: RunType) -> Result<(), Error> {
pub fn run_chocolatey(run_type: RunType) -> Option<(&'static str, bool)> { let choco = require("choco")?;
if let Some(choco) = utils::which("choco") {
print_separator("Chocolatey");
let success = || -> Result<(), Error> { print_separator("Chocolatey");
run_type.execute(&choco).args(&["upgrade", "all"]).check_run()?; run_type.execute(&choco).args(&["upgrade", "all"]).check_run()
Ok(())
}()
.is_ok();
return Some(("Chocolatey", success));
}
None
} }
#[must_use] pub fn run_scoop(run_type: RunType) -> Result<(), Error> {
pub fn run_scoop(run_type: RunType) -> Option<(&'static str, bool)> { let scoop = require("scoop")?;
if let Some(scoop) = utils::which("scoop") {
print_separator("Scoop");
let success = || -> Result<(), Error> { print_separator("Scoop");
run_type.execute(&scoop).args(&["update"]).check_run()?;
run_type.execute(&scoop).args(&["update", "*"]).check_run()?;
Ok(())
}()
.is_ok();
return Some(("Scoop", success)); run_type.execute(&scoop).args(&["update"]).check_run()?;
} run_type.execute(&scoop).args(&["update", "*"]).check_run()
None
} }
pub struct Powershell { pub struct Powershell {
@@ -78,45 +59,24 @@ impl Powershell {
self.profile.as_ref() self.profile.as_ref()
} }
#[must_use] pub fn update_modules(&self, run_type: RunType) -> Result<(), Error> {
pub fn update_modules(&self, run_type: RunType) -> Option<(&'static str, bool)> { let powershell = require_option(self.path.as_ref())?;
if let Some(powershell) = &self.path {
print_separator("Powershell Modules Update");
let success = || -> Result<(), Error> { print_separator("Powershell Modules Update");
run_type run_type.execute(&powershell).args(&["Update-Module", "-v"]).check_run()
.execute(&powershell)
.args(&["Update-Module", "-v"])
.check_run()?;
Ok(())
}()
.is_ok();
return Some(("Powershell Modules Update", success));
}
None
} }
#[must_use] pub fn windows_update(&self, run_type: RunType) -> Result<(), Error> {
pub fn windows_update(&self, run_type: RunType) -> Option<(&'static str, bool)> { let powershell = require_option(self.path.as_ref())?;
if let Some(powershell) = &self.path {
if Self::has_command(&powershell, "Install-WindowsUpdate") {
print_separator("Windows Update");
let success = || -> Result<(), Error> { if !Self::has_command(&powershell, "Install-WindowsUpdate") {
run_type Err(ErrorKind::SkipStep)?;
.execute(&powershell)
.args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"])
.check_run()?;
Ok(())
}()
.is_ok();
return Some(("Windows Update", success));
}
} }
print_separator("Windows Update");
None run_type
.execute(&powershell)
.args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"])
.check_run()
} }
} }

View File

@@ -181,7 +181,7 @@ pub fn require<T: AsRef<OsStr> + Debug>(binary_name: T) -> Result<PathBuf, Error
} }
} }
#[cfg(target_os = "linux")] #[allow(dead_code)]
pub fn require_option<T>(option: Option<T>) -> Result<T, Error> { pub fn require_option<T>(option: Option<T>) -> Result<T, Error> {
if let Some(value) = option { if let Some(value) = option {
Ok(value) Ok(value)