Implement --clear-cache flag

This commit is contained in:
Danilo Bargen 2016-01-10 22:39:00 +01:00
commit 55bbd78360
2 changed files with 23 additions and 3 deletions

View file

@ -127,4 +127,19 @@ impl Cache {
}
}
/// Delete the cache directory.
pub fn clear(&self) -> Result<(), TldrError> {
let path = try!(self.get_cache_dir());
if path.exists() && path.is_dir() {
try!(fs::remove_dir_all(&path).map_err(|_| {
CacheError(format!("Could not remove cache directory ({}).", path.display()))
}));
} else if path.exists() {
return Err(CacheError(format!("Cache path ({}) is not a directory.", path.display())));
} else {
return Err(CacheError(format!("Cache path ({}) does not exist.", path.display())));
};
Ok(())
}
}

View file

@ -124,8 +124,13 @@ fn main() {
// Clear cache, pass through
if args.flag_clear_cache {
println!("Flag --clear-cache not yet implemented.");
process::exit(1);
cache.clear().unwrap_or_else(|e| {
match e {
UpdateError(msg) | CacheError(msg) => println!("Could not delete cache: {}", msg),
};
process::exit(1);
});
println!("Successfully deleted cache.");
}
// Update cache, pass through
@ -196,7 +201,7 @@ fn main() {
}
// Some flags can be run without a command.
if !args.flag_update {
if !(args.flag_update || args.flag_clear_cache) {
println!("{}", USAGE);
process::exit(1);
}