Coder Social home page Coder Social logo

duct.rs's Introduction

duct.rs Travis build AppVeyor build crates.io docs.rs

A cross-platform library for running child processes and building pipelines.

duct wants to make shelling out in Rust as easy and flexible as it is in Bash. It takes care of gotchas and inconsistencies in the way different platforms shell out. And it's a cross-language library; the original implementation is in Python, with an identical API.

Changelog

  • Version 0.11 introduced the before_spawn method.
  • Version 0.10 changed how environment variable casing is handled on Windows. See the docs for env_remove.
  • Version 0.9 removed the sh function. It now lives in its own crate, duct_sh.

Example

duct tries to be as concise as possible, so that you don't wish you were back writing shell scripts. At the same time, it's explicit about what happens to output, and strict about error codes in child processes.

#[macro_use]
extern crate duct;

use duct::cmd;

fn main() {
    // Read the name of the current git branch. If git exits with an error
    // code here (because we're not in a git repo, for example), `read` will
    // return an error too.
    let current_branch = cmd!("git", "symbolic-ref", "--short", "HEAD").read().unwrap();

    // Log the current branch, with git taking over the terminal as usual.
    // The `cmd` function works just like the `cmd!` macro, but it takes a
    // collection instead of a variable list of arguments.
    let args = &["log", &current_branch];
    cmd("git", args).run().unwrap();

    // More complicated expressions become trees. Here's a pipeline with two
    // child processes on the left, just because we can. In Bash this would
    // be: stdout=$((echo -n part one "" && echo part two) | sed s/p/sm/g)
    let part_one = &["-n", "part", "one", ""];
    let stdout = cmd("echo", part_one)
        .then(cmd!("echo", "part", "two"))
        .pipe(cmd!("sed", "s/p/sm/g"))
        .read()
        .unwrap();
    assert_eq!("smart one smart two", stdout);
}

duct uses os_pipe internally, and the docs for that one include a big example that takes a dozen lines of code to read both stdout and stderr from a child process. duct can do that in one (moderately long) line:

let output = cmd!("sh", "-c", "echo foo && echo bar 2>&1").stderr_to_stdout().read().unwrap();

assert!(output.split_whitespace().eq(vec!["foo", "bar"]));

duct.rs's People

Contributors

faern avatar iwillspeak avatar matklad avatar oconnor663 avatar talhakhatri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.