This commit is contained in:
Thomas Forgione 2019-02-12 11:04:04 +01:00
parent d7e8b57c67
commit 56e39a33f9
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
6 changed files with 111 additions and 89 deletions

1
Cargo.lock generated
View File

@ -11,6 +11,7 @@ name = "gclone"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View File

@ -7,6 +7,7 @@ edition = "2018"
[dependencies] [dependencies]
colored = "1.7.0" colored = "1.7.0"
walkdir = "2.2.7" walkdir = "2.2.7"
lazy_static = "1.2.0"
[[bin]] [[bin]]
name = "gclone" name = "gclone"

74
src/cache.rs Normal file
View File

@ -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<String>);
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<Cache> {
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<String> {
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);
}
}

View File

@ -6,7 +6,7 @@ use std::path::PathBuf;
use colored::*; use colored::*;
use gclone::{git_dir, Cache, Result}; use gclone::{GIT_DIR, Cache, Result};
macro_rules! unwrap { macro_rules! unwrap {
($e: expr) => { ($e: expr) => {
@ -57,8 +57,6 @@ fn help() {
fn main() -> Result<()> { fn main() -> Result<()> {
let git_dir = git_dir()?;
// Parse args // Parse args
let url = match env::args().nth(1) { let url = match env::args().nth(1) {
Some(arg) => arg, Some(arg) => arg,
@ -87,7 +85,7 @@ fn main() -> Result<()> {
}; };
// Build path // Build path
let mut path = PathBuf::from(git_dir); let mut path = PathBuf::from(&*GIT_DIR);
path.push(&server); path.push(&server);
path.push(&owner); path.push(&owner);
path.push(&repo); path.push(&repo);

View File

@ -1,11 +1,25 @@
use std::{env, fmt, result}; use std::{env, fmt, error, result};
use std::error::Error; use std::process::exit;
use std::path::PathBuf; use std::io;
use std::fs::File;
use std::io::{self, Write, BufRead, BufReader};
use colored::*; 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 { macro_rules! impl_from_error {
($type: ty, $variant: path, $from: ty) => { ($type: ty, $variant: path, $from: ty) => {
@ -18,23 +32,23 @@ macro_rules! impl_from_error {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum MError { pub enum Error {
NoGclonePath(env::VarError), NoGclonePath(env::VarError),
IoError(io::Error), IoError(io::Error),
} }
impl_from_error!(MError, MError::NoGclonePath, env::VarError); impl_from_error!(Error, Error::NoGclonePath, env::VarError);
impl_from_error!(MError, MError::IoError, io::Error); 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 { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
MError::NoGclonePath(e) => Error::NoGclonePath(e) =>
write!(fmt, "{} {} {}", write!(fmt, "{} {} {}",
"error:".bold().red(), "error:".bold().red(),
"couldn't read environment variable GCLONE_PATH".bold(), "couldn't read environment variable GCLONE_PATH".bold(),
e), e),
MError::IoError(e) => Error::IoError(e) =>
write!(fmt, "{} {} {}", write!(fmt, "{} {} {}",
"error:".bold().red(), "error:".bold().red(),
"couldn't read or write cache:".bold(), "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<T> = result::Result<T, MError>; pub type Result<T> = result::Result<T, Error>;
pub fn git_dir() -> Result<String> {
Ok(env::var("GCLONE_PATH")?)
}
pub struct Cache(Vec<String>);
impl Cache {
pub fn new() -> Result<Cache> {
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<Cache> {
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<String> {
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);
}
}

View File

@ -27,7 +27,10 @@ fn main() -> Result<()> {
fn main_with_cache(request: &str, regen: bool) -> 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); let matches = cache.find(&request);
@ -39,7 +42,7 @@ fn main_with_cache(request: &str, regen: bool) -> Result<()> {
"warning:".bold().yellow(), "warning:".bold().yellow(),
"directory not found, regenerating cache...".bold()); "directory not found, regenerating cache...".bold());
let cache = Cache::new()?; let cache = Cache::generate();
match cache.write() { match cache.write() {
Ok(_) => (), Ok(_) => (),