Unpack tldr archive directly into ~/.cache/tldr-rs

This commit is contained in:
Danilo Bargen 2016-01-06 23:25:44 +01:00
commit bb14466213
4 changed files with 12 additions and 52 deletions

1
Cargo.lock generated
View file

@ -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]]

View file

@ -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"

View file

@ -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

View file

@ -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<R: Read>(&self, archive: &mut Archive<R>, 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<TldrIndex, TldrError> {
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<u64, TldrError> {
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(())
}
}