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)]
{
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(),
)?);
)?;
}
}

View File

@@ -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") {
pub fn run_chocolatey(run_type: RunType) -> Result<(), Error> {
let choco = require("choco")?;
print_separator("Chocolatey");
let success = || -> Result<(), Error> {
run_type.execute(&choco).args(&["upgrade", "all"]).check_run()?;
Ok(())
}()
.is_ok();
return Some(("Chocolatey", success));
}
None
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") {
pub fn run_scoop(run_type: RunType) -> Result<(), Error> {
let scoop = require("scoop")?;
print_separator("Scoop");
let success = || -> Result<(), Error> {
run_type.execute(&scoop).args(&["update"]).check_run()?;
run_type.execute(&scoop).args(&["update", "*"]).check_run()?;
Ok(())
}()
.is_ok();
return Some(("Scoop", success));
}
None
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 {
pub fn update_modules(&self, run_type: RunType) -> Result<(), Error> {
let powershell = require_option(self.path.as_ref())?;
print_separator("Powershell Modules Update");
let success = || -> Result<(), Error> {
run_type
.execute(&powershell)
.args(&["Update-Module", "-v"])
.check_run()?;
Ok(())
}()
.is_ok();
return Some(("Powershell Modules Update", success));
run_type.execute(&powershell).args(&["Update-Module", "-v"]).check_run()
}
None
}
pub fn windows_update(&self, run_type: RunType) -> Result<(), Error> {
let powershell = require_option(self.path.as_ref())?;
#[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") {
if !Self::has_command(&powershell, "Install-WindowsUpdate") {
Err(ErrorKind::SkipStep)?;
}
print_separator("Windows Update");
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));
}
}
None
.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> {
if let Some(value) = option {
Ok(value)