1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use log::info;

use crate::Result;

pub async fn run_command(mut command: tokio::process::Command) -> Result<()> {
    info!("Running command `{:?}`", command);
    let exit_status = command.spawn()?.wait().await?;
    if !exit_status.success() {
        return Err(format!("exec failed: {:?}", command).into());
    }
    Ok(())
}

pub fn run_command_blocking(mut command: std::process::Command) -> Result<()> {
    info!("Running command blocking `{:?}`", command);
    let exit_status = command.spawn()?.wait()?;
    if !exit_status.success() {
        return Err(format!("exec failed: {:?}", command).into());
    }
    Ok(())
}

pub async fn run_command_output(mut command: tokio::process::Command) -> Result<String> {
    info!("Fetching command `{:?}`", command);
    let output = command.spawn()?.wait_with_output().await?;
    if !output.status.success() {
        return Err(format!("exec failed: {:?}", command).into());
    }

    let output = String::from_utf8(output.stdout)?;
    Ok(output)
}