#process #future

bawawa

Opinionated set of tools to manage and compose process executions

6 releases

0.1.5 Aug 22, 2019
0.1.4 Jul 9, 2019

#343 in Asynchronous

Download history 7/week @ 2020-10-24 13/week @ 2020-10-31 7/week @ 2020-11-07 7/week @ 2020-11-14 13/week @ 2020-11-21 9/week @ 2020-11-28 19/week @ 2020-12-05 7/week @ 2020-12-12 7/week @ 2020-12-19 8/week @ 2020-12-26 11/week @ 2021-01-02 8/week @ 2021-01-09 7/week @ 2021-01-16 7/week @ 2021-01-23 10/week @ 2021-01-30 14/week @ 2021-02-06

1,045 downloads per month

MIT/Apache

33KB
614 lines

Bawawa: project management

Crates.io

Documentation

Usage

First, add this to your Cargo.toml:

[dependencies]
bawawa = "0.1"

Next, add this to your crate:

extern crate bawawa;

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in bawawa by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

process management

this module provides some wrapping around the standard library's std::process::Command and std::process::Child and associated types.

Here we provide an opinionated API where we capture standard inputs and outputs by default. The errors are also wrapped to provide better understanding of what did fail (especially the PID or the command line).

There are a couple of items to keep in mind when utilising this API:

  • as soon as Process is dropped the associated process will be terminated;
  • Process captures Stdout and Stderr, if you don't read the standard output it won't be visible on your terminal;
  • Process control Stdin too
  • the API utilizes the Future framework. If you don't push it in a runtime or call wait the functions will do nothing.

the Program

Program is an object that guarantees (within reason) the existence of a program within the execution environment. When constructing, the Program is checked so that once created it is known if it exists and if it has appropriate execution rights.

# use bawawa::{Program, Error};
#
let rustc = Program::new("rustc".to_owned())?;
# Ok::<(), Error>(())

the Command

this is the command line, the Program, the parameters and the associated environment variables necessary to spawn a new Process.

# use bawawa::{Command, Program, Error};
#
# let rustc = Program::new("rustc".to_owned())?;
let mut get_rustc_version = Command::new(rustc);
get_rustc_version.arguments(&["--version"]);

println!("{}", get_rustc_version);
# Ok::<(), Error>(())

spawn a Process

Once the Command is ready with the appropriate parameter it is possible to spawn a Process. The trait Control allows to follow the life cycle of the spawned Process.

# use bawawa::{Command, Control, Process, Program, Error};
#
# let rustc = Program::new("rustc".to_owned())?;
# let mut get_rustc_version = Command::new(rustc);
# get_rustc_version.arguments(&["--version"]);
let process = Process::spawn(get_rustc_version)?;

println!("spawned command: '{}' (PID: {})", process.command(), process.id());
# Ok::<(), Error>(())

We provide functions to capture the standard output and standard error output utilising the StandardOutput::capture_stdout or StandardError::capture_stderr and to send items to the standard inputs with StandardInput::send_stdin.

# use bawawa::{Command, Control, StandardOutput, Process, Program, Error};
# use futures::Stream as _;
#
# let rustc = Program::new("rustc".to_owned())?;
# let mut get_rustc_version = Command::new(rustc);
# get_rustc_version.arguments(&["--version"]);
# let process = Process::spawn(get_rustc_version)?;

let mut capture_stdout = process
.capture_stdout(
// specify the codec, the way to decode data
// from the captured output. Here we read line
// by line.
tokio_codec::LinesCodec::new()
)
.wait(); // from the _futures_ crate's Stream trait

println!("compiler: {}", capture_stdout.next().unwrap()?);
// compiler: rustc 1.35.0 (3c235d560 2019-05-20)
# Ok::<(), Error>(())

Dependencies

~2.5MB
~48K SLoC