All Projects → rust-shell-script → Rust_cmd_lib

rust-shell-script / Rust_cmd_lib

Licence: apache-2.0
Common rust command-line macros and utilities, to write shell-script like tasks in a clean, natural and rusty way

Programming Languages

shell
77523 projects
rust
11053 projects
bash
514 projects
script
160 projects

Projects that are alternatives of or similar to Rust cmd lib

Tosheets
Send your stdin to google sheets
Stars: ✭ 536 (-11.26%)
Mutual labels:  command-line, pipe
Onhold
🔊 Play sounds while and after shell jobs complete
Stars: ✭ 146 (-75.83%)
Mutual labels:  command-line, pipe
Ipt
Interactive Pipe To: The Node.js cli interactive workflow
Stars: ✭ 783 (+29.64%)
Mutual labels:  command-line, pipe
Ncbi Genome Download
Scripts to download genomes from the NCBI FTP servers
Stars: ✭ 494 (-18.21%)
Mutual labels:  command-line
Telegram Send
Send messages and files over Telegram from the command-line.
Stars: ✭ 519 (-14.07%)
Mutual labels:  command-line
Carbon Now Cli
🎨 Beautiful images of your code — from right inside your terminal.
Stars: ✭ 5,165 (+755.13%)
Mutual labels:  command-line
Broot
A new way to see and navigate directory trees : https://dystroy.org/broot
Stars: ✭ 6,362 (+953.31%)
Mutual labels:  command-line
Cli
A command-line interface for Hetzner Cloud
Stars: ✭ 542 (-10.26%)
Mutual labels:  command-line
Rtv
Browse Reddit from your terminal
Stars: ✭ 4,558 (+654.64%)
Mutual labels:  command-line
Shiori
Simple bookmark manager built with Go
Stars: ✭ 5,315 (+779.97%)
Mutual labels:  command-line
Googler
🔍 Google from the terminal
Stars: ✭ 5,594 (+826.16%)
Mutual labels:  command-line
Git Labelmaker
🎏 Manage your GitHub labels from the command line!
Stars: ✭ 534 (-11.59%)
Mutual labels:  command-line
Jrnl
Collect your thoughts and notes without leaving the command line.
Stars: ✭ 5,126 (+748.68%)
Mutual labels:  command-line
Csvtk
A cross-platform, efficient and practical CSV/TSV toolkit in Golang
Stars: ✭ 566 (-6.29%)
Mutual labels:  command-line
Macports Base
The MacPorts command-line client
Stars: ✭ 502 (-16.89%)
Mutual labels:  command-line
Sconsify
A spotify console application
Stars: ✭ 572 (-5.3%)
Mutual labels:  command-line
Wow
😮❗️❗️ Wow❗️ now my Go commandline app is spinning with 🌈 and 🐴
Stars: ✭ 494 (-18.21%)
Mutual labels:  command-line
Ttyplot
a realtime plotting utility for terminal/console with data input from stdin
Stars: ✭ 532 (-11.92%)
Mutual labels:  pipe
Tools Osx
A small collection of command line tools for Mac OS X, incl.: clipcat, dict, eject, launch, ql, swuser, trash & with.
Stars: ✭ 576 (-4.64%)
Mutual labels:  command-line
Jsonui
jsonui is an interactive JSON explorer on your command line
Stars: ✭ 583 (-3.48%)
Mutual labels:  command-line

cmd_lib

Rust command-line library

Common rust command-line macros and utilities, to write shell-script like tasks easily in rust programming language. Available at crates.io.

Why you need this

If you need to run some external commands in rust, the std::process::Command is a good abstraction layer on top of different OS syscalls. It provides fine-grained control over how a new process should be spawned, and it allows you to wait for process to finish and check the exit status or collect all of its output. However, when Redirection or Piping is needed, you need to set up the parent and child IO handles manually, like this in the rust cookbook, which is often a tedious work.

A lot of developers just choose shell(sh, bash, ...) scripts for such tasks, by using < to redirect input, > to redirect output and '|' to pipe outputs. In my experience, this is the only good parts of shell script. You can find all kinds of pitfalls and mysterious tricks to make other parts of shell script work. As the shell scripts grow, they will ultimately be unmaintainable and no one wants to touch them any more.

