pub struct ShellTask { /* private fields */ }
Expand description
A ShellTask
runs commands and provides a passthrough log handler
for each log line.
Implementationsยง
Sourceยงimpl ShellTask
impl ShellTask
Sourcepub fn env<K, V>(&mut self, key: K, value: V) -> &mut ShellTask
pub fn env<K, V>(&mut self, key: K, value: V) -> &mut ShellTask
Adds an environment variable to the command run by ShellTask
.
Sourcepub fn current_dir<P>(&mut self, path: P)
pub fn current_dir<P>(&mut self, path: P)
Sets the directory the command should be run in.
Sourcepub fn descriptor(&self) -> String
pub fn descriptor(&self) -> String
Returns the full command that was used to instantiate this ShellTask
.
Sourcepub fn bash_descriptor(&self) -> String
pub fn bash_descriptor(&self) -> String
Returns the ShellTask::descriptor
with the classic $
shell prefix.
Sourcepub fn run<F, T>(&self, log_handler: F) -> Result<ShellTaskOutput<T>>where
F: Fn(ShellTaskLog) -> ShellTaskBehavior<T> + Send + Sync + 'static,
T: Send + Sync + 'static,
pub fn run<F, T>(&self, log_handler: F) -> Result<ShellTaskOutput<T>>where
F: Fn(ShellTaskLog) -> ShellTaskBehavior<T> + Send + Sync + 'static,
T: Send + Sync + 'static,
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ยง
Auto Trait Implementationsยง
impl Freeze for ShellTask
impl RefUnwindSafe for ShellTask
impl Send for ShellTask
impl Sync for ShellTask
impl Unpin for ShellTask
impl UnwindSafe for ShellTask
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more