gclone/src/pgd.rs

47 lines
1.1 KiB
Rust
Raw Normal View History

2019-02-27 10:57:01 +01:00
#[macro_use]
extern crate log;
2019-02-21 15:21:27 +01:00
2019-02-27 10:57:01 +01:00
use std::process::exit;
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() {
2019-02-27 10:57:01 +01:00
beautylog::init(log::LevelFilter::Info).ok();
2019-02-12 14:51:45 +01:00
if let Err(e) = main_result() {
2019-02-27 10:57:01 +01:00
error!("{}", e);
2019-02-12 14:51:45 +01:00
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 => {
2019-02-27 10:57:01 +01:00
warn!("directory not found, regenerating cache...");
2019-02-21 15:21:27 +01:00
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
}