From 25f7b7df9d8c63467df382eca69aba57e74a9fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Mon, 7 Sep 2026 13:46:58 +0000 Subject: [PATCH] feat: fail when a command fails --- src/command_builder.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/command_builder.rs b/src/command_builder.rs index b7a9d78..8e8832c 100644 --- a/src/command_builder.rs +++ b/src/command_builder.rs @@ -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 { - 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 { @@ -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"), } }