Add --decompose flag

This commit is contained in:
Kevin Liu 2016-09-04 06:07:56 -07:00
parent 64d2ff7521
commit e6daa3b532

View file

@ -18,25 +18,36 @@ fn main() {
.about("Rm ImProved .about("Rm ImProved
Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.") Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.")
.arg(Arg::with_name("SOURCE") .arg(Arg::with_name("SOURCE")
.help("File or directory to remove") .help("File or directory to remove")
.required(true) .required(true)
.multiple(true) .multiple(true)
.index(1)) .index(1)
.conflicts_with("decompose"))
.arg(Arg::with_name("graveyard") .arg(Arg::with_name("graveyard")
.help("Directory where deleted files go to rest") .help("Directory where deleted files go to rest")
.long("graveyard") .long("graveyard")
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("decompose")
.help("Permanently delete (unlink) the entire graveyard")
.long("decompose"))
.get_matches(); .get_matches();
let graveyard: &Path = Path::new(matches.value_of("graveyard") let graveyard: &Path = Path::new(matches.value_of("graveyard")
.unwrap_or(GRAVEYARD)); .unwrap_or(GRAVEYARD));
let sources: clap::Values = matches.values_of("SOURCE").unwrap();
let cwd: PathBuf = current_dir().expect("Error getting current directory"); if matches.is_present("decompose") {
if cwd.starts_with(graveyard) { fs::remove_dir_all(graveyard).expect("Failed to delete graveyard");
println!("You should use rm to delete files in the graveyard.");
return; return;
} }
let cwd: PathBuf = current_dir().expect("Error getting current directory");
if cwd.starts_with(graveyard) {
println!("You should use rm to delete files in the graveyard, \
or --decompose to delete everything at once.");
return;
}
let sources: clap::Values = matches.values_of("SOURCE").unwrap();
for source in sources { for source in sources {
if let Err(e) = bury(source, &cwd, graveyard) { if let Err(e) = bury(source, &cwd, graveyard) {
println!("ERROR: {}: {}", e, source); println!("ERROR: {}: {}", e, source);