feat: fail when a command fails
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use std::{
|
||||
fmt::Display,
|
||||
process::{Child, Command, Stdio},
|
||||
process::{Child, Command, ExitStatus, Stdio},
|
||||
};
|
||||
|
||||
use crate::debug_eprintln;
|
||||
@@ -70,12 +70,18 @@ impl CommandBuilder {
|
||||
}
|
||||
|
||||
pub fn exec_get_stdout(self) -> Result<String> {
|
||||
Ok(String::from_utf8(self.build()?.output()?.stdout)?)
|
||||
let shown = self.to_string();
|
||||
let out = self.build()?.output()?;
|
||||
|
||||
check(&shown, out.status)?;
|
||||
Ok(String::from_utf8(out.stdout)?)
|
||||
}
|
||||
|
||||
pub fn exec(self) -> Result<()> {
|
||||
self.build()?.spawn()?.wait()?;
|
||||
Ok(())
|
||||
let shown = self.to_string();
|
||||
let status = self.build()?.spawn()?.wait()?;
|
||||
|
||||
check(&shown, status)
|
||||
}
|
||||
|
||||
pub fn spawn(self) -> Result<Child> {
|
||||
@@ -83,7 +89,20 @@ impl CommandBuilder {
|
||||
}
|
||||
|
||||
pub fn exec_redirect_stdout(self, stdio: Stdio) -> Result<()> {
|
||||
self.build()?.stdout(stdio).spawn()?.wait()?;
|
||||
Ok(())
|
||||
let shown = self.to_string();
|
||||
let status = self.build()?.stdout(stdio).spawn()?.wait()?;
|
||||
|
||||
check(&shown, status)
|
||||
}
|
||||
}
|
||||
|
||||
fn check(command: &str, status: ExitStatus) -> Result<()> {
|
||||
if status.success() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match status.code() {
|
||||
Some(code) => bail!("`{command}` exited with {code}"),
|
||||
None => bail!("`{command}` was killed by a signal"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user