feat: add overwrite confirmation CLI flags
This commit is contained in:
1 parent
f8b5372e2e
commit
787bad133c
2 files changed
+42
No files matched your search
+16
@@ -46,6 +46,22 @@ pub struct Args {
|
|||||||
)]
|
)]
|
||||||
pub quiet: bool,
|
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(
|
#[arg(
|
||||||
short = 'c',
|
short = 'c',
|
||||||
long,
|
long,
|
||||||
|
|||||||
+26
@@ -28,6 +28,26 @@ use std::process;
|
|||||||
|
|
||||||
pub const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/version.txt"));
|
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]
|
#[tracing::instrument]
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut command = Args::command();
|
let mut command = Args::command();
|
||||||
@@ -164,6 +184,12 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Some(out_path) = &args.output_path {
|
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) {
|
if let Err(e) = fs::write(out_path, &output_bytes) {
|
||||||
tracing::error!("Error writing output file: {}", e);
|
tracing::error!("Error writing output file: {}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
|
|||||||
Reference in new issue
Block a user