Rust

execute

Module

Running commands: input, output, progress and a timeout.

Contents

use utopia_console::Output;
Rust
pub struct Output

What a run produced.

use utopia_console::Program;
Rust
pub enum Program<'a>

What to run.

Also implements From.
use utopia_console::execute;
Rust
pub async fn execute<'a>(program: impl Into<Program<'a>>, stdin: &[u8], timeout: Option<Duration>, mut on_progress: Option<Progress<'_>>) -> Result<Output, Error>

Runs program with stdin as its input, collecting its output, and returns its exit status and output.

A shell command runs as /bin/sh -c '( <command> ) 3>/dev/null', so its status is the shell's $? of it. A program that cannot be started reports status 1 and no output.

Rust
use utopia_console::{execute, Command};

let sort = Command::new("sort")?;
let output = execute(&sort, b"delta\nalpha\n", None, None).await?;
assert_eq!(output.status, 0);
assert_eq!(output.stdout, b"alpha\ndelta\n");

let output = execute("echo hello; exit 3", b"", None, None).await?;
assert_eq!((output.status, output.stdout.as_slice()), (3, &b"hello\n"[..]));
Rust
pub type Progress<'p> = &'p mut (dyn FnMut(&[u8]) + Send)

A callback receiving each piece of output as it arrives.