Skip to main content

Cmd

Struct Cmd 

Source
pub struct Cmd { /* private fields */ }
Expand description

Builder for a subprocess invocation.

Construct via Cmd::new, configure with builder methods, terminate with Cmd::run. Every knob composes with every other — timeout + env + retry

  • stdin work together without combinatorial API explosion.

Implementations§

Source§

impl Cmd

Source

pub fn new(program: impl Into<OsString>) -> Self

Start a new command with the given program.

Source

pub fn arg(self, arg: impl Into<OsString>) -> Self

Append a single argument.

Source

pub fn args<I, S>(self, args: I) -> Self
where I: IntoIterator<Item = S>, S: Into<OsString>,

Append arguments.

Source

pub fn in_dir(self, dir: impl AsRef<Path>) -> Self

Set the working directory.

Source

pub fn env(self, key: impl Into<OsString>, value: impl Into<OsString>) -> Self

Add one environment variable.

Source

pub fn envs<I, K, V>(self, vars: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<OsString>, V: Into<OsString>,

Add multiple environment variables.

Source

pub fn env_remove(self, key: impl Into<OsString>) -> Self

Remove an environment variable (applied after inherited env).

Source

pub fn env_clear(self) -> Self

Clear the entire inherited environment; only .env() / .envs() reach the child.

Source

pub fn stdin(self, data: impl Into<StdinData>) -> Self

Feed data into the child’s stdin.

Accepts Vec<u8>, &[u8], String, &str, or StdinData::from_reader for streaming input. Owned bytes are re-fed on each retry; readers are one-shot.

Source

pub fn stderr(self, mode: Redirection) -> Self

Configure stderr routing. Default is Redirection::Capture.

Source

pub fn timeout(self, timeout: Duration) -> Self

Kill this attempt after the given duration.

Source

pub fn deadline(self, deadline: Instant) -> Self

Kill if not done by this instant (composes across retries).

Source

pub fn retry(self, policy: RetryPolicy) -> Self

Attach a RetryPolicy. Defaults retry up to 3× on transient errors.

Source

pub fn retry_when( self, f: impl Fn(&RunError) -> bool + Send + Sync + 'static, ) -> Self

Replace the retry predicate without changing the backoff schedule.

If no RetryPolicy is set yet, this installs the default policy and then overrides its predicate.

Source

pub fn secret(self) -> Self

Mark this command as containing secrets.

CmdDisplay and RunError render args as <secret> instead of their values. Useful for docker login, kubectl --token=…, etc.

Source

pub fn before_spawn<F>(self, hook: F) -> Self
where F: Fn(&mut Command) -> Result<()> + Send + Sync + 'static,

Register a hook called immediately before each spawn attempt.

Source

pub fn to_command(&self) -> Command

Build a raw std::process::Command mirroring this Cmd’s configuration.

Escape hatch for cases procpilot’s builder doesn’t cover. Does not apply stdin data, timeout, retry, or stderr redirection — those are runner-level concerns.

Source

pub fn display(&self) -> CmdDisplay

Snapshot the command for display/logging.

Source

pub fn spawn(self) -> Result<SpawnedProcess, RunError>

Spawn the command as a long-lived process handle.

Returns a SpawnedProcess for streaming, bidirectional protocols, or any case where you need live access to stdin/stdout. Stdin and stdout are always piped; stderr follows the configured Redirection (default Redirection::Capture, drained into a background thread and surfaced on SpawnedProcess::wait).

If stdin bytes were set via stdin, they’re fed automatically in a background thread; otherwise the caller can pipe data via SpawnedProcess::take_stdin.

timeout, deadline, and retry are ignored on this path — they only apply to the one-shot run method. Use SpawnedProcess::wait_timeout or SpawnedProcess::kill for per-call bounds.

Source

pub fn spawn_and_collect_lines<F>(self, f: F) -> Result<RunOutput, RunError>
where F: FnMut(&str) -> Result<()>,

Spawn and invoke f for each line of stdout as it arrives.

Returns the final RunOutput when the child exits, or a RunError::NonZeroExit if it exited non-zero. If f returns an error, the child is killed and the error is surfaced as RunError::Spawn.

Cmd::new("cargo")
    .args(["check", "--message-format=json"])
    .spawn_and_collect_lines(|line| {
        println!("{line}");
        Ok(())
    })?;
Source

pub fn run(self) -> Result<RunOutput, RunError>

Run the command, blocking until it completes (or times out).

Trait Implementations§

Source§

impl Debug for Cmd

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Cmd

§

impl !RefUnwindSafe for Cmd

§

impl Send for Cmd

§

impl Sync for Cmd

§

impl Unpin for Cmd

§

impl UnsafeUnpin for Cmd

§

impl !UnwindSafe for Cmd

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.