From b2ea2c3842c3ac2ef397012a486b92341b8a8fa5 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Fri, 8 Jan 2016 18:55:40 +0100 Subject: [PATCH] Warn if cache is too old or missing --- src/main.rs | 20 ++++++++++++++++++-- src/updater.rs | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index c8a5109..8681466 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,8 @@ To render a local file (for testing): $ tldr --render /path/to/file.md "; -const ARCHIVE_URL: &'static str = "https://github.com/tldr-pages/tldr/archive/master.tar.gz"; +const ARCHIVE_URL: &'static str = "http://localhost:8001/master.tar.gz"; +const MAX_CACHE_AGE: i64 = 2592000; // 30 days #[derive(Debug, RustcDecodable)] @@ -111,6 +112,9 @@ fn main() { process::exit(0); } + // Initialize updater + let dl = Updater::new(ARCHIVE_URL); + // Clear cache, pass through if args.flag_clear_cache { println!("Flag --clear-cache not yet implemented."); @@ -119,7 +123,6 @@ fn main() { // Update cache, pass through if args.flag_update { - let dl = Updater::new(ARCHIVE_URL); dl.update().unwrap_or_else(|e| { match e { TldrError::UpdateError(msg) => println!("Could not update cache: {}", msg), @@ -157,6 +160,19 @@ fn main() { // Show command from cache if let Some(command) = args.arg_command { + if !args.flag_update { + match dl.last_update() { + Some(ago) if ago > MAX_CACHE_AGE => { + println!("Cache wasn't updated in {} days.", MAX_CACHE_AGE / 24 / 3600); + println!("You should probably run `tldr --update` soon."); + }, + None => { + println!("Cache not found. Please run `tldr --update`."); + process::exit(1); + }, + _ => {}, + } + } println!("Page rendering from cache not yet implemented."); process::exit(1); } diff --git a/src/updater.rs b/src/updater.rs index 174d30d..28c5476 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -76,12 +76,12 @@ impl Updater { /// Return the number of seconds since the cache directory was last modified. #[cfg(unix)] - pub fn last_update(&self) -> Option { + pub fn last_update(&self) -> Option { if let Ok(cache_dir) = self.get_cache_dir() { if let Ok(metadata) = fs::metadata(cache_dir) { let mtime = metadata.mtime(); let now = time::now_utc().to_timespec(); - println!("Changed {} seconds ago", now.sec - mtime); + return Some(now.sec - mtime) }; }; None