Skip to content

Commit

Permalink
Expose exit status from --exec-batch <cmd>
Browse files Browse the repository at this point in the history
closes #333
  • Loading branch information
sharkdp committed Sep 13, 2019
1 parent 630749f commit d197519
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion src/exec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use std::io::Write;
use std::process::Command;
use std::sync::Mutex;

use crate::exit_codes::ExitCode;

/// Executes a command.
pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) {
pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {
// Spawn the supplied command.
let output = cmd.output();

Expand All @@ -28,12 +30,20 @@ pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) {

let _ = stdout.lock().write_all(&output.stdout);
let _ = stderr.lock().write_all(&output.stderr);

if output.status.code() == Some(0) {
ExitCode::Success
} else {
ExitCode::GeneralError
}
}
Err(ref why) if why.kind() == io::ErrorKind::NotFound => {
print_error!("Command not found: {:?}", cmd);
ExitCode::GeneralError
}
Err(why) => {
print_error!("Problem while executing command: {}", why);
ExitCode::GeneralError
}
}
}
9 changes: 7 additions & 2 deletions src/exec/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// according to those terms.

use super::CommandTemplate;
use crate::exit_codes::ExitCode;
use crate::walk::WorkerResult;
use std::path::PathBuf;
use std::sync::mpsc::Receiver;
Expand Down Expand Up @@ -45,7 +46,11 @@ pub fn job(
}
}

pub fn batch(rx: Receiver<WorkerResult>, cmd: &CommandTemplate, show_filesystem_errors: bool) {
pub fn batch(
rx: Receiver<WorkerResult>,
cmd: &CommandTemplate,
show_filesystem_errors: bool,
) -> ExitCode {
let paths = rx.iter().filter_map(|value| match value {
WorkerResult::Entry(val) => Some(val),
WorkerResult::Error(err) => {
Expand All @@ -55,5 +60,5 @@ pub fn batch(rx: Receiver<WorkerResult>, cmd: &CommandTemplate, show_filesystem_
None
}
});
cmd.generate_and_execute_batch(paths);
cmd.generate_and_execute_batch(paths)
}
10 changes: 7 additions & 3 deletions src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
use regex::Regex;

use crate::exit_codes::ExitCode;

use self::command::execute_command;
use self::input::{basename, dirname, remove_extension};
pub use self::job::{batch, job};
Expand Down Expand Up @@ -152,14 +154,14 @@ impl CommandTemplate {
cmd.arg(arg.generate(&input).as_ref());
}

execute_command(cmd, &out_perm)
execute_command(cmd, &out_perm);
}

pub fn in_batch_mode(&self) -> bool {
self.mode == ExecutionMode::Batch
}

pub fn generate_and_execute_batch<I>(&self, paths: I)
pub fn generate_and_execute_batch<I>(&self, paths: I) -> ExitCode
where
I: Iterator<Item = PathBuf>,
{
Expand All @@ -185,7 +187,9 @@ impl CommandTemplate {
}

if has_path {
execute_command(cmd, &Mutex::new(()));
execute_command(cmd, &Mutex::new(()))
} else {
ExitCode::Success
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/exit_codes.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
pub enum ExitCode {
Success,
GeneralError,
KilledBySigint,
}

impl Into<i32> for ExitCode {
fn into(self) -> i32 {
match self {
ExitCode::Success => 0,
ExitCode::GeneralError => 1,
ExitCode::KilledBySigint => 130,
}
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod walk;
use std::env;
use std::error::Error;
use std::path::{Path, PathBuf};
use std::process;
use std::sync::Arc;
use std::time;

Expand Down Expand Up @@ -250,7 +251,10 @@ fn main() {
.dot_matches_new_line(true)
.build()
{
Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),
Ok(re) => {
let exit_code = walk::scan(&dir_vec, Arc::new(re), Arc::new(config));
process::exit(exit_code.into());
}
Err(err) => {
print_error_and_exit!(
"{}\nHint: You can use the '--fixed-strings' option to search for a \
Expand Down
14 changes: 10 additions & 4 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum WorkerResult {
/// If the `--exec` argument was supplied, this will create a thread pool for executing
/// jobs in parallel from a given command line and the discovered paths. Otherwise, each
/// path will simply be written to standard output.
pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) -> ExitCode {
let mut path_iter = path_vec.iter();
let first_path_buf = path_iter
.next()
Expand Down Expand Up @@ -122,18 +122,20 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
spawn_senders(&config, &wants_to_quit, pattern, parallel_walker, tx);

// Wait for the receiver thread to print out all results.
receiver_thread.join().unwrap();
let exit_code = receiver_thread.join().unwrap();

if wants_to_quit.load(Ordering::Relaxed) {
process::exit(ExitCode::KilledBySigint.into());
}

exit_code
}

fn spawn_receiver(
config: &Arc<FdOptions>,
wants_to_quit: &Arc<AtomicBool>,
rx: Receiver<WorkerResult>,
) -> thread::JoinHandle<()> {
) -> thread::JoinHandle<ExitCode> {
let config = Arc::clone(config);
let wants_to_quit = Arc::clone(wants_to_quit);

Expand All @@ -144,7 +146,7 @@ fn spawn_receiver(
// This will be set to `Some` if the `--exec` argument was supplied.
if let Some(ref cmd) = config.command {
if cmd.in_batch_mode() {
exec::batch(rx, cmd, show_filesystem_errors);
exec::batch(rx, cmd, show_filesystem_errors)
} else {
let shared_rx = Arc::new(Mutex::new(rx));

Expand All @@ -169,6 +171,8 @@ fn spawn_receiver(
for h in handles {
h.join().unwrap();
}

ExitCode::Success
}
} else {
let start = time::Instant::now();
Expand Down Expand Up @@ -233,6 +237,8 @@ fn spawn_receiver(
output::print_entry(&mut stdout, &value, &config, &wants_to_quit);
}
}

ExitCode::Success
}
})
}
Expand Down

0 comments on commit d197519

Please sign in to comment.