2 releases

0.2.1 Mar 3, 2024
0.2.0 Mar 3, 2024

#489 in Procedural macros

Download history 9/week @ 2024-07-01 6/week @ 2024-07-08 61/week @ 2024-07-15 42/week @ 2024-07-22 81/week @ 2024-07-29 28/week @ 2024-08-05 34/week @ 2024-08-12 16/week @ 2024-08-19 41/week @ 2024-08-26 48/week @ 2024-09-02 4/week @ 2024-09-09 10/week @ 2024-09-16 50/week @ 2024-09-23 32/week @ 2024-09-30 26/week @ 2024-10-07 11/week @ 2024-10-14

119 downloads per month

MIT license

10KB
178 lines

sh: Command-running macro

This crate provides two macros for facilitating interactions with the underlying system. The [cmd] macro is the lower level macro that implements a DSL to construct QCmds. The [sh] macro is a thin wrapper on top that executes each command in sequence, panicking if there's a failure.

The DSL allows for easily piping data into and out of the commands from Strings and Vec<u8>s.

Examples

# use sh::sh;
# #[cfg(target_os = "linux")]
# fn run() {
let world = "world";
let mut out = String::new();
// We can use expressions as arguments
// and pipe the cmd output to a String or Vec<u8>
sh!(echo hello {world} > {&mut out});
assert_eq!(out, "hello world\n");

// We can also pipe a String/&str or Vec<u8>/&[u8] to a command
out.clear();
let input = "foo bar baz";
sh!(cat < {input} > {&mut out});
assert_eq!(&out, input);

// We can execute many commands at once
let mut out1 = String::new();
let mut out2 = String::new();
let mut out3 = String::new();

sh! {
  echo hello world 1 > {&mut out1}; // Note the `;`
  echo hello world 2 > {&mut out2};
  echo hello world 3 > {&mut out3};
}

assert_eq!(&out1, "hello world 1\n");
assert_eq!(&out2, "hello world 2\n");
assert_eq!(&out3, "hello world 3\n");
# }
# run();

For more information, see the documentation for [cmd].

Future Goals

  • Support piping from/to files
  • Support piping from one command to the next

Dependencies

~0.4–1MB
~21K SLoC