Finishes cache management

This commit is contained in:
2019-02-11 20:51:00 +01:00
parent bf7fe8d8bc
commit dca36f057b
4 changed files with 103 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ use std::fs::File;
use std::io::{Write, BufRead, BufReader};
use colored::*;
use walkdir::WalkDir;
pub fn git_dir() -> String {
match env::var("GCLONE_PATH") {
@@ -24,6 +25,16 @@ pub fn git_dir() -> String {
pub struct Cache(Vec<String>);
impl Cache {
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())
}
pub fn read() -> Cache {
let mut path = PathBuf::from(git_dir());
@@ -31,7 +42,7 @@ impl Cache {
let file = match File::open(&path) {
Ok(f) => f,
Err(_) => return Cache(vec![]),
Err(_) => return Cache::new(),
};
let file = BufReader::new(file);

View File

@@ -11,8 +11,6 @@ fn flush_stdout() {
}
fn main() {
let cache = Cache::read();
let request = match env::args().nth(1) {
Some(arg) => arg,
None => {
@@ -24,15 +22,44 @@ fn main() {
},
};
main_with_cache(&request, true);
}
fn main_with_cache(request: &str, regen: bool) {
let cache = Cache::read();
let matches = cache.find(&request);
match matches.len() {
0 => {
eprintln!("{} {}",
"error:".bold().red(),
"no such directory".bold());
exit(1);
if regen {
eprintln!("{} {}",
"warning:".bold().yellow(),
"directory not found, regenerating cache...".bold());
let cache = Cache::new();
match cache.write() {
Ok(_) => (),
Err(e) => {
eprintln!("{} {} {}",
"warning:".bold().yellow(),
"couldn't write cache:".bold(),
e.description());
},
}
return main_with_cache(request, false);
} else {
eprintln!("{} {}",
"error:".bold().red(),
"no such directory".bold());
exit(1);
}
},
1 => {
@@ -41,15 +68,15 @@ fn main() {
len => {
eprintln!("{} {}",
"info:".bold(),
"multiple entries found, please select one");
eprintln!("{}", "info: multiple entries found, please select one".bold());
for (i, j) in matches.iter().enumerate() {
eprintln!("[{}] {}", i + 1, j);
eprintln!("{} {}",
&format!("[{}]", i + 1).bold().blue(),
&format!("{}", j).yellow());
}
eprint!("Enter your choice: ");
eprint!("{}", "Enter your choice: ".bold());
flush_stdout();
let mut string = String::new();
@@ -90,7 +117,6 @@ fn main() {
}
println!("{}", matches[value]);
},
}