This cmd_lib library is trying to provide the redirection and piping capabilities, and other facilities to make writing shell-script like tasks easily without launching any shell. For the rust cookbook examples, they can usually be implemented as one line of rust macro with the help of this library, as in the examples/rust_cookbook.rs. Since they are rust code, you can always rewrite them in rust natively in the future, if necessary without spawning external commands.

What this library looks like

To get a first impression, here is an example from examples/dd_test.rs:

use cmd_lib::{run_cmd, run_fun, spawn_with_output, use_builtin_cmd, CmdResult};

use_builtin_cmd!(info);
cmd_lib::set_debug(true);
run_cmd! (
    info "Dropping caches at first";
    sudo bash -c "echo 3 > /proc/sys/vm/drop_caches"
)?;
run_cmd!(info "Running with thread_num: $thread_num, block_size: $block_size")?;
let cnt: i32 = (DATA_SIZE / thread_num as i64 / block_size as i64) as i32;
let mut procs = vec![];
for i in 0..thread_num {
    let off = cnt * i;
    let proc = spawn_with_output!(
        sudo bash -c "dd if=$file of=/dev/null bs=$block_size skip=$off count=$cnt 2>&1"
    )?;
    procs.push(proc);
}
let mut total_bandwidth = 0;
cmd_lib::set_debug(false);
for (i, mut proc) in procs.into_iter().enumerate() {
    let output = proc.wait_result()?;
    let bandwidth = run_fun!(echo $output | awk r"/MB/ {print $10}")?;
    total_bandwidth += bandwidth.parse::<i32>().unwrap();
    run_cmd!(info "thread $i bandwidth: $bandwidth MB/s")?;
}
run_cmd!(info "Total bandwidth: $total_bandwidth MB/s")?;

Output will be like this:

➜  rust_cmd_lib git:(master) ✗ cargo run --example dd_test -- -b 4096 -f /dev/nvme0n1 -t 4
Dropping caches at first
Running "sudo bash -c echo 3 > /proc/sys/vm/drop_caches" ...
Running with thread_num: 4, block_size: 4096
Running "sudo bash -c dd if=/dev/nvme0n1 of=/dev/null bs=4096 skip=0 count=655360 2>&1" ...
Running "sudo bash -c dd if=/dev/nvme0n1 of=/dev/null bs=4096 skip=655360 count=655360 2>&1" ...
Running "sudo bash -c dd if=/dev/nvme0n1 of=/dev/null bs=4096 skip=1310720 count=655360 2>&1" ...
Running "sudo bash -c dd if=/dev/nvme0n1 of=/dev/null bs=4096 skip=1966080 count=655360 2>&1" ...
thread 0 bandwidth: 267 MB/s
thread 1 bandwidth: 266 MB/s
thread 2 bandwidth: 274 MB/s
thread 3 bandwidth: 304 MB/s
Total bandwidth: 1111 MB/s

What this library provides

Macros to run external commands

  • run_cmd! --> CmdResult
let msg = "I love rust";
run_cmd!(echo $msg)?;
run_cmd!(echo "This is the message: $msg")?;

// pipe commands are also supported
run_cmd!(du -ah . | sort -hr | head -n 10)?;

// or a group of commands
// if any command fails, just return Err(...)
let file = "/tmp/f";
let keyword = "rust";
if run_cmd! {
    cat ${file} | grep ${keyword};
    echo "bad cmd" >&2;
    ls /nofile || true;
    date;
    ls oops;
    cat oops;
}.is_err() {
    // your error handling code
}
  • run_fun! --> FunResult
let version = run_fun!(rustc --version)?;
eprintln!("Your rust version is {}", version);

// with pipes
let n = run_fun!(echo "the quick brown fox jumped over the lazy dog" | wc -w)?;
eprintln!("There are {} words in above sentence", n);

Abstraction without overhead

Since all the macros' lexical analysis and syntactic analysis happen at compile time, it can basically generate code the same as calling std::process APIs manually. It also includes command type checking, so most of the errors can be found at compile time instead of at runtime.

Intuitive parameters passing

