From 55bbd78360cb8aab5a8b96e81e5960c696d3c32d Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Sun, 10 Jan 2016 22:39:00 +0100 Subject: [PATCH] Implement --clear-cache flag --- src/cache.rs | 15 +++++++++++++++ src/main.rs | 11 ++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 05efff2..f783ecc 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -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(()) + } + } diff --git a/src/main.rs b/src/main.rs index 9473064..141d0ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); }