Working on cache

This commit is contained in:
Thomas Forgione 2019-02-11 17:56:43 +01:00
parent a98638b947
commit aa736dc2a8
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
4 changed files with 96 additions and 11 deletions

View File

@ -6,3 +6,11 @@ edition = "2018"
[dependencies]
colored = "1.7.0"
[[bin]]
name = "gclone"
path = "src/gclone.rs"
[[bin]]
name = "cdg"
path = "src/cdg.rs"

5
src/cdg.rs Normal file
View File

@ -0,0 +1,5 @@
use gclone::Cache;
fn main() {
let cache = Cache::read();
}

View File

@ -6,6 +6,8 @@ use std::path::PathBuf;
use colored::*;
use gclone::{git_dir, Cache};
macro_rules! unwrap {
($e: expr) => {
match $e {
@ -55,17 +57,7 @@ fn help() {
fn main() {
let git_dir = 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);
},
};
let git_dir = git_dir();
// Parse args
let url = match env::args().nth(1) {
@ -144,4 +136,18 @@ fn main() {
eprintln!("{} {}", "info:".bold(), "done!");
// Append to cache
let mut cache = Cache::read();
cache.append(path.display().to_string());
match cache.write() {
Ok(_) => (),
Err(e) => {
eprintln!("{} {} {}",
"warning:".bold().yellow(),
"couldn't write cache:".bold(),
e.description());
},
}
}

66
src/lib.rs Normal file
View File

@ -0,0 +1,66 @@
use std::env;
use std::process::exit;
use std::error::Error;
use std::path::PathBuf;
use std::fs::File;
use std::io::{Write, BufRead, BufReader};
use colored::*;
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);
},
}
}
pub struct Cache(Vec<String>);
impl Cache {
pub fn read() -> Cache {
let mut path = PathBuf::from(git_dir());
path.push(".cdgcache");
let file = match File::open(&path) {
Ok(f) => f,
Err(_) => return Cache(vec![]),
};
let file = BufReader::new(file);
let mut values = vec![];
for line in file.lines() {
if let Ok(l) = line {
values.push(l);
}
}
Cache(values)
}
pub fn write(&self) -> Result<(), Box<Error>> {
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 append(&mut self, value: String) {
self.0.push(value);
}
}