When passing parameters to run_cmd! and run_fun! macros, if they are not part to rust String literals, they will be converted to string as an atomic component, so you don't need to quote them. The parameters will be like $a or ${a} in run_cmd! or run_fun! macros.

let dir = "my folder";
run_cmd!(echo "Creating $dir at /tmp")?;
run_cmd!(mkdir -p /tmp/$dir)?;

// or with group commands:
let dir = "my folder";
run_cmd!(echo "Creating $dir at /tmp"; mkdir -p /tmp/$dir)?;

You can consider "" as glue, so everything inside the quotes will be treated as a single atomic component.

If they are part of Raw string literals, there will be no string interpolation, the same as in idiomatic rust. However, you can always use format! macro to form the new string. For example:

// string interpolation
let key_word = "time";
let awk_opts = format!(r#"/{}/ {{print $(NF-3) " " $(NF-1) " " $NF}}"#, key_word);
run_cmd!(ping -c 10 www.google.com | awk $awk_opts)?;

Notice here $awk_opts will be treated as single option passing to awk command.

If you want to use dynamic parameters, you can use $[] to access vector variable:

let gopts = vec![vec!["-l", "-a", "/"], vec!["-a", "/var"]];
for opts in gopts {
    run_cmd!(ls $[opts])?;
}

Redirection and Piping

Right now piping and stdin, stdout, stderr redirection are supported. Most parts are the same as in bash scripts.

Builtin commands

cd

cd: set process current directory, which is always enabled

run_cmd! (
    cd /tmp;
    ls | wc -l;
)?;

Notice that builtin cd will only change with current scope and it will restore the previous current directory when it exits the scope.

Use std::env::set_current_dir if you want to change the current working directory for the whole program.

true

Just return true without launching any processes.

echo
use_builtin_cmd!(true, echo); // find more builtin commands in src/builtins.rs
run_cmd!(echo "This is from builtin command!")?;

Macros to register your own commands

Declare your function with export_cmd attribute, and import it with use_custom_cmd macro:

#[export_cmd(my_cmd)]
fn foo(args: CmdArgs, _envs: CmdEnvs) -> FunResult {
    println!("msg from foo(), args: {:?}", args);
    Ok("bar".into())
}

use_custom_cmd!(my_cmd);
run_cmd!(my_cmd)?;
println!("get result: {}", run_fun!(my_cmd)?);

Low-level process spawning macro

spawn!() macro executes the whole command as a child process, returning a handle to it. By default, stdin, stdout and stderr are inherited from the parent. To capture the output, you can use spawn_with_output!() macro instead.

To get result, you can call wait_result() to get CmdResult/FunResult.

spawn!(ping -c 10 192.168.0.1)?.wait_result()?;
let output = spawn_with_output!(/bin/cat file.txt | sed s/a/b/)?.wait_result();

Macros to define, get and set thread-local global variables

  • tls_init! to define thread local global variable
  • tls_get! to get the value
  • tls_set! to set the value
tls_init!(DELAY, f64, 1.0);
const DELAY_FACTOR: f64 = 0.8;
tls_set!(DELAY, |d| *d *= DELAY_FACTOR);
let d = tls_get!(DELAY);
// check more examples in examples/tetris.rs

Other Notes

Environment Variables

You can use std::env::var to fetch the environment variable key from the current process. It will report error if the environment variable is not present, and it also includes other checks to avoid silent failures.

To set environment variables, you can use std::env::set_var. There are also other related APIs in the std::env module.

To set environment variables for the command only, you can put the assignments before the command. Like this:

run_cmd!(FOO=100 /tmp/test_run_cmd_lib.sh)?;

Security Notes

Using macros can actually avoid command injection, since we do parsing before variable substitution. For example, below code is fine even without any quotes:

fn cleanup_uploaded_file(file: &str) -> CmdResult {
    run_cmd!(/bin/rm -f /var/upload/$file)
}

It is not the case in bash, which will always do variable substitution at first.

Glob/Wildcard

This library does not provide glob functions, to avoid silent errors and other surprises. You can use the glob package instead.

Thread Safety

This library tries very hard to not set global states, so parallel cargo test can be executed just fine. However, there might be some internal process related APIs which are not thread-safe. More investigation is needed.

License: MIT OR Apache-2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].