mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-23 08:34:18 +02:00
commit
2e2d015d46
4 changed files with 55 additions and 4 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
|
@ -10,6 +10,7 @@ dependencies = [
|
|||
"log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tar 0.3.2 (git+https://github.com/dbrgn/tar-rs?branch=pax_header)",
|
||||
"time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -253,6 +254,16 @@ dependencies = [
|
|||
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "0.2.38"
|
||||
|
|
|
|||
|
|
@ -15,3 +15,4 @@ curl = "^0.2"
|
|||
env_logger = { version = "^0.3", optional = true }
|
||||
rustc-serialize = "^0.3"
|
||||
docopt = "^0.6"
|
||||
time = "^0.1"
|
||||
|
|
|
|||
21
src/main.rs
21
src/main.rs
|
|
@ -8,6 +8,7 @@ extern crate flate2;
|
|||
extern crate tar;
|
||||
extern crate curl;
|
||||
extern crate rustc_serialize;
|
||||
extern crate time;
|
||||
|
||||
use std::io::BufReader;
|
||||
use std::fs::File;
|
||||
|
|
@ -59,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)]
|
||||
|
|
@ -110,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.");
|
||||
|
|
@ -118,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),
|
||||
|
|
@ -156,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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
use std::io::Read;
|
||||
use std::fs;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(unix)] use std::os::unix::fs::MetadataExt;
|
||||
|
||||
use flate2::read::GzDecoder;
|
||||
use tar::Archive;
|
||||
use curl::http;
|
||||
use time;
|
||||
|
||||
use error::TldrError::{self, UpdateError};
|
||||
|
||||
|
|
@ -22,6 +26,12 @@ impl Updater {
|
|||
}
|
||||
}
|
||||
|
||||
/// Return the path to the cache directory.
|
||||
fn get_cache_dir(&self) -> Result<PathBuf, TldrError> {
|
||||
let home_dir = try!(env::home_dir().ok_or(UpdateError("Could not determine home directory".into())));
|
||||
Ok(home_dir.join(".cache").join("tldr-rs"))
|
||||
}
|
||||
|
||||
/// Download the archive
|
||||
fn download(&self) -> Result<http::Response, TldrError> {
|
||||
let resp = try!(
|
||||
|
|
@ -48,8 +58,7 @@ impl Updater {
|
|||
let mut archive = try!(self.decompress(response.get_body()));
|
||||
|
||||
// Determine paths
|
||||
let home_dir = try!(env::home_dir().ok_or(UpdateError("Could not determine home directory".into())));
|
||||
let cache_dir = home_dir.join(".cache").join("tldr-rs");
|
||||
let cache_dir = try!(self.get_cache_dir());
|
||||
|
||||
// Extract archive
|
||||
try!(archive.unpack(&cache_dir).map_err(|e| {
|
||||
|
|
@ -65,4 +74,17 @@ impl Updater {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Return the number of seconds since the cache directory was last modified.
|
||||
#[cfg(unix)]
|
||||
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();
|
||||
return Some(now.sec - mtime)
|
||||
};
|
||||
};
|
||||
None
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue