radicle_cli/commands/watch/
args.rs1use std::time;
2
3#[allow(rustdoc::broken_intra_doc_links)]
4use clap::Parser;
5
6use radicle::git;
7use radicle::git::fmt::RefString;
8use radicle::prelude::{NodeId, RepoId};
9
10const ABOUT: &str = "Wait for some state to be updated";
11
12const LONG_ABOUT: &str = r#"
13Watches a Git reference, and optionally exits when it reaches a target value.
14If no target value is passed, exits when the target changes."#;
15
16fn parse_refstr(refstr: &str) -> Result<RefString, git::fmt::Error> {
17 RefString::try_from(refstr)
18}
19
20#[derive(Parser, Debug)]
21#[command(about = ABOUT, long_about = LONG_ABOUT,disable_version_flag = true)]
22pub struct Args {
23 #[arg(long)]
25 pub(super) repo: Option<RepoId>,
26
27 #[arg(long, short, alias = "ref", value_name = "REF", value_parser = parse_refstr)]
31 pub(super) refstr: git::fmt::RefString,
32
33 #[arg(long, short, value_name = "OID")]
35 pub(super) target: Option<git::Oid>,
36
37 #[arg(long, short, value_name = "NID")]
39 pub(super) node: Option<NodeId>,
40
41 #[arg(long, short, value_name = "MILLIS", default_value_t = 1000)]
43 interval: u64,
44
45 #[arg(long, value_name = "MILLIS")]
47 timeout: Option<u64>,
48}
49
50impl Args {
51 pub(super) fn interval(&self) -> time::Duration {
53 time::Duration::from_millis(self.interval)
54 }
55
56 pub(super) fn timeout(&self) -> time::Duration {
58 time::Duration::from_millis(self.timeout.unwrap_or(u64::MAX))
59 }
60}
61
62#[cfg(test)]
63mod test {
64 use super::Args;
65 use clap::Parser;
66
67 #[test]
68 fn should_parse_ref_str() {
69 let args = Args::try_parse_from(["watch", "--ref", "refs/heads/master"]);
70 assert!(args.is_ok())
71 }
72}