use cached_path::{Cache, Error, Options, ProgressBar};
use color_eyre::eyre::Result;
use log::debug;
use std::path::PathBuf;
use std::time::Duration;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(
name = "cached-path",
about = "Get the cached path to a resource.",
setting = structopt::clap::AppSettings::ColoredHelp,
)]
struct Opt {
#[structopt()]
resource: String,
#[structopt(long = "dir", env = "RUST_CACHED_PATH_ROOT")]
dir: Option<PathBuf>,
#[structopt(long = "subdir")]
subdir: Option<String>,
#[structopt(long = "extract")]
extract: bool,
#[structopt(long = "timeout")]
timeout: Option<u64>,
#[structopt(long = "connect-timeout")]
connect_timeout: Option<u64>,
#[structopt(long = "max-retries", default_value = "3")]
max_retries: u32,
#[structopt(long = "max-backoff", default_value = "5000")]
max_backoff: u32,
#[structopt(long = "freshness-lifetime")]
freshness_lifetime: Option<u64>,
#[structopt(long = "offline")]
offline: bool,
#[structopt(short = "-q", long = "quietly")]
quietly: bool,
}
fn main() -> Result<()> {
color_eyre::install()?;
env_logger::init();
let opt = Opt::from_args();
debug!("{:?}", opt);
let cache = build_cache_from_opt(&opt)?;
let options = Options::new(opt.subdir.as_deref(), opt.extract);
let path = cache.cached_path_with_options(&opt.resource, &options)?;
println!("{}", path.to_string_lossy());
Ok(())
}
fn build_cache_from_opt(opt: &Opt) -> Result<Cache, Error> {
let mut cache_builder = Cache::builder().offline(opt.offline);
if let Some(dir) = &opt.dir {
cache_builder = cache_builder.dir(dir.clone());
}
if let Some(timeout) = opt.timeout {
cache_builder = cache_builder.timeout(Duration::from_secs(timeout));
}
if let Some(connect_timeout) = opt.connect_timeout {
cache_builder = cache_builder.connect_timeout(Duration::from_secs(connect_timeout));
}
if let Some(freshness_lifetime) = opt.freshness_lifetime {
cache_builder = cache_builder.freshness_lifetime(freshness_lifetime);
}
cache_builder = cache_builder.max_retries(opt.max_retries);
cache_builder = cache_builder.max_backoff(opt.max_backoff);
if !opt.quietly {
cache_builder = cache_builder.progress_bar(Some(ProgressBar::Full));
} else {
cache_builder = cache_builder.progress_bar(None);
}
cache_builder.build()
}