Skip to content

Commit

Permalink
Add option to skip errors when no matches are found
Browse files Browse the repository at this point in the history
  • Loading branch information
roqvist authored and dmerejkowsky committed Oct 12, 2024
1 parent 3f3b2d8 commit be5aec2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ struct Options {
help = "Whether to enable colorful output. Choose between 'always', 'auto', or 'never'. Default is 'auto'"
)]
color_when: Option<ColorWhen>,

#[arg(
short = 'e',
long = "allow-empty",
help = "Exits without error when no matches are found."
)]
allow_empty: bool,
}

fn regex_query_or_die(pattern: &str, replacement: &str, word: bool) -> Query {
Expand Down Expand Up @@ -202,6 +209,7 @@ pub fn run() -> Result<()> {
selected_file_types,
preserve_case,
word_regex,
allow_empty
} = opt;

let dry_run = !go;
Expand Down Expand Up @@ -230,6 +238,7 @@ pub fn run() -> Result<()> {
ignored,
selected_file_types,
ignored_file_types,
allow_empty
};

let path = path.unwrap_or_else(|| Path::new(".").to_path_buf());
Expand Down Expand Up @@ -265,12 +274,24 @@ fn run_on_directory(
directory_patcher.run(&query)?;
let stats = directory_patcher.stats();
if stats.total_replacements() == 0 {
console.print_error(&format!(
"{}: {}",
"Error".bold().red(),
"nothing found to replace"
));
process::exit(2);
match settings.allow_empty {
true => {
console.print_message(&format!(
"{}",
"nothing found to replace"
));

Check failure on line 282 in src/app.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

error: useless use of `format!` --> src/app.rs:279:40 | 279 | console.print_message(&format!( | ________________________________________^ 280 | | "{}", 281 | | "nothing found to replace" 282 | | )); | |_________________^ help: consider using `.to_string()`: `"nothing found to replace".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `-D clippy::useless-format` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_format)]`

Check failure on line 282 in src/app.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

error: useless use of `format!` --> src/app.rs:279:40 | 279 | console.print_message(&format!( | ________________________________________^ 280 | | "{}", 281 | | "nothing found to replace" 282 | | )); | |_________________^ help: consider using `.to_string()`: `"nothing found to replace".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `-D clippy::useless-format` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_format)]`
process::exit(0);
},
false => {
console.print_error(&format!(
"{}: {}",
"Error".bold().red(),
"nothing found to replace"
));
process::exit(2);
}
}

}

let stats = &stats;
Expand Down
2 changes: 2 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ pub struct Settings {
pub selected_file_types: Vec<String>,
/// List of file types to ignore (default: empty)
pub ignored_file_types: Vec<String>,
/// Exits without an error when no matches are found (default: false)
pub allow_empty: bool,
}

0 comments on commit be5aec2

Please sign in to comment.