From 26b8f519c401dea8aeaaf249b78fb0ef454943ae Mon Sep 17 00:00:00 2001 From: Severen Redwood Date: Tue, 19 Apr 2016 04:40:28 +1200 Subject: [PATCH] Use rust-xdg for cache creation (#29) * Add rust-xdg as a dependency * Use rust-xdg to create the cache directory Closes #10. --- Cargo.lock | 26 ++++++++++++++++---------- Cargo.toml | 11 ++++++----- src/cache.rs | 17 ++++++++--------- src/error.rs | 2 -- src/formatter.rs | 1 - src/main.rs | 11 ++--------- src/tokenizer.rs | 3 --- 7 files changed, 32 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 23e1cce..9cf10cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "tealdeer" -version = "0.1.0" +version = "0.2.0" dependencies = [ "ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.63 (registry+https://github.com/rust-lang/crates.io-index)", @@ -14,6 +14,7 @@ dependencies = [ "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "xdg 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -49,7 +50,7 @@ dependencies = [ "curl-sys 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.7.10 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -61,7 +62,7 @@ dependencies = [ "gcc 0.3.27 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "libz-sys 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.7.10 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -116,7 +117,7 @@ dependencies = [ [[package]] name = "kernel32-sys" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -180,7 +181,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "openssl-sys" -version = "0.7.9" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gdi32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -273,7 +274,7 @@ name = "thread-id" version = "2.0.0" 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)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -291,7 +292,7 @@ name = "time" version = "0.1.35" 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)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -322,7 +323,7 @@ name = "unreachable" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "void 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -361,7 +362,7 @@ dependencies = [ [[package]] name = "void" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -369,7 +370,7 @@ name = "walkdir" version = "0.1.5" 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)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -383,3 +384,8 @@ name = "winapi-build" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "xdg" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/Cargo.toml b/Cargo.toml index 82c5f6d..7f2dd94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,17 +17,18 @@ logging = ["env_logger"] dev = ["clippy"] [dependencies] -ansi_term = "^0.7" log = "^0.3" +xdg = "^2.0" tar = "^0.4" -flate2 = "^0.2" curl = "^0.2" -rustc-serialize = "^0.3" -docopt = "^0.6" time = "^0.1" +flate2 = "^0.2" +docopt = "^0.6" walkdir = "^0.1" -env_logger = { version = "^0.3", optional = true } +ansi_term = "^0.7" +rustc-serialize = "^0.3" clippy = {version = "~0.0.37", optional = true} +env_logger = { version = "^0.3", optional = true } [dev-dependencies] tempdir = "^0.3" diff --git a/src/cache.rs b/src/cache.rs index f156f7a..a51de9a 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; #[cfg(unix)] use std::os::unix::fs::MetadataExt; +use xdg::BaseDirectories; use flate2::read::GzDecoder; use tar::Archive; use curl::http; @@ -14,7 +15,6 @@ use time; use error::TldrError::{self, CacheError, UpdateError}; use types::OsType; - #[derive(Debug)] pub struct Cache { url: String, @@ -22,7 +22,6 @@ pub struct Cache { } impl Cache { - pub fn new(url: S, os: OsType) -> Cache where S: Into { Cache { url: url.into(), @@ -36,6 +35,7 @@ impl Cache { // $TLDR_RS_CACHE_DIR env variable. if let Ok(value) = env::var("TLDR_RS_CACHE_DIR") { let path = PathBuf::from(value); + if path.exists() && path.is_dir() { return Ok(path) } else { @@ -46,11 +46,12 @@ impl Cache { } }; - // Otherwise, fall back to ~/.cache/tealdeer - let home_dir = try!(env::home_dir().ok_or( - CacheError("Could not determine home directory".into()) - )); - Ok(home_dir.join(".cache").join("tealdeer")) + // Otherwise, fall back to $XDG_CACHE_HOME/tealdeer. + let xdg_dirs = match BaseDirectories::with_prefix(::NAME) { + Ok(dirs) => dirs, + Err(_) => return Err(CacheError("Could not determine XDG base directory.".into())), + }; + Ok(xdg_dirs.get_cache_home()) } /// Download the archive @@ -162,7 +163,6 @@ impl Cache { /// Return the available pages. pub fn list_pages(&self) -> Result, TldrError> { - // Determine platforms directory and platform let cache_dir = try!(self.get_cache_dir()); let platforms_dir = cache_dir.join("tldr-master").join("pages"); @@ -224,5 +224,4 @@ impl Cache { }; Ok(()) } - } diff --git a/src/error.rs b/src/error.rs index 8643a7a..bb4d555 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,13 +1,11 @@ use curl::ErrCode; - #[derive(Debug)] pub enum TldrError { CacheError(String), UpdateError(String), } - impl From for TldrError { fn from(err: ErrCode) -> TldrError { TldrError::UpdateError(err.to_string()) diff --git a/src/formatter.rs b/src/formatter.rs index ccf8609..56f4891 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -7,7 +7,6 @@ use ansi_term::{Colour, ANSIStrings}; use tokenizer::Tokenizer; use types::LineType; - /// Provide formatting for {{ curly braces }} in ExampleCode lines fn format_braces(text: &str) -> String { let parts = text.split("{{").flat_map(|s| s.split("}}")) diff --git a/src/main.rs b/src/main.rs index c615310..1972599 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,11 +23,12 @@ wrong_self_convention, wrong_pub_self_convention))] #[macro_use] extern crate log; -#[cfg(feature = "logging")]extern crate env_logger; +#[cfg(feature = "logging")] extern crate env_logger; extern crate docopt; extern crate ansi_term; extern crate flate2; extern crate tar; +extern crate xdg; extern crate curl; extern crate rustc_serialize; extern crate time; @@ -53,7 +54,6 @@ use error::TldrError::{UpdateError, CacheError}; use formatter::print_lines; use types::OsType; - const NAME: &'static str = "tealdeer"; const VERSION: &'static str = env!("CARGO_PKG_VERSION"); const USAGE: &'static str = " @@ -89,7 +89,6 @@ To render a local file (for testing): const ARCHIVE_URL: &'static str = "https://github.com/tldr-pages/tldr/archive/master.tar.gz"; const MAX_CACHE_AGE: i64 = 2592000; // 30 days - #[derive(Debug, RustcDecodable)] struct Args { arg_command: Option, @@ -102,7 +101,6 @@ struct Args { flag_clear_cache: bool, } - /// Print page by path fn print_page(path: &Path) -> Result<(), String> { // Open file @@ -118,7 +116,6 @@ fn print_page(path: &Path) -> Result<(), String> { Ok(()) } - /// Check the cache for freshness fn check_cache(args: &Args, cache: &Cache) { if !args.flag_update { @@ -139,7 +136,6 @@ fn check_cache(args: &Args, cache: &Cache) { }; } - #[cfg(feature = "logging")] fn init_log() { env_logger::init().unwrap(); @@ -148,7 +144,6 @@ fn init_log() { #[cfg(not(feature = "logging"))] fn init_log() { } - #[cfg(target_os = "linux")] fn get_os() -> OsType { OsType::Linux } @@ -158,9 +153,7 @@ fn get_os() -> OsType { OsType::OsX } #[cfg(not(any(target_os = "linux", target_os = "macos")))] fn get_os() -> OsType { OsType::Other } - fn main() { - // Initialize logger init_log(); diff --git a/src/tokenizer.rs b/src/tokenizer.rs index fad3d96..7eccb5e 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -4,7 +4,6 @@ use std::io::BufRead; use types::LineType; - #[derive(Debug)] pub struct Tokenizer { reader: R, @@ -12,7 +11,6 @@ pub struct Tokenizer { } impl Tokenizer where R: BufRead { - pub fn new(reader: R) -> Tokenizer { Tokenizer { reader: reader, @@ -29,5 +27,4 @@ impl Tokenizer where R: BufRead { Ok(_) => Some(LineType::from(&self.current_line[..])), } } - }