#shell #scripting #windows #github #script #unix #macro

btl

Simple shell scripting in rust. Github Repo: https://github.com/znx3p0/btlsh

13 releases

0.3.0 Apr 6, 2021
0.2.9 Feb 24, 2021
0.2.8 Jan 28, 2021
0.1.2 Jan 25, 2021

#1455 in Rust patterns

MIT license

8KB
174 lines

Btl

docs

Btl is a simple library that makes shell scripting with rust easier. It was originally written with the purposes of being used for build.rs files, but it can be used for more complex purposes.

It's main premise is about integrating shell scripting with rust ergonomically. This works both on windows and unix machines.

Btl is extremely simple, composed of six macros which have the exact same syntax. These macros allows for embedding shell scripts to rust while allowing extreme customization ergonomically.

Macros:

  • shell!{}
  • detach!{}
  • execute!{}
  • exec!{}
  • cd!{}

support for tokio is under the "tokio_shell" feature, and they have the same syntax and names, but they have the "async_" prefix.

NOTE: btl uses sh as the unix backend, and powershell as the windows backend.

If you happen to find any bug, please raise an issue at the github repo.

Example:

fn shell() -> Result<(), std::io::Error> {
    // all macros use the try operator to avoid unwrapping
    // creates a detached process that may outlive the current process
    detach! {
        "touch m.txt";
        "sleep {}", 10;
        "rm m.txt";
    };

    // changes directory
    cd!("..").unwrap();
    let contents = execute! {
        "ls";
    };
    // returns a string containing the stdout of the process
    println!("contents {:?}", contents);

    // returns a bool indicating if the operation was successful
    if exec! {
        "ls";
    } {
        println!("success")
    } else {
        println!("not successful")
    }

    shell! {
        "ls {}", "-la";
    };

    Ok(())
}

Dependencies

~0–8MB
~59K SLoC