From 56e39a33f991773d1e7d3503e350a0ef8ce303c4 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 12 Feb 2019 11:04:04 +0100 Subject: [PATCH] Cleaning --- Cargo.lock | 1 + Cargo.toml | 1 + src/cache.rs | 74 +++++++++++++++++++++++++++++++++ src/gclone.rs | 6 +-- src/lib.rs | 111 +++++++++++++------------------------------------- src/pgd.rs | 7 +++- 6 files changed, 111 insertions(+), 89 deletions(-) create mode 100644 src/cache.rs diff --git a/Cargo.lock b/Cargo.lock index 7604f86..52b1696 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,7 @@ name = "gclone" version = "0.1.0" dependencies = [ "colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index d7c04fa..cd43922 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] colored = "1.7.0" walkdir = "2.2.7" +lazy_static = "1.2.0" [[bin]] name = "gclone" diff --git a/src/cache.rs b/src/cache.rs new file mode 100644 index 0000000..fe29cd4 --- /dev/null +++ b/src/cache.rs @@ -0,0 +1,74 @@ +use std::path::PathBuf; +use std::fs::File; +use std::io::{Write, BufRead, BufReader}; +use walkdir::WalkDir; +use crate::{Result, GIT_DIR}; + +pub struct Cache(Vec); + +impl Cache { + + pub fn generate() -> Cache { + Cache(WalkDir::new(&*GIT_DIR) + .max_depth(3) + .into_iter() + .filter_map(|x| x.ok()) + .map(|x| x.path().display().to_string()) + .collect()) + } + + pub fn read() -> Result { + + let mut path = PathBuf::from(&*GIT_DIR); + path.push(".cdgcache"); + + let file = File::open(&path)?; + let file = BufReader::new(file); + + let mut values = vec![]; + + for line in file.lines() { + if let Ok(l) = line { + values.push(l); + } + } + + Ok(Cache(values)) + } + + pub fn read_or_generate() -> Cache { + match Cache::read() { + Ok(c) => c, + Err(_) => Cache::generate(), + } + } + + pub fn write(&self) -> Result<()> { + let mut path = PathBuf::from(&*GIT_DIR); + path.push(".cdgcache"); + let mut file = File::create(path)?; + + for line in &self.0 { + writeln!(file, "{}", line)?; + } + + Ok(()) + } + + pub fn find(&self, dirname: &str) -> Vec { + let dirname = &format!("/{}", dirname); + let mut matches = vec![]; + for line in &self.0 { + if line.ends_with(dirname) { + matches.push(line.clone()); + } + } + + matches + } + + pub fn append(&mut self, value: String) { + self.0.push(value); + } +} + diff --git a/src/gclone.rs b/src/gclone.rs index f4f4eda..6dcf102 100644 --- a/src/gclone.rs +++ b/src/gclone.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use colored::*; -use gclone::{git_dir, Cache, Result}; +use gclone::{GIT_DIR, Cache, Result}; macro_rules! unwrap { ($e: expr) => { @@ -57,8 +57,6 @@ fn help() { fn main() -> Result<()> { - let git_dir = git_dir()?; - // Parse args let url = match env::args().nth(1) { Some(arg) => arg, @@ -87,7 +85,7 @@ fn main() -> Result<()> { }; // Build path - let mut path = PathBuf::from(git_dir); + let mut path = PathBuf::from(&*GIT_DIR); path.push(&server); path.push(&owner); path.push(&repo); diff --git a/src/lib.rs b/src/lib.rs index 62f3a84..fee9afa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,25 @@ -use std::{env, fmt, result}; -use std::error::Error; -use std::path::PathBuf; -use std::fs::File; -use std::io::{self, Write, BufRead, BufReader}; +use std::{env, fmt, error, result}; +use std::process::exit; +use std::io; use colored::*; -use walkdir::WalkDir; +use lazy_static::lazy_static; + +mod cache; +pub use cache::Cache; + +lazy_static! { + pub static ref GIT_DIR: String = { + match env::var("GCLONE_PATH") { + Ok(d) => d, + Err(e) => { + let e: Error = e.into(); + eprintln!("{}", e); + exit(1); + }, + } + }; +} macro_rules! impl_from_error { ($type: ty, $variant: path, $from: ty) => { @@ -18,23 +32,23 @@ macro_rules! impl_from_error { } #[derive(Debug)] -pub enum MError { +pub enum Error { NoGclonePath(env::VarError), IoError(io::Error), } -impl_from_error!(MError, MError::NoGclonePath, env::VarError); -impl_from_error!(MError, MError::IoError, io::Error); +impl_from_error!(Error, Error::NoGclonePath, env::VarError); +impl_from_error!(Error, Error::IoError, io::Error); -impl fmt::Display for MError { +impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match self { - MError::NoGclonePath(e) => + Error::NoGclonePath(e) => write!(fmt, "{} {} {}", "error:".bold().red(), "couldn't read environment variable GCLONE_PATH".bold(), e), - MError::IoError(e) => + Error::IoError(e) => write!(fmt, "{} {} {}", "error:".bold().red(), "couldn't read or write cache:".bold(), @@ -43,76 +57,7 @@ impl fmt::Display for MError { } } -impl Error for MError {} +impl error::Error for Error {} -pub type Result = result::Result; - -pub fn git_dir() -> Result { - Ok(env::var("GCLONE_PATH")?) -} - -pub struct Cache(Vec); - -impl Cache { - - pub fn new() -> Result { - Ok(Cache(WalkDir::new(git_dir()?) - .max_depth(3) - .into_iter() - .filter_map(|x| x.ok()) - .map(|x| x.path().display().to_string()) - .collect())) - } - - pub fn read() -> Result { - - let mut path = PathBuf::from(git_dir()?); - path.push(".cdgcache"); - - let file = match File::open(&path) { - Ok(f) => f, - Err(_) => return Ok(Cache(vec![])), - }; - - let file = BufReader::new(file); - - let mut values = vec![]; - - for line in file.lines() { - if let Ok(l) = line { - values.push(l); - } - } - - Ok(Cache(values)) - } - - pub fn write(&self) -> Result<()> { - let mut path = PathBuf::from(git_dir()?); - path.push(".cdgcache"); - let mut file = File::create(path)?; - - for line in &self.0 { - writeln!(file, "{}", line)?; - } - - Ok(()) - } - - pub fn find(&self, dirname: &str) -> Vec { - let dirname = &format!("/{}", dirname); - let mut matches = vec![]; - for line in &self.0 { - if line.ends_with(dirname) { - matches.push(line.clone()); - } - } - - matches - } - - pub fn append(&mut self, value: String) { - self.0.push(value); - } -} +pub type Result = result::Result; diff --git a/src/pgd.rs b/src/pgd.rs index 2a04302..909c5fb 100644 --- a/src/pgd.rs +++ b/src/pgd.rs @@ -27,7 +27,10 @@ fn main() -> Result<()> { fn main_with_cache(request: &str, regen: bool) -> Result<()> { - let cache = Cache::read()?; + let (cache, regen) = match Cache::read() { + Ok(c) => (c, regen), + Err(_) => (Cache::generate(), false), + }; let matches = cache.find(&request); @@ -39,7 +42,7 @@ fn main_with_cache(request: &str, regen: bool) -> Result<()> { "warning:".bold().yellow(), "directory not found, regenerating cache...".bold()); - let cache = Cache::new()?; + let cache = Cache::generate(); match cache.write() { Ok(_) => (),