mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-09 09:49:10 +02:00
Use rust-xdg for cache creation (#29)
* Add rust-xdg as a dependency * Use rust-xdg to create the cache directory Closes #10.
This commit is contained in:
parent
baa24c405a
commit
26b8f519c4
7 changed files with 32 additions and 39 deletions
26
Cargo.lock
generated
26
Cargo.lock
generated
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
11
Cargo.toml
11
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"
|
||||
|
|
|
|||
17
src/cache.rs
17
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<S>(url: S, os: OsType) -> Cache where S: Into<String> {
|
||||
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<Vec<String>, 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(())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
use curl::ErrCode;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TldrError {
|
||||
CacheError(String),
|
||||
UpdateError(String),
|
||||
}
|
||||
|
||||
|
||||
impl From<ErrCode> for TldrError {
|
||||
fn from(err: ErrCode) -> TldrError {
|
||||
TldrError::UpdateError(err.to_string())
|
||||
|
|
|
|||
|
|
@ -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("}}"))
|
||||
|
|
|
|||
11
src/main.rs
11
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<String>,
|
||||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ use std::io::BufRead;
|
|||
|
||||
use types::LineType;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Tokenizer<R: BufRead> {
|
||||
reader: R,
|
||||
|
|
@ -12,7 +11,6 @@ pub struct Tokenizer<R: BufRead> {
|
|||
}
|
||||
|
||||
impl<R> Tokenizer<R> where R: BufRead {
|
||||
|
||||
pub fn new(reader: R) -> Tokenizer<R> {
|
||||
Tokenizer {
|
||||
reader: reader,
|
||||
|
|
@ -29,5 +27,4 @@ impl<R> Tokenizer<R> where R: BufRead {
|
|||
Ok(_) => Some(LineType::from(&self.current_line[..])),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue