Convert more legacy steps

This commit is contained in:
Roey Darwish Dror
2019-02-11 14:10:06 +02:00
parent dfe06a5111
commit a1b3e37f93
3 changed files with 27 additions and 26 deletions

View File

@@ -149,10 +149,12 @@ 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_legacy( execute(
&mut report,
"System update",
|| distribution.upgrade(&sudo, config.cleanup(), run_type), || distribution.upgrade(&sudo, config.cleanup(), run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
Err(e) => { Err(e) => {
println!("Error detecting current distribution: {}", e); println!("Error detecting current distribution: {}", e);
@@ -353,10 +355,12 @@ fn run() -> Result<(), Error> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
report.push_result(execute_legacy(|| 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_legacy( execute(
|| linux::run_needrestart(&sudo, run_type), &mut report,
"Restarts",
|| linux::run_needrestart(sudo.as_ref(), run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]

View File

@@ -1,7 +1,7 @@
use crate::error::{Error, ErrorKind}; use crate::error::{Error, ErrorKind};
use crate::executor::RunType; use crate::executor::RunType;
use crate::terminal::{print_separator, print_warning}; use crate::terminal::{print_separator, print_warning};
use crate::utils::which; use crate::utils::{require, require_option, which};
use failure::ResultExt; use failure::ResultExt;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
@@ -59,10 +59,10 @@ impl Distribution {
} }
#[must_use] #[must_use]
pub fn upgrade(self, sudo: &Option<PathBuf>, cleanup: bool, run_type: RunType) -> Option<(&'static str, bool)> { pub fn upgrade(self, sudo: &Option<PathBuf>, cleanup: bool, run_type: RunType) -> Result<(), Error> {
print_separator("System update"); print_separator("System update");
let success = match self { match self {
Distribution::Arch => upgrade_arch_linux(&sudo, cleanup, run_type), Distribution::Arch => upgrade_arch_linux(&sudo, cleanup, run_type),
Distribution::CentOS => upgrade_redhat(&sudo, run_type), Distribution::CentOS => upgrade_redhat(&sudo, run_type),
Distribution::Fedora => upgrade_fedora(&sudo, run_type), Distribution::Fedora => upgrade_fedora(&sudo, run_type),
@@ -70,9 +70,7 @@ impl Distribution {
Distribution::Gentoo => upgrade_gentoo(&sudo, run_type), Distribution::Gentoo => upgrade_gentoo(&sudo, run_type),
Distribution::OpenSuse => upgrade_opensuse(&sudo, run_type), Distribution::OpenSuse => upgrade_opensuse(&sudo, run_type),
Distribution::Void => upgrade_void(&sudo, run_type), Distribution::Void => upgrade_void(&sudo, run_type),
}; }
Some(("System update", success.is_ok()))
} }
pub fn show_summary(self) { pub fn show_summary(self) {
@@ -235,24 +233,15 @@ fn upgrade_debian(sudo: &Option<PathBuf>, cleanup: bool, run_type: RunType) -> R
Ok(()) Ok(())
} }
#[must_use] pub fn run_needrestart(sudo: Option<&PathBuf>, run_type: RunType) -> Result<(), Error> {
pub fn run_needrestart(sudo: &Option<PathBuf>, run_type: RunType) -> Option<(&'static str, bool)> { let sudo = require_option(sudo)?;
if let Some(sudo) = sudo { let needrestart = require("needrestart")?;
if let Some(needrestart) = which("needrestart") {
print_separator("Check for needed restarts");
let success = || -> Result<(), Error> { print_separator("Check for needed restarts");
run_type.execute(&sudo).arg(needrestart).check_run()?;
Ok(()) run_type.execute(&sudo).arg(needrestart).check_run()?;
}()
.is_ok();
return Some(("Restarts", success)); Ok(())
}
}
None
} }
#[must_use] #[must_use]

View File

@@ -180,3 +180,11 @@ pub fn require<T: AsRef<OsStr> + Debug>(binary_name: T) -> Result<PathBuf, Error
}, },
} }
} }
pub fn require_option<T>(option: Option<T>) -> Result<T, Error> {
if let Some(value) = option {
Ok(value)
} else {
Err(ErrorKind::SkipStep)?
}
}