Convert process failure into error

This commit is contained in:
Roey Darwish Dror
2018-06-03 16:44:19 +03:00
parent 504d79121d
commit 4a208d9e8c

View File

@@ -10,6 +10,12 @@ mod error {
Io(::std::io::Error); Io(::std::io::Error);
} }
errors {
ProcessFailed {
description("Process failed")
display("Process failed")
}
}
} }
} }
@@ -26,24 +32,16 @@ use std::process::{Command, ExitStatus};
use terminal::Terminal; use terminal::Terminal;
use which::which; use which::which;
trait Chain trait Check {
where fn check(self) -> Result<()>;
Self: std::marker::Sized,
{
fn and_then<F>(self, f: F) -> ::std::io::Result<Self>
where
F: FnOnce() -> ::std::io::Result<Self>;
} }
impl Chain for ExitStatus { impl Check for ExitStatus {
fn and_then<F>(self, f: F) -> ::std::io::Result<Self> fn check(self) -> Result<()> {
where if self.success() {
F: FnOnce() -> ::std::io::Result<Self>, Ok(())
{
if !self.success() {
Ok(self)
} else { } else {
f() Err(ErrorKind::ProcessFailed.into())
} }
} }
} }
@@ -176,12 +174,14 @@ fn run() -> Result<()> {
.arg("update") .arg("update")
.spawn()? .spawn()?
.wait()? .wait()?
.and_then(|| { .check()
.and_then(|()| {
Command::new(&sudo) Command::new(&sudo)
.arg("apt") .arg("apt")
.arg("dist-upgrade") .arg("dist-upgrade")
.spawn()? .spawn()?
.wait() .wait()
.map_err(|e| e.into())
})?; })?;
} }
} }
@@ -201,7 +201,14 @@ fn run() -> Result<()> {
.arg("refresh") .arg("refresh")
.spawn()? .spawn()?
.wait()? .wait()?
.and_then(|| Command::new(&fwupdmgr).arg("get-updates").spawn()?.wait())?; .check()
.and_then(|()| {
Command::new(&fwupdmgr)
.arg("get-updates")
.spawn()?
.wait()
.map_err(|e| e.into())
})?;
} }
if let Ok(sudo) = &sudo { if let Ok(sudo) = &sudo {
@@ -219,13 +226,15 @@ fn run() -> Result<()> {
.arg("update") .arg("update")
.spawn()? .spawn()?
.wait()? .wait()?
.and_then(|| Command::new(&brew).arg("upgrade").spawn()?.wait())? .check()
.and_then(|| { .and_then(|()| Command::new(&brew).arg("upgrade").spawn()?.wait()?.check())
.and_then(|()| {
Command::new(&brew) Command::new(&brew)
.arg("cleanup") .arg("cleanup")
.arg("-sbr") .arg("-sbr")
.spawn()? .spawn()?
.wait() .wait()
.map_err(|e| e.into())
})?; })?;
} }