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