use std::path::PathBuf;
use clap::Parser;
use humantime::Duration;
use tokio::time::sleep;
use crate::error::ParseError;
mod error;
#[derive(Debug, Parser)]
struct CliArgs {
#[clap(short, long, default_value = "10m")]
#[arg(value_parser = parse_refresh_time)]
pub refresh_time: Duration,
#[clap(required = true)]
#[arg(value_parser = parse_directory)]
pub directory: PathBuf,
}
impl Default for CliArgs {
fn default() -> Self {
Self {
refresh_time: parse_refresh_time("10m").unwrap(),
directory: Default::default(),
}
}
}
#[tokio::main]
async fn main() {
loop {
do_sync_task(CliArgs::parse()).await
}
}
async fn do_sync_task(args: CliArgs) {
println!("I am awake! {:?}", args);
sleep(args.refresh_time.into()).await;
}
fn parse_directory(arg: &str) -> Result<PathBuf, ParseError> {
let directory = PathBuf::from(arg);
if directory.exists() && directory.is_dir() {
Ok(PathBuf::from(arg))
} else {
Err(ParseError::new(
"path does not exist or is not a directory.",
))
}
}
fn parse_refresh_time(arg: &str) -> Result<humantime::Duration, ParseError> {
arg.parse::<humantime::Duration>().map_err(ParseError::from)
}