Skip to content

dakom/run_script

This branch is 2 commits ahead of, 74 commits behind sagiegurari/run_script:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b8b6ec7 · Jan 2, 2020

History

69 Commits
Feb 8, 2019
Jan 2, 2019
Dec 24, 2019
Jan 2, 2020
Jan 2, 2020
Dec 24, 2019
Nov 3, 2017
Nov 3, 2017
Nov 3, 2017
Sep 28, 2019
Dec 24, 2019
Nov 4, 2017
Aug 15, 2019
Dec 24, 2019
Dec 24, 2019

Repository files navigation

run_script

crates.io Build Status Build status codecov
license Libraries.io for GitHub Documentation downloads
Built with cargo-make

Run shell scripts in rust.

Overview

This library enables to invoke shell scripts based on their content.
While std::process::Command works great to execute standalone command, you need more manual code to take a script text and execute it.
For this purpose, this library was created.

Usage

Simply include the library and invoke the run/spawn function with the script text and run options:

extern crate run_script;

use run_script::ScriptOptions;

fn main() {
    let options = ScriptOptions::new();

    let args = vec![];

    // run the script and get the script execution output
    let (code, output, error) = run_script::run(
        r#"
        echo "Directory Info:"
        dir
        "#,
        &args,
        &options
    ).unwrap();

    println!("Exit Code: {}", code);
    println!("Output: {}", output);
    println!("Error: {}", error);

    // run the script and get a handle to the running child process
    let child = run_script::spawn(
        r#"
        echo "Directory Info:"
        dir
        "#,
        &args,
        &options
    ).unwrap();

    let spawn_output = child.wait_with_output().unwrap();

    println!("Success: {}", &spawn_output.status.success());
}

The library also provides the run_script! and spawn_script! macros for simpler usage.

#[macro_use]
extern crate run_script;

use run_script::ScriptOptions;

fn main() {
    // simple call to run script with only the script text
    let (code, output, error) = run_script!(
        r#"
        echo "Test"
        exit 0
        "#
    ).unwrap();

    // run script invoked with the script text and options
    let options = ScriptOptions::new();
    let (code, output, error) = run_script!(
        r#"
        echo "Test"
        exit 0
        "#,
        &options
    ).unwrap();

    // run script invoked with all arguments
    let options = ScriptOptions::new();
    let (code, output, error) = run_script!(
        r#"
        echo "Test"
        exit 0
        "#,
        &vec!["ARG1".to_string(), "ARG2".to_string()],
        &options
    ).unwrap();

    // spawn_script! works the same as run_script! but returns the child process handle
    let child = spawn_script!(
        r#"
        echo "Test"
        exit 0
        "#
    ).unwrap();
}

Installation

In order to use this library, just add it as a dependency:

[dependencies]
run_script = "*"

API Documentation

See full docs at: API Docs

Contributing

See contributing guide

Release History

Date Version Description
2019-12-24 v0.4.0 New spawn function and spawn_script! macro #7
2018-03-20 v0.1.14 Fix permissions issue (#2)
2017-12-23 v0.1.10 New run_script! macro
2017-11-04 v0.1.1 Initial release.

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

About

Run shell scripts in rust.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%