help-probe 0.1.0

CLI tool discovery and automation framework that extracts structured information from command help text
Documentation
use std::process::{Output, Stdio};
use std::time::Duration;

use anyhow::{Context, Result, anyhow};
use tokio::process::Command;
use tokio::time;

/// Outcome of running a command with timeout.
pub enum RunOutcome {
    /// Command completed (may have non-zero exit code).
    Completed(Output),
    /// Command timed out before completion.
    TimedOut,
}

/// Run a command with arguments, capturing stdout/stderr, with a timeout.
pub async fn run_with_timeout(
    program: &str,
    args: &[String],
    timeout: Duration,
) -> Result<RunOutcome> {
    let mut cmd = Command::new(program);
    cmd.args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    let child = cmd
        .spawn()
        .with_context(|| format!("Failed to spawn command: {} {:?}", program, args))?;

    match time::timeout(timeout, child.wait_with_output()).await {
        Ok(Ok(output)) => Ok(RunOutcome::Completed(output)),
        Ok(Err(e)) => Err(anyhow!("Error while running command: {}", e)),
        Err(_) => Ok(RunOutcome::TimedOut),
    }
}