From 8529a5d3ce62b60e9c7c54058b0b07254fe93c44 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Tue, 2 Apr 2024 16:37:03 +0100 Subject: [PATCH] feat: confirmation before deleting targets --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main.rs b/src/main.rs index 0104544..c0f3c89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use anyhow::{Context, Result}; use clap::Parser; use colored::Colorize; use std::{fs, path, process}; +use std::{io, io::Write}; #[derive(Parser, Debug)] #[command(version, about, long_about = None, author)] @@ -11,10 +12,39 @@ struct Args { #[arg(long)] no_preserve: bool, + /// Ask once before removing all + #[arg(short, long)] + ask_once: bool, + + /// Ask before removing each target + #[arg(short = 'x', long, conflicts_with = "ask_once")] + ask_each: bool, + #[arg(trailing_var_arg = true, allow_hyphen_values = false)] targets: Vec, } +fn confirm_parse() { + io::stdout().flush().unwrap(); + + let mut confirm = String::new(); + io::stdin().read_line(&mut confirm).expect("failed to read input"); + + if confirm != "y\n" { + process::exit(0); + } +} + +fn confirm_once() { + print!("Are you sure you want to delete the specified targets? [y/N]: "); + confirm_parse(); +} + +fn confirm_each(target: &String) { + print!("Are you sure you want to delete {}? [y/N]: ", target); + confirm_parse(); +} + fn vaporise() -> Result<()> { let args = Args::parse(); @@ -27,12 +57,20 @@ fn vaporise() -> Result<()> { process::exit(0); } + if args.ask_once { + confirm_once(); + } + for target in args.targets.iter() { if !args.no_preserve && (target == "/" || target == "~") { println!("{}: you're trying to delete an important directory ({})! specify '{}' if you really want to do this", "error".red().bold(), "--no-preserve".yellow(), target); process::exit(0); } + if args.ask_each { + confirm_each(target); + } + if path::Path::new(target).exists() { if fs::metadata(target).unwrap().is_dir() { fs::remove_dir_all(target)