Skip to content
Myr

Expose exit status from `--exec-batch <cmd>`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #333

authored by sharkdp · committed by David Peter · parent 630749f · Browse files · 7 files+44 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+44 added−11 removed

  • other:+44 added−11 removed

7 files+44 added−11 removed

modified:src/exec/command.rs+11 added-1 removed
9 unmodified lines
10use std::io::Write;11use std::process::Command;12use std::sync::Mutex;1314use crate::exit_codes::ExitCode;1516/// Executes a command.15pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) {17pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {18 // Spawn the supplied command.19 let output = cmd.output();2021 // Then wait for the command to exit, if it was spawned.
7 unmodified lines
29 let stderr = io::stderr();3031 let _ = stdout.lock().write_all(&output.stdout);32 let _ = stderr.lock().write_all(&output.stderr);3334 if output.status.code() == Some(0) {35 ExitCode::Success36 } else {37 ExitCode::GeneralError38 }39 }40 Err(ref why) if why.kind() == io::ErrorKind::NotFound => {41 print_error!("Command not found: {:?}", cmd);42 ExitCode::GeneralError43 }44 Err(why) => {45 print_error!("Problem while executing command: {}", why);46 ExitCode::GeneralError47 }48 }49}
modified:src/exec/job.rs+7 added-2 removed
5 unmodified lines
6// notice may not be copied, modified, or distributed except7// according to those terms.89use super::CommandTemplate;10use crate::exit_codes::ExitCode;11use crate::walk::WorkerResult;12use std::path::PathBuf;13use std::sync::mpsc::Receiver;14use std::sync::{Arc, Mutex};
30 unmodified lines
45 cmd.generate_and_execute(&value, Arc::clone(&out_perm));46 }47}4848pub fn batch(rx: Receiver<WorkerResult>, cmd: &CommandTemplate, show_filesystem_errors: bool) {49pub fn batch(50 rx: Receiver<WorkerResult>,51 cmd: &CommandTemplate,52 show_filesystem_errors: bool,53) -> ExitCode {54 let paths = rx.iter().filter_map(|value| match value {55 WorkerResult::Entry(val) => Some(val),56 WorkerResult::Error(err) => {57 if show_filesystem_errors {
1 unmodified line
59 }60 None61 }62 });58 cmd.generate_and_execute_batch(paths);63 cmd.generate_and_execute_batch(paths)64}
modified:src/exec/mod.rs+7 added-3 removed
18 unmodified lines
1920use lazy_static::lazy_static;21use regex::Regex;2223use crate::exit_codes::ExitCode;2425use self::command::execute_command;26use self::input::{basename, dirname, remove_extension};27pub use self::job::{batch, job};28use self::token::Token;
124 unmodified lines
153 for arg in &self.args[1..] {154 cmd.arg(arg.generate(&input).as_ref());155 }156155 execute_command(cmd, &out_perm)157 execute_command(cmd, &out_perm);158 }159160 pub fn in_batch_mode(&self) -> bool {161 self.mode == ExecutionMode::Batch162 }163162 pub fn generate_and_execute_batch<I>(&self, paths: I)164 pub fn generate_and_execute_batch<I>(&self, paths: I) -> ExitCode165 where166 I: Iterator<Item = PathBuf>,167 {168 let mut cmd = Command::new(self.args[0].generate("").as_ref());
17 unmodified lines
186 }187 }188189 if has_path {188 execute_command(cmd, &Mutex::new(()));190 execute_command(cmd, &Mutex::new(()))191 } else {192 ExitCode::Success193 }194 }195}196
153 unmodified lines
modified:src/exit_codes.rs+2 added
1pub enum ExitCode {2 Success,3 GeneralError,4 KilledBySigint,5}67impl Into<i32> for ExitCode {8 fn into(self) -> i32 {9 match self {10 ExitCode::Success => 0,11 ExitCode::GeneralError => 1,12 ExitCode::KilledBySigint => 130,13 }14 }
1 unmodified line
modified:src/main.rs+5 added-1 removed
17 unmodified lines
1819use std::env;20use std::error::Error;21use std::path::{Path, PathBuf};22use std::process;23use std::sync::Arc;24use std::time;2526use atty::Stream;
223 unmodified lines
250 .case_insensitive(!config.case_sensitive)251 .dot_matches_new_line(true)252 .build()253 {253 Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),254 Ok(re) => {255 let exit_code = walk::scan(&dir_vec, Arc::new(re), Arc::new(config));256 process::exit(exit_code.into());257 }258 Err(err) => {259 print_error_and_exit!(260 "{}\nHint: You can use the '--fixed-strings' option to search for a \261 literal string instead of a regular expression",
5 unmodified lines
modified:src/walk.rs+10 added-4 removed
45 unmodified lines
46///47/// If the `--exec` argument was supplied, this will create a thread pool for executing48/// jobs in parallel from a given command line and the discovered paths. Otherwise, each49/// path will simply be written to standard output.50pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {50pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) -> ExitCode {51 let mut path_iter = path_vec.iter();52 let first_path_buf = path_iter53 .next()54 .expect("Error: Path vector can not be empty");
66 unmodified lines
121 // Spawn the sender threads.122 spawn_senders(&config, &wants_to_quit, pattern, parallel_walker, tx);123124 // Wait for the receiver thread to print out all results.125 receiver_thread.join().unwrap();125 let exit_code = receiver_thread.join().unwrap();126127 if wants_to_quit.load(Ordering::Relaxed) {128 process::exit(ExitCode::KilledBySigint.into());129 }130131 exit_code132}133134fn spawn_receiver(135 config: &Arc<FdOptions>,136 wants_to_quit: &Arc<AtomicBool>,137 rx: Receiver<WorkerResult>,136) -> thread::JoinHandle<()> {138) -> thread::JoinHandle<ExitCode> {139 let config = Arc::clone(config);140 let wants_to_quit = Arc::clone(wants_to_quit);141142 let show_filesystem_errors = config.show_filesystem_errors;
2 unmodified lines
145 thread::spawn(move || {146 // This will be set to `Some` if the `--exec` argument was supplied.147 if let Some(ref cmd) = config.command {148 if cmd.in_batch_mode() {147 exec::batch(rx, cmd, show_filesystem_errors);149 exec::batch(rx, cmd, show_filesystem_errors)150 } else {151 let shared_rx = Arc::new(Mutex::new(rx));152153 let out_perm = Arc::new(Mutex::new(()));
16 unmodified lines
170 // Wait for all threads to exit before exiting the program.171 for h in handles {172 h.join().unwrap();173 }174175 ExitCode::Success176 }177 } else {178 let start = time::Instant::now();179
56 unmodified lines
236 for value in buffer {237 output::print_entry(&mut stdout, &value, &config, &wants_to_quit);238 }239 }240241 ExitCode::Success242 }243 })244}245
136 unmodified lines
modified:Cargo.lock+2 added
1# This file is automatically @generated by Cargo.2# It is not intended for manual editing.3[[package]]4name = "aho-corasick"5version = "0.6.9"6source = "registry+https://github.com/rust-lang/crates.io-index"
512 unmodified lines