crazy_train/executer.rs
1//! This module provides functionality for executing shell commands and capturing their outputs.
2//!
3//! The [`Output`] struct represents the output of a shell command, including the status code,
4//! standard output (stdout), and standard error (stderr).
5
6use crate::errors::Result;
7
8/// Represents the output of a shell command execution.
9#[derive(Debug)]
10pub struct Output {
11 /// The exit status code of the command. It is optional to accommodate commands that may not
12 /// return a status code.
13 pub status_code: Option<i32>,
14 /// The standard output produced by the command.
15 pub stdout: String,
16 /// The standard error output produced by the command.
17 pub stderr: String,
18}
19
20/// Executes a shell command and returns its output.
21///
22/// # Errors
23///
24/// This function will return an error if:
25/// - The command fails to execute.
26/// - There is an error capturing the output or converting it to a UTF-8 string.
27pub fn run_sh(command: &str) -> Result<Output> {
28 let output = duct_sh::sh_dangerous(command)
29 .stderr_capture()
30 .stderr_capture()
31 .unchecked()
32 .run()?;
33
34 Ok(Output {
35 status_code: output.status.code(),
36 stdout: std::str::from_utf8(&output.stdout)?.to_string(),
37 stderr: std::str::from_utf8(&output.stderr)?.to_string(),
38 })
39}