added flag to enable prompt before operatoins; fixed argument parsing bug

This commit is contained in:
ascendforever 2023-10-24 18:31:42 -04:00
parent d037fa3ea4
commit 57d664807b
2 changed files with 30 additions and 12 deletions

Binary file not shown.

View file

@ -45,25 +45,37 @@ struct CLIArguments {
help="Perform no operations on the filesystem")] help="Perform no operations on the filesystem")]
dry_run: bool, dry_run: bool,
#[structopt(short="i",
help="Prompt once before operating")]
prompt: bool,
#[structopt(short, long, value_name="VALUE", #[structopt(short, long, value_name="VALUE",
help="Minimum file size to be considered for hardlinking\nNever goes below 1 (the default)")] help="Minimum file size to be considered for hardlinking\nNever goes below 1 (the default)")]
min_size: u64, min_size: Option<u64>,
#[structopt(value_name="TARGET", #[structopt(value_name="TARGET",
help="Target files and directories (recursive)\nEach ';' denotes a new set of targets\n Each set of targets are separate from all other sets\n All targets must be on the same device\nAll symlinks are ignored")] help="Target files and directories (recursive)\nEach ';' denotes a new set of targets\n Each set of targets are separate from all other sets\n All targets must be on the same device\nAll symlinks are ignored")]
targets: Vec<String>, targets: Vec<String>,
} }
impl Default for CLIArguments {
fn default() -> Self { /// return whether or not user gave confirmation
Self { fn prompt_confirm(run_targets: &Vec<Vec<String>>) -> bool {
verbose: 0, println!("Are you sure you want to link all duplicates in each of these sets of targets?");
quiet: 0, for spaths in run_targets {
no_brace_output: false, println!(" {}", shlex::join(spaths.iter().map(|string| string.as_str())));
dry_run: false,
min_size: 1,
targets: Vec::new()
} }
print!("> ");
std::io::stdout().flush().unwrap_or_else(|_| ());
let mut response = String::new();
std::io::stdin().read_line(&mut response).unwrap_or_else(
|_| {
eprintln!("Problem reading input");
std::process::exit(1);
} }
);
response.to_lowercase().starts_with("y")
} }
fn process_args() -> (Vec<Vec<PathBuf>>, Config) { fn process_args() -> (Vec<Vec<PathBuf>>, Config) {
@ -71,6 +83,12 @@ fn process_args() -> (Vec<Vec<PathBuf>>, Config) {
let run_targets: Vec<Vec<String>> = split_vec(&args.targets, ";"); let run_targets: Vec<Vec<String>> = split_vec(&args.targets, ";");
if args.prompt {
if !prompt_confirm(&run_targets) {
std::process::exit(0);
}
}
let run_paths: Vec<Vec<PathBuf>> = run_targets.iter().enumerate().map( let run_paths: Vec<Vec<PathBuf>> = run_targets.iter().enumerate().map(
|(i,spaths)| { |(i,spaths)| {
if spaths.len() < 2 { if spaths.len() < 2 {
@ -94,7 +112,7 @@ fn process_args() -> (Vec<Vec<PathBuf>>, Config) {
} }
(run_paths, Config { (run_paths, Config {
min_size: std::cmp::max(1, args.min_size), min_size: args.min_size.unwrap_or(1),
no_brace_output: args.no_brace_output, no_brace_output: args.no_brace_output,
dry_run: args.dry_run, dry_run: args.dry_run,
verbosity: args.verbose - args.quiet verbosity: args.verbose - args.quiet