79 lines
1.6 KiB
Rust
79 lines
1.6 KiB
Rust
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 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);
|
|
}
|
|
}
|
|
|