Files
sausage/host/src/exec.rs
T

41 lines
1.4 KiB
Rust

//! The contract between the harness and the process table: run a command in the world.
//! The process table implements it; the harness's `bash` tool uses it.
use std::time::Duration;
/// What to run: a shell string (run by the world's `sh -c`) or an argv without a shell.
#[derive(Clone, Debug)]
pub enum Command {
Shell(String),
Argv(Vec<String>),
}
/// How to run it. `cwd` is a world path. `timeout` ends the command and all its processes.
/// `conversation` is the harness conversation that runs the command. Its processes and their
/// descendants carry it, so the host knows the caller (for example in the `wasi:http` hook).
#[derive(Clone, Debug, Default)]
pub struct Options {
pub cwd: String,
pub env: Vec<(String, String)>,
pub timeout: Option<Duration>,
pub conversation: Option<String>,
}
/// The stream that one output chunk came from.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Stream {
Stdout,
Stderr,
}
/// Run commands in the world. Dropping the future kills the command and all processes it started.
#[async_trait::async_trait]
pub trait Exec: Send + Sync {
/// Run to completion and return the exit status. `output` gets each chunk as it arrives, in order.
async fn exec(
&self,
command: Command,
options: Options,
output: &mut (dyn for<'chunk> FnMut(Stream, &'chunk [u8]) + Send),
) -> anyhow::Result<i32>;
}