Skip to content
Myr

Rename error => print_error_and_exit and introduce print_error

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

authored by sharkdp · committed by David Peter · parent 6407dc4 · Browse files · 4 files+23 added−18 removed
This commit

State

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

Diff stats

4 files+23 added−18 removed

  • other:+23 added−18 removed

4 files+23 added−18 removed

modified:src/exec/job.rs+2 added-2 removed
5 unmodified lines
6// notice may not be copied, modified, or distributed except7// according to those terms.89use super::CommandTemplate;10use internal::error;10use internal::print_error_and_exit;11use std::path::PathBuf;12use std::sync::mpsc::Receiver;13use std::sync::{Arc, Mutex};14use walk::WorkerResult;
15 unmodified lines
30 let value: PathBuf = match lock.recv() {31 Ok(value) => match value {32 WorkerResult::Entry(val) => val,33 WorkerResult::Error(err) => {34 error(&format!("{}", err));34 print_error_and_exit(&format!("{}", err));35 }36 },37 Err(_) => break,38 };
7 unmodified lines
modified:src/internal.rs+7 added-3 removed
5 unmodified lines
6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::ffi::OsString;10use std::io::Write;10use std::path::PathBuf;11use std::process;12use std::time;13
158 unmodified lines
172 /// The given constraints on the size of returned files173 pub size_constraints: Vec<SizeFilter>,174}175176/// Print error message to stderr.177pub fn print_error(message: &str) {178 eprintln!("{}", message);179}180181/// Print error message to stderr and exit with status `1`.178pub fn error(message: &str) -> ! {179 writeln!(&mut ::std::io::stderr(), "{}", message).expect("Failed writing to stderr");182pub fn print_error_and_exit(message: &str) -> ! {183 print_error(message);184 process::exit(1);185}186187/// Determine if a regex pattern contains a literal uppercase character.
298 unmodified lines
modified:src/main.rs+8 added-7 removed
36 unmodified lines
37use regex::{RegexBuilder, RegexSetBuilder};3839use exec::CommandTemplate;40use internal::{41 error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions, FileTypes, SizeFilter,41 pattern_has_uppercase_char, print_error_and_exit, transform_args_with_exec, FdOptions,42 FileTypes, SizeFilter,43};44use lscolors::LsColors;4546fn main() {
5 unmodified lines
5253 // Get the current working directory54 let current_dir = Path::new(".");55 if !fshelper::is_dir(current_dir) {55 error("Error: could not get current directory.");56 print_error_and_exit("Error: could not get current directory.");57 }5859 // Get one or more root directories to search.60 let mut dir_vec: Vec<_> = match matches.values_of("path") {61 Some(paths) => paths62 .map(|path| {63 let path_buffer = PathBuf::from(path);64 if !fshelper::is_dir(&path_buffer) {64 error(&format!(65 print_error_and_exit(&format!(66 "Error: '{}' is not a directory.",67 path_buffer.to_string_lossy()68 ));69 }
19 unmodified lines
89 if !matches.is_present("full-path")90 && pattern.contains(std::path::MAIN_SEPARATOR)91 && fshelper::is_dir(Path::new(pattern))92 {92 error(&format!(93 print_error_and_exit(&format!(94 "Error: The search pattern '{pattern}' contains a path-separation character ('{sep}') \95 and will not lead to any search results.\n\n\96 If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \97 fd . '{pattern}'\n\n\
44 unmodified lines
142 v.map(|sf| {143 if let Some(f) = SizeFilter::from_string(sf) {144 return f;145 }145 error(&format!("Error: {} is not a valid size constraint.", sf));146 print_error_and_exit(&format!("Error: {} is not a valid size constraint.", sf));147 })148 .collect()149 })150 .unwrap_or_else(|| vec![]);
60 unmodified lines
211 .case_insensitive(true)212 .build()213 {214 Ok(re) => re,214 Err(err) => error(err.description()),215 Err(err) => print_error_and_exit(err.description()),216 }217 }),218 command,219 exclude_patterns: matches
12 unmodified lines
232 .dot_matches_new_line(true)233 .build()234 {235 Ok(re) => walk::scan(&dir_vec, Arc::new(re), Arc::new(config)),235 Err(err) => error(236 Err(err) => print_error_and_exit(237 format!(238 "{}\nHint: You can use the '--fixed-strings' option to search for a \239 literal string instead of a regular expression",240 err.description()
5 unmodified lines
modified:src/walk.rs+6 added-6 removed
8 unmodified lines
9extern crate ctrlc;1011use exec;12use fshelper;13use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};13use internal::{print_error, print_error_and_exit, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};14use output;1516use std::error::Error;17use std::path::PathBuf;
41 unmodified lines
5960 for pattern in &config.exclude_patterns {61 let res = override_builder.add(pattern);62 if res.is_err() {63 error(&format!("Error: malformed exclude pattern '{}'", pattern));63 print_error_and_exit(&format!("Error: malformed exclude pattern '{}'", pattern));64 }65 }66 let overrides = override_builder.build().unwrap_or_else(|_| {67 error("Mismatch in exclude patterns");67 print_error_and_exit("Mismatch in exclude patterns");68 });6970 let mut walker = WalkBuilder::new(first_path_buf.as_path());71 walker
16 unmodified lines
88 if let Some(err) = result {89 match err {90 ignore::Error::Partial(_) => (),91 _ => {92 error(&format!(92 print_error_and_exit(&format!(93 "Error while parsing custom ignore file '{}': {}.",94 ignore_file.to_string_lossy(),95 err.description()96 ));
91 unmodified lines
188 }189 }190 }191 WorkerResult::Error(err) => {192 error(&format!("{}", err));192 print_error(&format!("{}", err));193 }194 }195 }196
93 unmodified lines
290291 let search_str_o = if config.search_full_path {292 match fshelper::path_absolute_form(entry_path) {293 Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),294 Err(_) => error("Error: unable to get full path."),294 Err(_) => print_error_and_exit("Error: unable to get full path."),295 }296 } else {297 entry_path.file_name().map(|f| f.to_string_lossy())298 };
25 unmodified lines