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
sourceimpl ShellTask
impl ShellTask
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<Option<T>>where
F: Fn(ShellTaskLog) -> ShellTaskBehavior<T> + Send + Sync + 'static,
T: Send + Sync + 'static,
pub fn run<F, T>(&self, log_handler: F) -> Result<Option<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, ShellTaskBehavior};
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let result: Option<String> = ShellTask::new("rustc --version")?.run(|line| {
match line {
ShellTaskLog::Stderr(_) => {
ShellTaskBehavior::Passthrough
},
ShellTaskLog::Stdout(message) => {
eprintln!("{}", &message);
ShellTaskBehavior::EarlyReturn(Ok(message))
}
}
})?;
assert!(result.is_some());
Ok(())
}If your log_handler returns ShellTaskBehavior::Passthrough for
the entire lifecycle of the task, ShellTask::run will return None.
Example
use anyhow::anyhow;
use shell_candy::{ShellTask, ShellTaskLog, ShellTaskBehavior};
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let result: Option<()> = ShellTask::new("rustc --version")?.run(|line| {
match line {
ShellTaskLog::Stderr(message) | ShellTaskLog::Stdout(message) => {
eprintln!("info: {}", &message);
ShellTaskBehavior::Passthrough
}
}
})?;
assert!(result.is_none());
Ok(())
}Auto Trait Implementations
impl RefUnwindSafe for ShellTask
impl Send for ShellTask
impl Sync for ShellTask
impl Unpin for ShellTask
impl UnwindSafe for ShellTask
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more