Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip errors when no matches are found #112

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
));
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,
}
Loading