Compare commits

...

2 Commits

Author SHA1 Message Date
Thomas Forgione c44c9fe789
Merge branch 'master' of gitea.tforgione.fr:tforgione/gclone 2019-02-12 10:45:39 +01:00
Thomas Forgione 2ebe6a98be
Cleaning 2019-02-12 10:41:13 +01:00
2 changed files with 59 additions and 27 deletions

View File

@ -6,7 +6,7 @@ use std::path::PathBuf;
use colored::*;
use gclone::{git_dir, Cache};
use gclone::{git_dir, Cache, Result};
macro_rules! unwrap {
($e: expr) => {
@ -55,9 +55,9 @@ fn help() {
}
fn main() {
fn main() -> Result<()> {
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() {
};
if url == "-h" || url == "--help" {
return help();
return Ok(help());
}
let (server, owner, repo) = match parse_url(&url) {
@ -137,7 +137,7 @@ fn main() {
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,4 +150,6 @@ fn main() {
},
}
Ok(())
}

View File

@ -1,48 +1,78 @@
use std::env;
use std::{env, fmt, result};
use std::process::exit;
use std::error::Error;
use std::path::PathBuf;
use std::fs::File;
use std::io::{Write, BufRead, BufReader};
use std::io::{self, Write, BufRead, BufReader};
use colored::*;
use walkdir::WalkDir;
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);
},
macro_rules! impl_from_error {
($type: ty, $variant: path, $from: ty) => {
impl From<$from> for $type {
fn from(e: $from) -> $type {
$variant(e)
}
}
}
}
#[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() -> Cache {
Cache(WalkDir::new(git_dir())
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())
.collect()))
}
pub fn read() -> Cache {
pub fn read() -> Result<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 Cache::new(),
Err(_) => return Ok(Cache(vec![])),
};
let file = BufReader::new(file);
@ -55,11 +85,11 @@ impl Cache {
}
}
Cache(values)
Ok(Cache(values))
}
pub fn write(&self) -> Result<(), Box<Error>> {
let mut path = PathBuf::from(git_dir());
pub fn write(&self) -> Result<()> {
let mut path = PathBuf::from(git_dir()?);
path.push(".cdgcache");
let mut file = File::create(path)?;