Rename Updater to Cache

This commit is contained in:
Danilo Bargen 2016-01-08 19:16:45 +01:00
commit 4e951a0247
3 changed files with 21 additions and 16 deletions

View file

@ -10,25 +10,25 @@ use tar::Archive;
use curl::http;
use time;
use error::TldrError::{self, UpdateError};
use error::TldrError::{self, CacheError, UpdateError};
#[derive(Debug)]
pub struct Updater {
pub struct Cache {
url: String,
}
impl Updater {
impl Cache {
pub fn new<S>(url: S) -> Updater where S: Into<String> {
Updater {
pub fn new<S>(url: S) -> Cache where S: Into<String> {
Cache {
url: url.into(),
}
}
/// 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())));
let home_dir = try!(env::home_dir().ok_or(CacheError("Could not determine home directory".into())));
Ok(home_dir.join(".cache").join("tldr-rs"))
}
@ -74,8 +74,8 @@ impl Updater {
Ok(())
}
/// Return the number of seconds since the cache directory was last modified.
#[cfg(unix)]
/// Return the number of seconds since the cache directory was last modified.
pub fn last_update(&self) -> Option<i64> {
if let Ok(cache_dir) = self.get_cache_dir() {
if let Ok(metadata) = fs::metadata(cache_dir) {

View file

@ -3,7 +3,8 @@ use curl::ErrCode;
#[derive(Debug)]
pub enum TldrError {
UpdateError(String)
CacheError(String),
UpdateError(String),
}

View file

@ -19,12 +19,12 @@ use docopt::Docopt;
mod types;
mod tokenizer;
mod formatter;
mod updater;
mod cache;
mod error;
use tokenizer::Tokenizer;
use updater::Updater;
use error::TldrError;
use cache::Cache;
use error::TldrError::{UpdateError, CacheError};
use formatter::print_lines;
@ -112,8 +112,8 @@ fn main() {
process::exit(0);
}
// Initialize updater
let dl = Updater::new(ARCHIVE_URL);
// Initialize cache
let cache = Cache::new(ARCHIVE_URL);
// Clear cache, pass through
if args.flag_clear_cache {
@ -123,9 +123,9 @@ fn main() {
// Update cache, pass through
if args.flag_update {
dl.update().unwrap_or_else(|e| {
cache.update().unwrap_or_else(|e| {
match e {
TldrError::UpdateError(msg) => println!("Could not update cache: {}", msg),
UpdateError(msg) | CacheError(msg) => println!("Could not update cache: {}", msg),
};
process::exit(1);
});
@ -160,8 +160,10 @@ fn main() {
// Show command from cache
if let Some(command) = args.arg_command {
// Check cache
if !args.flag_update {
match dl.last_update() {
match cache.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.");
@ -173,6 +175,8 @@ fn main() {
_ => {},
}
}
// Search for command in cache
println!("Page rendering from cache not yet implemented.");
process::exit(1);
}