Compare commits

..

No commits in common. "c44c9fe789efcc7a34dde0834f9251aa7d414c56" and "dca36f057b63baa5cc3a7a839a48b8f7edc8430f" have entirely different histories.

2 changed files with 27 additions and 59 deletions

View File

@ -6,7 +6,7 @@ use std::path::PathBuf;
use colored::*;
use gclone::{git_dir, Cache, Result};
use gclone::{git_dir, Cache};
macro_rules! unwrap {
($e: expr) => {
@ -55,9 +55,9 @@ fn help() {
}
fn main() -> Result<()> {
fn main() {
let git_dir = git_dir()?;
let git_dir = git_dir();
// Parse args
let url = match env::args().nth(1) {
@ -72,7 +72,7 @@ fn main() -> Result<()> {
};
if url == "-h" || url == "--help" {
return Ok(help());
return help();
}
let (server, owner, repo) = match parse_url(&url) {
@ -137,7 +137,7 @@ fn main() -> Result<()> {
eprintln!("{} {}", "info:".bold(), "done!");
// Append to cache
let mut cache = Cache::read()?;
let mut cache = Cache::read();
cache.append(path.display().to_string());
match cache.write() {
@ -150,6 +150,4 @@ fn main() -> Result<()> {
},
}
Ok(())
}

View File

@ -1,78 +1,48 @@
use std::{env, fmt, result};
use std::env;
use std::process::exit;
use std::error::Error;
use std::path::PathBuf;
use std::fs::File;
use std::io::{self, Write, BufRead, BufReader};
use std::io::{Write, BufRead, BufReader};
use colored::*;
use walkdir::WalkDir;
macro_rules! impl_from_error {
($type: ty, $variant: path, $from: ty) => {
impl From<$from> for $type {
fn from(e: $from) -> $type {
$variant(e)
}
}
pub fn git_dir() -> String {
match env::var("GCLONE_PATH") {
Ok(path) => path,
Err(e) => {
eprintln!("{} {} {}",
"error:".bold().red(),
"environment variable GCLONE_PATH must be set:".bold(),
e.description());
exit(1);
},
}
}
#[derive(Debug)]
pub enum MError {
NoGclonePath(env::VarError),
IoError(io::Error),
}
impl_from_error!(MError, MError::NoGclonePath, env::VarError);
impl_from_error!(MError, MError::IoError, io::Error);
impl fmt::Display for MError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
MError::NoGclonePath(e) =>
write!(fmt, "{} {} {}",
"error:".bold().red(),
"couldn't read environment variable GCLONE_PATH".bold(),
e),
MError::IoError(e) =>
write!(fmt, "{} {} {}",
"error:".bold().red(),
"couldn't read or write cache:".bold(),
e),
}
}
}
impl Error for MError {}
pub type Result<T> = result::Result<T, MError>;
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()?)
pub fn new() -> Cache {
Cache(WalkDir::new(git_dir())
.max_depth(3)
.into_iter()
.filter_map(|x| x.ok())
.map(|x| x.path().display().to_string())
.collect()))
.collect())
}
pub fn read() -> Result<Cache> {
pub fn read() -> Cache {
let mut path = PathBuf::from(git_dir()?);
let mut path = PathBuf::from(git_dir());
path.push(".cdgcache");
let file = match File::open(&path) {
Ok(f) => f,
Err(_) => return Ok(Cache(vec![])),
Err(_) => return Cache::new(),
};
let file = BufReader::new(file);
@ -85,11 +55,11 @@ impl Cache {
}
}
Ok(Cache(values))
Cache(values)
}
pub fn write(&self) -> Result<()> {
let mut path = PathBuf::from(git_dir()?);
pub fn write(&self) -> Result<(), Box<Error>> {
let mut path = PathBuf::from(git_dir());
path.push(".cdgcache");
let mut file = File::create(path)?;