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
Provenance
- Commit
a0505bd- Tree
478c87c- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
630749f
7 files changed+44 added−11 removed
modified:src/exec/command.rs+11 added-1 removed
9 unmodified lines10 use std::io::Write;11 use std::process::Command;12 use std::sync::Mutex;13 14+use crate::exit_codes::ExitCode;15+16 /// Executes a command.15-pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) {17+pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {18 // Spawn the supplied command.19 let output = cmd.output();20 21 // Then wait for the command to exit, if it was spawned.7 unmodified lines29 let stderr = io::stderr();30 31 let _ = stdout.lock().write_all(&output.stdout);32 let _ = stderr.lock().write_all(&output.stderr);33+34+ 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 lines6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9 use super::CommandTemplate;10+use crate::exit_codes::ExitCode;11 use crate::walk::WorkerResult;12 use std::path::PathBuf;13 use std::sync::mpsc::Receiver;14 use std::sync::{Arc, Mutex};30 unmodified lines45 cmd.generate_and_execute(&value, Arc::clone(&out_perm));46 }47 }48 48-pub fn batch(rx: Receiver<WorkerResult>, cmd: &CommandTemplate, show_filesystem_errors: bool) {49+pub 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 line59 }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 lines19 20 use lazy_static::lazy_static;21 use regex::Regex;22 23+use crate::exit_codes::ExitCode;24+25 use self::command::execute_command;26 use self::input::{basename, dirname, remove_extension};27 pub use self::job::{batch, job};28 use self::token::Token;124 unmodified lines153 for arg in &self.args[1..] {154 cmd.arg(arg.generate(&input).as_ref());155 }156 155- execute_command(cmd, &out_perm)157+ execute_command(cmd, &out_perm);158 }159 160 pub fn in_batch_mode(&self) -> bool {161 self.mode == ExecutionMode::Batch162 }163 162- 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 lines186 }187 }188 189 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 linesmodified:src/exit_codes.rs+2 added
1 pub enum ExitCode {2+ Success,3 GeneralError,4 KilledBySigint,5 }6 7 impl 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 linemodified:src/main.rs+5 added-1 removed
17 unmodified lines18 19 use std::env;20 use std::error::Error;21 use std::path::{Path, PathBuf};22+use std::process;23 use std::sync::Arc;24 use std::time;25 26 use atty::Stream;223 unmodified lines250 .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 linesmodified:src/walk.rs+10 added-4 removed
45 unmodified lines46 ///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.50-pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {50+pub 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 lines121 // Spawn the sender threads.122 spawn_senders(&config, &wants_to_quit, pattern, parallel_walker, tx);123 124 // Wait for the receiver thread to print out all results.125- receiver_thread.join().unwrap();125+ let exit_code = receiver_thread.join().unwrap();126 127 if wants_to_quit.load(Ordering::Relaxed) {128 process::exit(ExitCode::KilledBySigint.into());129 }130+131+ exit_code132 }133 134 fn 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);141 142 let show_filesystem_errors = config.show_filesystem_errors;2 unmodified lines145 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));152 153 let out_perm = Arc::new(Mutex::new(()));16 unmodified lines170 // Wait for all threads to exit before exiting the program.171 for h in handles {172 h.join().unwrap();173 }174+175+ ExitCode::Success176 }177 } else {178 let start = time::Instant::now();179 56 unmodified lines236 for value in buffer {237 output::print_entry(&mut stdout, &value, &config, &wants_to_quit);238 }239 }240+241+ ExitCode::Success242 }243 })244 }245 136 unmodified linesmodified:Cargo.lock+2 added
1+# This file is automatically @generated by Cargo.2+# It is not intended for manual editing.3 [[package]]4 name = "aho-corasick"5 version = "0.6.9"6 source = "registry+https://github.com/rust-lang/crates.io-index"512 unmodified lines