easy_cmd/macros.rs
1/// Runs a system command and exits with code 1 if an error occurs.
2///
3/// # Arguments
4///
5/// * `$cmd` - The command to run in the current working directory. The command can be represented
6/// as a single string or a collection of strings, or as any other type that implements the
7/// [`crate::AsCommand`] trait.
8///
9/// # Example
10///
11/// ```
12/// use easy_cmd::run_command;
13///
14/// // Run a command in the current working directory.
15/// run_command!("git status");
16///
17/// // The command can also be provided as a collection of strings.
18/// run_command!(&["git", "status"]);
19/// ```
20#[macro_export]
21macro_rules! run_command {
22 ($cmd:expr) => {{
23 // Note that we need to supply &str as a type parameter because even though it isn't used,
24 // the compiler still needs to know the type, and it cannot infer it from the macro call.
25 if let Err(e) = easy_cmd::run_command::<_, &str>($cmd, None) {
26 eprintln!("{}", e);
27 std::process::exit(1);
28 }
29 }};
30}
31
32/// Runs a system command in a specific directory and exits with code 1 if an error occurs.
33///
34/// # Arguments
35///
36/// * `$cmd` - The command to run in the directory specified by `$dir`. The command can be
37/// represented as a single string or a collection of strings, or as any other type that
38/// implements the [`crate::AsCommand`] trait.
39/// * `$dir` - Path to the directory that the command specified by `$cmd` should be run in (can be a
40/// `&str`, [`String`], [`std::path::Path`], or [`std::path::PathBuf`].
41///
42/// # Example
43///
44/// ```
45/// use easy_cmd::run_command_in_dir;
46/// use std::path::Path;
47///
48/// // Run a command in a specific directory.
49/// run_command_in_dir!("git status", "src");
50///
51/// // The command can also be provided as a collection of strings, and the directory can also be
52/// // specified as a Path.
53/// run_command_in_dir!(&["git", "status"], Path::new("src"));
54/// ```
55#[macro_export]
56macro_rules! run_command_in_dir {
57 ($cmd:expr, $dir:expr) => {{
58 if let Err(e) = easy_cmd::run_command($cmd, Some($dir)) {
59 eprintln!("{}", e);
60 std::process::exit(1);
61 }
62 }};
63}