Warn if cache is too old or missing

This commit is contained in:
Danilo Bargen 2016-01-08 18:55:40 +01:00
commit b2ea2c3842
2 changed files with 20 additions and 4 deletions

View file

@ -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);
}

View file

@ -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<u64> {
pub fn last_update(&self) -> Option<i64> {
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