gclone/src/pgd.rs

49 lines
1.1 KiB
Rust
Raw Normal View History

2019-02-11 20:27:21 +01:00
use std::process::exit;
2019-02-21 15:21:27 +01:00
2019-02-11 20:27:21 +01:00
use colored::*;
2019-02-21 15:21:27 +01:00
2019-02-12 14:51:45 +01:00
use gclone::{first_arg, Cache, Error, Result};
2019-02-11 17:56:43 +01:00
2019-02-12 14:51:45 +01:00
fn main() {
if let Err(e) = main_result() {
eprintln!("{}", e);
exit(1);
}
2019-02-11 20:27:21 +01:00
}
2019-02-12 14:51:45 +01:00
fn main_result() -> Result<()> {
let request = first_arg()?;
2019-02-12 10:47:14 +01:00
main_with_cache(&request, true)
2019-02-11 20:51:00 +01:00
}
2019-02-12 10:47:14 +01:00
fn main_with_cache(request: &str, regen: bool) -> Result<()> {
2019-02-12 14:51:45 +01:00
let (cache, just_generated) = match Cache::read() {
Ok(c) => (c, false),
Err(_) => {
let cache = Cache::generate();
cache.write()?;
(cache, true)
2019-02-21 15:21:27 +01:00
}
2019-02-12 11:04:04 +01:00
};
2019-02-11 20:51:00 +01:00
2019-02-12 14:51:45 +01:00
let only_match = match cache.find_interactive(&request) {
2019-02-21 15:21:27 +01:00
Err(Error::NoSuchDirectory) if regen && !just_generated => {
eprintln!(
"{} {}",
"warning:".bold().yellow(),
"directory not found, regenerating cache...".bold()
);
let cache = Cache::generate();
cache.write()?;
return main_with_cache(request, false);
}
2019-02-11 20:27:21 +01:00
2019-02-12 14:51:45 +01:00
Err(e) => return Err(e),
Ok(x) => x,
};
2019-02-11 20:27:21 +01:00
2019-02-12 14:51:45 +01:00
println!("{}", only_match);
2019-02-11 20:27:21 +01:00
2019-02-12 10:47:14 +01:00
Ok(())
2019-02-11 17:56:43 +01:00
}