Skip to main content

subspace_core/credentials/
runner.rs

1//! Running credential command-line tools without exposing their output in errors.
2
3use std::time::Duration;
4
5mod process;
6mod request;
7
8use request::{CapturedOutput, RunnerRequest};
9
10/// Runs a CLI, injectable so tests fake the external process boundary.
11pub trait Runner: Send + Sync {
12    /// Runs `program` from the `PATH` with `args` and without the `remove`
13    /// variables, and returns stdout on success. Errors never include output.
14    fn run(
15        &self,
16        program: &str,
17        args: &[&str],
18        remove: &[&str],
19        timeout: Duration,
20    ) -> Result<String, String>;
21}
22
23pub struct SystemRunner;
24
25impl Runner for SystemRunner {
26    fn run(
27        &self,
28        program: &str,
29        args: &[&str],
30        remove: &[&str],
31        timeout: Duration,
32    ) -> Result<String, String> {
33        let output = process::capture(RunnerRequest::new(program, args, remove, timeout))?;
34        successful_output(program, output)
35    }
36}
37
38fn successful_output(program: &str, output: CapturedOutput) -> Result<String, String> {
39    match output.exit_code {
40        Some(0) => Ok(output.stdout),
41        _ => Err(format!("{program} failed")),
42    }
43}
44
45#[cfg(test)]
46mod tests;