From 787bad133c9164358c496f5411ed6470e1e5046c Mon Sep 17 00:00:00 2001 From: Elias Wendland <193786789+eliaswen@users.noreply.github.com> Date: Thu, 16 Jul 2026 16:54:29 +0200 Subject: [PATCH] feat: add overwrite confirmation CLI flags --- src/args.rs | 16 ++++++++++++++++ src/main.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/args.rs b/src/args.rs index 2175857..3031a23 100644 --- a/src/args.rs +++ b/src/args.rs @@ -46,6 +46,22 @@ pub struct Args { )] pub quiet: bool, + #[arg( + short = 'y', + long, + help = "Overwrite output files without asking.", + conflicts_with = "no" + )] + pub yes: bool, + + #[arg( + short = 'n', + long, + help = "Do not overwrite output files, exit immediately if file exists.", + conflicts_with = "yes" + )] + pub no: bool, + #[arg( short = 'c', long, diff --git a/src/main.rs b/src/main.rs index 40821b6..44e5683 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,26 @@ use std::process; pub const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/version.txt")); +fn should_overwrite(args: &Args, out_path: &str) -> bool { + match (args.yes, args.no || args.quiet) { + (true, _) => true, + (_, true) => false, + (false, false) => { + print!("File '{}' already exists. Overwrite? [y/N]: ", out_path); + let _ = io::stdout().flush(); + + let mut input = String::new(); + match io::stdin().read_line(&mut input) { + Ok(_) => { + let input = input.trim().to_lowercase(); + input == "y" || input == "yes" + } + Err(_) => false, + } + } + } +} + #[tracing::instrument] fn main() { let mut command = Args::command(); @@ -164,6 +184,12 @@ fn main() { }; if let Some(out_path) = &args.output_path { + let out_path_file = std::path::Path::new(out_path); + if out_path_file.exists() && !should_overwrite(&args, out_path) { + tracing::info!("Not overwriting '{}'. Exiting.", out_path); + process::exit(1); + } + if let Err(e) = fs::write(out_path, &output_bytes) { tracing::error!("Error writing output file: {}", e); process::exit(1);