use std::time;
#[allow(rustdoc::broken_intra_doc_links)]
use clap::Parser;
use radicle::git;
use radicle::git::fmt::RefString;
use radicle::prelude::{NodeId, RepoId};
const ABOUT: &str = "Wait for some state to be updated";
const LONG_ABOUT: &str = r#"
Watches a Git reference, and optionally exits when it reaches a target value.
If no target value is passed, exits when the target changes."#;
fn parse_refstr(refstr: &str) -> Result<RefString, git::fmt::Error> {
RefString::try_from(refstr)
}
#[derive(Parser, Debug)]
#[command(about = ABOUT, long_about = LONG_ABOUT,disable_version_flag = true)]
pub struct Args {
#[arg(long)]
pub(super) repo: Option<RepoId>,
#[arg(long, short, alias = "ref", value_name = "REF", value_parser = parse_refstr)]
pub(super) refstr: git::fmt::RefString,
#[arg(long, short, value_name = "OID")]
pub(super) target: Option<git::Oid>,
#[arg(long, short, value_name = "NID")]
pub(super) node: Option<NodeId>,
#[arg(long, short, value_name = "MILLIS", default_value_t = 1000)]
interval: u64,
#[arg(long, value_parser = humantime::parse_duration)]
timeout: Option<std::time::Duration>,
}
impl Args {
pub(super) fn interval(&self) -> time::Duration {
time::Duration::from_millis(self.interval)
}
pub(super) fn timeout(&self) -> time::Duration {
self.timeout
.unwrap_or_else(|| time::Duration::from_millis(u64::MAX))
}
}
#[cfg(test)]
mod test {
use super::Args;
use clap::Parser;
#[test]
fn should_parse_ref_str() {
let args = Args::try_parse_from(["watch", "--ref", "refs/heads/master"]);
assert!(args.is_ok())
}
}