Execute

Build Status

This library is for extending Command in order to execute programs more easily.

Usage

```rust extern crate execute;

use std::process::Command;

use execute::Execute;

// ... ```

Verify the Program

Since Command is used for spawning a process of a command and the executed progrom is external which may not exist or may not be the program that we expected, we usually need to verify the external program at runtime.

The execute_check_exit_status_code method can be used to execute a command and check its exit status. For example,

```rust extern crate execute;

use std::process::Command;

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut firstcommand = Command::new(FFMPEGPATH);

first_command.arg("-version");

if firstcommand.executecheckexitstatuscode(0).iserr() { eprintln!("The path {} is not a correct FFmpeg executable binary file.", FFMPEG_PATH); } ```

Execute and Get the Exit Status

```rust extern crate execute;

use std::process::Command;

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut command = Command::new(FFMPEG_PATH);

command.arg("-i"); command.arg("/path/to/media-file"); command.arg("/path/to/output-file");

if let Some(exitcode) = command.execute().unwrap() { if exitcode == 0 { println!("Ok."); } else { eprintln!("Failed."); } } else { eprintln!("Interrupted!"); } ```

Execute and Get the Output

Output to the Screen

```rust extern crate execute;

use std::process::Command;

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut command = Command::new(FFMPEG_PATH);

command.arg("-i"); command.arg("/path/to/media-file"); command.arg("/path/to/output-file");

let output = command.execute_output().unwrap();

if let Some(exitcode) = output.status.code() { if exitcode == 0 { println!("Ok."); } else { eprintln!("Failed."); } } else { eprintln!("Interrupted!"); } ```

Output to Memory (Captured)

```rust extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut command = Command::new(FFMPEG_PATH);

command.arg("-i"); command.arg("/path/to/media-file"); command.arg("/path/to/output-file");

command.stdout(Stdio::piped()); command.stderr(Stdio::piped());

let output = command.execute_output().unwrap();

if let Some(exitcode) = output.status.code() { if exitcode == 0 { println!("Ok."); } else { eprintln!("Failed."); } } else { eprintln!("Interrupted!"); }

println!("{}", String::fromutf8(output.stdout).unwrap()); println!("{}", String::fromutf8(output.stderr).unwrap()); ```

Execute and Input Data

Input In-memory Data

```rust extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

let mut bc_command = Command::new("bc");

bc_command.stdout(Stdio::piped());

let output = bccommand.executeinput_output("2^99\n").unwrap();

println!("Answer: {}", String::fromutf8(output.stdout).unwrap().trimend()); ```

Input from a Reader

```rust extern crate execute;

use std::process::{Command, Stdio}; use std::fs::File;

use execute::Execute;

let mut cat_command = Command::new("cat");

cat_command.stdout(Stdio::piped());

let mut file = File::open("Cargo.toml").unwrap();

let output = catcommand.executeinputreaderoutput(&mut file).unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap()); ```

By default, the buffer size is 256 bytes. If you want to change that, you can use the _reader_output2 or _reader2 methods and define a length explicitly.

For example, to change the buffer size to 4096 bytes,

```rust extern crate execute;

use std::process::{Command, Stdio}; use std::fs::File;

use execute::generic_array::typenum::U4096; use execute::Execute;

let mut cat_command = Command::new("cat");

cat_command.stdout(Stdio::piped());

let mut file = File::open("Cargo.toml").unwrap();

let output = catcommand.executeinputreaderoutput2::(&mut file).unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap()); ```

Execute Many Commands and Pipe Them Together

```rust extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

let mut command1 = Command::new("echo"); command1.arg("HELLO WORLD");

let mut command2 = Command::new("cut"); command2.arg("-d").arg(" ").arg("-f").arg("1");

let mut command3 = Command::new("tr"); command3.arg("A-Z").arg("a-z");

command3.stdout(Stdio::piped());

let output = command1.executemanyoutput(&mut [&mut command2, &mut command3]).unwrap();

asserteq!(b"hello\n", output.stdout.asslice()); ```

Crates.io

https://crates.io/crates/execute

Documentation

https://docs.rs/execute

License

MIT