Struct shell_candy::ShellTask

source ·
pub struct ShellTask { /* private fields */ }
Expand description

A ShellTask runs commands and provides a passthrough log handler for each log line.

Implementations

Create a new ShellTask with a log line handler.

Adds an environment variable to the command run by ShellTask.

Sets the directory the command should be run in.

Returns the full command that was used to instantiate this ShellTask.

Returns the ShellTask::descriptor with the classic $ shell prefix.

Run a ShellTask, applying the log handler to each line.

You can make the task terminate early if your log_handler returns ShellTaskBehavior::EarlyReturn<T>. When this variant is returned from a log handler, ShellTask::run will return Some<T>.

Example
use anyhow::anyhow;
use shell_candy::{ShellTask, ShellTaskLog, ShellTaskOutput, ShellTaskBehavior};

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
    let result = ShellTask::new("rustc --version")?.run(|line| {
        match line {
            ShellTaskLog::Stderr(_) => {
                ShellTaskBehavior::Passthrough
            },
            ShellTaskLog::Stdout(message) => {
                eprintln!("{}", &message);
                ShellTaskBehavior::EarlyReturn(Ok(message))
            }
        }
    })?;
    assert!(matches!(result, ShellTaskOutput::EarlyReturn { .. }));
    Ok(())
}

If your log_handler returns ShellTaskBehavior::Passthrough for the entire lifecycle of the task, ShellTask::run will return ShellTaskOutput::CompleteOutput.

Example
use anyhow::anyhow;
use shell_candy::{ShellTask, ShellTaskLog, ShellTaskOutput, ShellTaskBehavior};

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
    let result = ShellTask::new("rustc --version")?.run(|line| {
        match line {
            ShellTaskLog::Stderr(message) | ShellTaskLog::Stdout(message) => {
                eprintln!("info: {}", &message);
                ShellTaskBehavior::<()>::Passthrough
            }
        }
    })?;
    assert!(matches!(result, ShellTaskOutput::CompleteOutput { .. }));
    Ok(())
}

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.