Skip to main content

chaud_hot/util/
command.rs

1use anyhow::{Context as _, Result, ensure};
2use std::process::{Command, Output};
3
4/// Helper for properly checking [`Command`] results.
5pub trait CommandExt {
6    fn output_checked(&mut self) -> Result<Output>;
7
8    fn stdout_str(&mut self) -> Result<String> {
9        let output = self.output_checked()?;
10        let stdout = String::from_utf8(output.stdout).context("Invalid stdout")?;
11        Ok(stdout)
12    }
13}
14
15impl CommandExt for Command {
16    fn output_checked(&mut self) -> Result<Output> {
17        let output = self.output().context("Command failed to run")?;
18        ensure!(
19            output.status.success(),
20            "Command failed with status: {}",
21            output.status
22        );
23        Ok(output)
24    }
25}