#log-line #command-line #log #command #shell #task #posix

shell-candy

🍬 shell-candy wraps std::process::Command, providing a more straightforward mechanism for handling individual log lines

9 unstable releases (3 breaking)

0.4.0 Nov 15, 2022
0.3.0 Nov 15, 2022
0.2.3 Nov 14, 2022
0.2.1 Oct 8, 2022
0.1.3 Oct 7, 2022

#2866 in Command line utilities

Download history 9/week @ 2023-12-18 6/week @ 2023-12-25 68/week @ 2024-01-01 183/week @ 2024-01-08 433/week @ 2024-01-15 454/week @ 2024-01-22 34/week @ 2024-01-29 6/week @ 2024-02-05 19/week @ 2024-02-12 21/week @ 2024-02-19 38/week @ 2024-02-26 56/week @ 2024-03-04 344/week @ 2024-03-11 341/week @ 2024-03-18 161/week @ 2024-03-25 131/week @ 2024-04-01

980 downloads per month

MIT license

22KB
344 lines

🍬 shell-candy

This crate wraps std::process::Command, providing an easier mechanism for handling individual log lines from external tools.

Usage

This example shows the basic usage of ShellTask: create one from a POSIX-style command, and then run it with a log line handler that you write yourself. This handler can either continue for every line for the length of the program, or it can return early and shut down the program.

You could use this function to pass log lines through your own log formatter like so:

use anyhow::Result;
use shell_candy::{ShellTaskLog, ShellTaskBehavior, ShellTask};

fn main() -> Result<()> {
  let task = ShellTask::new("rustc --version")?;
  task.run(|line| {
    match line {
      ShellTaskLog::Stdout(message) | ShellTaskLog::Stderr(message) => eprintln!("info: {}", &message),
    }
    ShellTaskBehavior::<()>::Passthrough
  })?;
  Ok(())
}

You could also use this function to return early if a command meets a specific criteria (like encountering an unrecoverable error):

use anyhow::{anyhow, Error, Result};
use shell_candy::{ShellTaskLog, ShellTaskBehavior, ShellTask};

fn main() -> Result<()> {
  let task = ShellTask::new("git log")?;
  task.run(|line| {
    match line {
      ShellTaskLog::Stdout(message) | ShellTaskLog::Stderr(message) => {
        if message.contains("an error that is unlikely to be in your git logs but just might be") {
          return ShellTaskBehavior::<()>::EarlyReturn(Err(anyhow!("encountered an error while running 'git log'").into()));
        }
      },
    }
    ShellTaskBehavior::<()>::Passthrough
  })?;
  Ok(())
}

More information

See the docs for more detailed information and example usage.

Dependencies

~3–14MB
~164K SLoC