From bb14466213c444026eacd1785f1c12d271ec73de Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Wed, 6 Jan 2016 23:25:44 +0100 Subject: [PATCH] Unpack tldr archive directly into ~/.cache/tldr-rs --- Cargo.lock | 1 - Cargo.toml | 1 - src/main.rs | 5 ++--- src/updater.rs | 57 +++++++++----------------------------------------- 4 files changed, 12 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a660a6b..dba7014 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,6 @@ 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)", - "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 7a80a37..11c12c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ ansi_term = "^0.7" log = "^0.3" tar = { git = "https://github.com/dbrgn/tar-rs", branch = "pax_header" } flate2 = "^0.2" -tempdir = "^0.3" curl = "^0.2" env_logger = { version = "^0.3", optional = true } rustc-serialize = "^0.3" diff --git a/src/main.rs b/src/main.rs index 51e2546..60288a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,6 @@ extern crate docopt; extern crate ansi_term; extern crate flate2; extern crate tar; -extern crate tempdir; extern crate curl; extern crate rustc_serialize; @@ -120,13 +119,13 @@ fn main() { // Update cache, pass through if args.flag_update { let dl = Updater::new(ARCHIVE_URL); - let copied = dl.update().unwrap_or_else(|e| { + dl.update().unwrap_or_else(|e| { match e { TldrError::UpdateError(msg) => println!("Could not update cache: {}", msg), }; process::exit(1); }); - println!("Cached {} tldr pages.", copied); + println!("Successfully updated cache."); } // Render local file and exit diff --git a/src/updater.rs b/src/updater.rs index 1e24e2f..cc60ee6 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -1,12 +1,11 @@ use std::io::Read; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::fs; use std::env; use flate2::read::GzDecoder; use tar::Archive; use curl::http; -use tempdir::TempDir; use rustc_serialize::json; use error::TldrError::{self, UpdateError}; @@ -66,15 +65,6 @@ impl Updater { Ok(Archive::new(decoder)) } - /// Extract archive, return pages and license - fn extract(&self, archive: &mut Archive, path: &Path) -> Result<(PathBuf, PathBuf), TldrError> { - try!(archive.unpack(path).map_err(|e| { - UpdateError(format!("Could not unpack compressed data: {}", e)) - })); - let repodir = path.join("tldr-master"); - Ok((repodir.join("pages"), repodir.join("LICENSE.md"))) - } - /// Given the path to the `pages` directory, return `TldrIndex` instances. fn get_index(&self, path: &Path) -> Result { let mut buffer = String::new(); @@ -118,56 +108,29 @@ impl Updater { } /// Update the pages cache. Return the number of cached pages. - pub fn update(&self) -> Result { + pub fn update(&self) -> Result<(), TldrError> { // First, download the compressed data let response = try!(self.download()); // Decompress the response body into an `Archive` let mut archive = try!(self.decompress(response.get_body())); - // Create temporary directory - let dir = try!(TempDir::new("tldr").map_err(|e| { - UpdateError(format!("Could not create temporary directory: {}", e)) - })); - - // Extract archive and get paths to pages and license - let (pages_src, license_src) = try!(self.extract(&mut archive, dir.path())); - // Determine paths let home_dir = try!(env::home_dir().ok_or(UpdateError("Could not determine home directory".into()))); - let cache_dir = home_dir.join(".tldr").join("cache"); - let pages_dir = &cache_dir.join("pages"); - let license_dst = &cache_dir.join("LICENSE.md"); - let index_dst = &pages_dir.join("index.json"); + let cache_dir = home_dir.join(".cache").join("tldr-rs"); - // Make sure that cache and pages directories exist + // Extract archive + try!(archive.unpack(&cache_dir).map_err(|e| { + UpdateError(format!("Could not unpack compressed data: {}", e)) + })); + + // Make sure that cache directory exists debug!("Ensure cache directory {:?} exists", &cache_dir); try!(fs::create_dir_all(&cache_dir).map_err(|e| { UpdateError(format!("Could not create cache directory: {}", e)) })); - debug!("Ensure pages directory {:?} exists", &pages_dir); - try!(fs::create_dir_all(&pages_dir).map_err(|e| { - UpdateError(format!("Could not create pages directory: {}", e)) - })); - // Copy license file - debug!("Copy license from {:?} to {:?}", &license_src, &license_dst); - try!(fs::copy(&license_src, &license_dst).map_err(|e| { - UpdateError(format!("Could not extract license file: {}", e)) - })); - - // Copy index file - let index_src = &pages_src.join("index.json"); - debug!("Copy index from {:?} to {:?}", &index_src, &index_dst); - try!(fs::copy(&index_src, &index_dst).map_err(|e| { - UpdateError(format!("Could not extract index file: {}", e)) - })); - - // Copy pages - let index = try!(self.get_index(&pages_src)); - let copied = try!(self.copy_pages(&pages_src, &pages_dir, &index)); - - Ok(copied) + Ok(()) } }