Browse Source

Allow multiple commands to be passed through.

tags/0.2.0^0
Bauke 6 months ago
parent
commit
aed16972cf
Signed by: Bauke <me@bauke.xyz> GPG Key ID: C1C0F29952BCF558
3 changed files with 38 additions and 35 deletions
  1. +1
    -1
      Cargo.lock
  2. +4
    -4
      Cargo.toml
  3. +33
    -30
      source/main.rs

+ 1
- 1
Cargo.lock View File

@@ -110,7 +110,7 @@ dependencies = [

[[package]]
name = "recursive-execute"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)",
"execute 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",


+ 4
- 4
Cargo.toml View File

@@ -3,7 +3,7 @@
[package]
name = "recursive-execute"
description = "Execute a command in specified directories recursively."
version = "0.1.1"
version = "0.2.0"
authors = ["Bauke <me@bauke.xyz>"]
license = "MIT OR Apache-2.0"
repository = "https://git.holllo.cc/Bauke/recursive-execute"
@@ -14,6 +14,6 @@ name = "recursive-execute"
path = "source/main.rs"

[dependencies]
clap = "2.33.1"
execute = "0.2.8"
walkdir = "2.3.1"
clap = "2.33"
execute = "0.2"
walkdir = "2.3"

+ 33
- 30
source/main.rs View File

@@ -26,9 +26,10 @@ fn main() {
// Options that are only allowed once.
Arg::with_name("command")
.long("command")
.long_help("The command to execute in each directory.")
.long_help("The command(s) to execute in each directory.")
.short("c")
.required(true)
.multiple(true)
.takes_value(true),
Arg::with_name("depth")
.default_value(default_depth)
@@ -51,9 +52,9 @@ fn main() {
// Extract boolean flags.
let verbose = cli.is_present("verbose");

// Extract the command to execute.
let target_command = cli
.value_of("command")
// Extract the commands to execute.
let target_commands = cli
.values_of("command")
.expect("Failed to extract the command to execute from the CLI");

// Extract the target directories.
@@ -95,35 +96,37 @@ fn main() {
continue 'walker_loop;
}

if verbose {
// Print what we're doing and where we are.
print!(
"Executing \"{}\" in {:?} (current depth {})",
target_command, dir_path, current_depth
);
}

// Spawn the command at the directory with the specified stdout.
// Print an error and continue if one occurs.
let mut child = match command(target_command)
.stdout(Stdio::inherit())
.current_dir(dir_path)
.spawn()
{
Ok(value) => value,
Err(err) => {
println!("Error executing command: {}", err);
println!();
continue 'walker_loop;
for target_command in target_commands.clone() {
if verbose {
// Print what we're doing and where we are.
print!(
"Executing \"{}\" in {:?} (current depth {})",
target_command, dir_path, current_depth
);
}
};

// Wait for the command to finish before continuing.
child.wait().expect("Failed to wait for child process");
// Spawn the command at the directory with the specified stdout.
// Print an error and continue if one occurs.
let mut child = match command(target_command)
.stdout(Stdio::inherit())
.current_dir(dir_path)
.spawn()
{
Ok(value) => value,
Err(err) => {
println!("Error executing command: {}", err);
println!();
continue 'walker_loop;
}
};

// If verbose was specified, print an extra newline to make the output more readable.
if verbose {
println!();
// Wait for the command to finish before continuing.
child.wait().expect("Failed to wait for child process");

// If verbose was specified, print an extra newline to make the output more readable.
if verbose {
println!();
}
}
}
}


Loading…
Cancel
Save