#shell #duct

bin+lib duct

a library for running child processes

21 releases (11 breaking)

✓ Uses Rust 2018 edition

0.13.3 Oct 30, 2019
0.13.0 Sep 22, 2019
0.12.0 Feb 15, 2019
0.11.1 Sep 20, 2018
0.1.0 Feb 20, 2016
Download history 1126/week @ 2019-07-17 1193/week @ 2019-07-24 1308/week @ 2019-07-31 1800/week @ 2019-08-07 1396/week @ 2019-08-14 1207/week @ 2019-08-21 1638/week @ 2019-08-28 1321/week @ 2019-09-04 1240/week @ 2019-09-11 1280/week @ 2019-09-18 1509/week @ 2019-09-25 1533/week @ 2019-10-02 1766/week @ 2019-10-09 1319/week @ 2019-10-16 1434/week @ 2019-10-23

6,126 downloads per month
Used in 22 crates (20 directly)

MIT license

105KB
1.5K SLoC

duct.rs Actions Status crates.io docs.rs

Duct is a library for running child processes. Duct makes it easy to build pipelines and redirect IO like a shell. At the same time, Duct helps you write correct, portable code: whitespace is never significant, errors from child processes get reported by default, and a variety of gotchas, bugs, and platform inconsistencies are handled for you the Right Way™.

Changelog

  • v0.13.3
    • Added Handle::pids and ReaderHandle::pids.
  • v0.13.2
    • Added ReaderHandle::try_wait.
    • Debug output for Expression is more concise.
  • v0.13.1
    • Added ReaderHandle::kill.
    • Kill methods no longer wait on IO threads to complete. This avoids blocking on unkilled grandchildren.
  • v0.13.0
    • The kill method now reaps killed child processes before returning.
    • Removed the then method.
    • Added ReaderHandle and Expression::reader.
    • Added Expression::stdout_stderr_swap.
    • Renamed stdin/stdout/stderr to stdin_path/stdout_path/stderr_path.
    • Renamed stdin_handle/stdout_handle/stderr_handle to stdin_file/stdout_file/stderr_file.
    • Renamed input to stdin_bytes.
    • Renamed Handle::output to Handle::into_output.

Examples

Run a command without capturing any output. Here "hi" is printed directly to the terminal:

use duct::cmd;
cmd!("echo", "hi").run()?;

Capture the standard output of a command. Here "hi" is returned as a String:

let stdout = cmd!("echo", "hi").read()?;
assert_eq!(stdout, "hi");

Capture the standard output of a pipeline:

let stdout = cmd!("echo", "hi").pipe(cmd!("sed", "s/i/o/")).read()?;
assert_eq!(stdout, "ho");

Merge standard error into standard output and read both incrementally:

use duct::cmd;
use std::io::prelude::*;
use std::io::BufReader;

let big_cmd = cmd!("bash", "-c", "echo out && echo err 1>&2");
let reader = big_cmd.stderr_to_stdout().reader()?;
let mut lines = BufReader::new(reader).lines();
assert_eq!(lines.next().unwrap()?, "out");
assert_eq!(lines.next().unwrap()?, "err");

Children that exit with a non-zero status return an error by default:

let result = cmd!("false").run();
assert!(result.is_err());

Dependencies

~125KB