mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-20 15:14:18 +02:00
Warn if cache is too old or missing
This commit is contained in:
parent
4db20cb6bb
commit
b2ea2c3842
2 changed files with 20 additions and 4 deletions
20
src/main.rs
20
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue