Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/seed: Use clap
Merged did:key:z6MkgFq6...nBGz opened 6 months ago
  • Migrate rad seed to clap
6 files changed +159 -166 b7a7f55e d9ae29de
modified crates/radicle-cli/src/commands/clone/args.rs
@@ -9,6 +9,8 @@ use radicle::identity::IdError;
use radicle::node::policy::Scope;
use radicle::prelude::*;

+
use crate::terminal;
+

pub(crate) const ABOUT: &str = "Clone a Radicle repository";

const LONG_ABOUT: &str = r#"
@@ -44,31 +46,6 @@ impl From<SyncArgs> for SyncSettings {
    }
}

-
#[derive(Clone, Debug)]
-
struct ScopeParser;
-

-
impl clap::builder::TypedValueParser for ScopeParser {
-
    type Value = Scope;
-

-
    fn parse_ref(
-
        &self,
-
        cmd: &clap::Command,
-
        arg: Option<&clap::Arg>,
-
        value: &std::ffi::OsStr,
-
    ) -> Result<Self::Value, clap::Error> {
-
        <Scope as std::str::FromStr>::from_str.parse_ref(cmd, arg, value)
-
    }
-

-
    fn possible_values(
-
        &self,
-
    ) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
-
        use clap::builder::PossibleValue;
-
        Some(Box::new(
-
            [PossibleValue::new("all"), PossibleValue::new("followed")].into_iter(),
-
        ))
-
    }
-
}
-

#[derive(Debug, Parser)]
#[clap(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
pub struct Args {
@@ -83,7 +60,11 @@ pub struct Args {
    pub(super) directory: Option<PathBuf>,

    /// Follow scope
-
    #[arg(long, default_value_t = Scope::All, value_name = "SCOPE", value_parser = ScopeParser)]
+
    #[arg(
+
        long,
+
        default_value_t = Scope::All,
+
        value_parser = terminal::args::ScopeParser
+
    )]
    pub(super) scope: Scope,

    #[clap(flatten)]
modified crates/radicle-cli/src/commands/help.rs
@@ -90,7 +90,10 @@ const COMMANDS: &[CommandItem] = &[
        about: crate::commands::clean::ABOUT,
    },
    CommandItem::Lexopt(crate::commands::rad_self::HELP),
-
    CommandItem::Lexopt(crate::commands::seed::HELP),
+
    CommandItem::Clap {
+
        name: "seed",
+
        about: crate::commands::seed::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::follow::HELP),
    CommandItem::Lexopt(crate::commands::unblock::HELP),
    CommandItem::Clap {
modified crates/radicle-cli/src/commands/seed.rs
@@ -1,10 +1,5 @@
-
use std::collections::BTreeSet;
-
use std::ffi::OsString;
-
use std::time;
+
mod args;

-
use anyhow::anyhow;
-

-
use nonempty::NonEmpty;
use radicle::node::policy;
use radicle::node::policy::{Policy, Scope};
use radicle::node::Handle;
@@ -12,155 +7,34 @@ use radicle::{prelude::*, Node};
use radicle_term::Element as _;

use crate::commands::sync;
-
use crate::node::SyncSettings;
use crate::terminal as term;
-
use crate::terminal::args::{Args, Error, Help};
-

-
pub const HELP: Help = Help {
-
    name: "seed",
-
    description: "Manage repository seeding policies",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad seed [<rid>...] [--[no-]fetch] [--from <nid>] [--scope <scope>] [<option>...]
-

-
    The `seed` command, when no Repository ID (<rid>) is provided, will list the
-
    repositories being seeded.
-

-
    When a Repository ID (<rid>) is provided it updates or creates the seeding policy for
-
    that repository. To delete a seeding policy, use the `rad unseed` command.
-

-
    When seeding a repository, a scope can be specified: this can be either `all` or
-
    `followed`. When using `all`, all remote nodes will be followed for that repository.
-
    On the other hand, with `followed`, only the repository delegates will be followed,
-
    plus any remote that is explicitly followed via `rad follow <nid>`.
-

-
Options
-

-
    --[no-]fetch           Fetch repository after updating seeding policy
-
    --from <nid>           Fetch from the given node (may be specified multiple times)
-
    --timeout <secs>       Fetch timeout in seconds (default: 9)
-
    --scope <scope>        Peer follow scope for this repository
-
    --verbose, -v          Verbose output
-
    --help                 Print help
-
"#,
-
};
-

-
#[derive(Debug)]
-
pub enum Operation {
-
    Seed {
-
        rids: NonEmpty<RepoId>,
-
        fetch: bool,
-
        seeds: BTreeSet<NodeId>,
-
        timeout: time::Duration,
-
        scope: Scope,
-
    },
-
    List,
-
}
-

-
#[derive(Debug)]
-
pub struct Options {
-
    pub op: Operation,
-
    pub verbose: bool,
-
}
-

-
impl Args for Options {
-
    fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
-
        use lexopt::prelude::*;
-

-
        let mut parser = lexopt::Parser::from_args(args);
-
        let mut rids: Vec<RepoId> = Vec::new();
-
        let mut scope: Option<Scope> = None;
-
        let mut fetch: Option<bool> = None;
-
        let mut timeout = time::Duration::from_secs(9);
-
        let mut seeds: BTreeSet<NodeId> = BTreeSet::new();
-
        let mut verbose = false;
-

-
        while let Some(arg) = parser.next()? {
-
            match &arg {
-
                Value(val) => {
-
                    let rid = term::args::rid(val)?;
-
                    rids.push(rid);
-
                }
-
                Long("scope") => {
-
                    let val = parser.value()?;
-
                    scope = Some(term::args::parse_value("scope", val)?);
-
                }
-
                Long("fetch") => {
-
                    fetch = Some(true);
-
                }
-
                Long("no-fetch") => {
-
                    fetch = Some(false);
-
                }
-
                Long("from") => {
-
                    let val = parser.value()?;
-
                    let nid = term::args::nid(&val)?;

-
                    seeds.insert(nid);
-
                }
-
                Long("timeout") | Short('t') => {
-
                    let value = parser.value()?;
-
                    let secs = term::args::parse_value("timeout", value)?;
-

-
                    timeout = time::Duration::from_secs(secs);
-
                }
-
                Long("verbose") | Short('v') => verbose = true,
-
                Long("help") | Short('h') => {
-
                    return Err(Error::Help.into());
-
                }
-
                _ => {
-
                    return Err(anyhow!(arg.unexpected()));
-
                }
-
            }
-
        }
-

-
        let op = match NonEmpty::from_vec(rids) {
-
            Some(rids) => Operation::Seed {
-
                rids,
-
                fetch: fetch.unwrap_or(true),
-
                scope: scope.unwrap_or(Scope::All),
-
                timeout,
-
                seeds,
-
            },
-
            None => Operation::List,
-
        };
-

-
        Ok((Options { op, verbose }, vec![]))
-
    }
-
}
+
pub use args::Args;
+
pub(crate) use args::ABOUT;

-
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
+
pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let mut node = radicle::Node::new(profile.socket());

-
    match options.op {
-
        Operation::Seed {
+
    match args::Operation::from(args) {
+
        args::Operation::List => seeding(&profile)?,
+
        args::Operation::Seed {
            rids,
-
            fetch,
+
            should_fetch,
+
            settings,
            scope,
-
            timeout,
-
            seeds,
        } => {
+
            let settings = settings.with_profile(&profile);
            for rid in rids {
                update(rid, scope, &mut node, &profile)?;

-
                if fetch && node.is_running() {
-
                    if let Err(e) = sync::fetch(
-
                        rid,
-
                        SyncSettings::default()
-
                            .seeds(seeds.clone())
-
                            .timeout(timeout)
-
                            .with_profile(&profile),
-
                        &mut node,
-
                        &profile,
-
                    ) {
+
                if should_fetch && node.is_running() {
+
                    if let Err(e) = sync::fetch(rid, settings.clone(), &mut node, &profile) {
                        term::error(e);
                    }
                }
            }
        }
-
        Operation::List => seeding(&profile)?,
    }

    Ok(())
added crates/radicle-cli/src/commands/seed/args.rs
@@ -0,0 +1,104 @@
+
use std::time;
+

+
use clap::Parser;
+

+
use nonempty::NonEmpty;
+
use radicle::node::policy::Scope;
+
use radicle::prelude::*;
+

+
use crate::node::SyncSettings;
+
use crate::terminal;
+

+
pub(crate) const ABOUT: &str = "Manage repository seeding policies";
+

+
const LONG_ABOUT: &str = r#"
+
The `seed` command, when no Repository ID is provided, will list the
+
repositories being seeded.
+

+
When a Repository ID is provided it updates or creates the seeding policy for
+
that repository. To delete a seeding policy, use the `rad unseed` command.
+

+
When seeding a repository, a scope can be specified: this can be either `all` or
+
`followed`. When using `all`, all remote nodes will be followed for that repository.
+
On the other hand, with `followed`, only the repository delegates will be followed,
+
plus any remote that is explicitly followed via `rad follow <nid>`.
+
"#;
+

+
#[derive(Parser, Debug)]
+
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
+
pub struct Args {
+
    #[arg(value_name = "RID", num_args = 1..)]
+
    pub(super) rids: Option<Vec<RepoId>>,
+

+
    /// Fetch repository after updating seeding policy
+
    #[arg(long, overrides_with("no_fetch"), hide(true))]
+
    fetch: bool,
+

+
    /// Do not fetch repository after updating seeding policy
+
    #[arg(long, overrides_with("fetch"))]
+
    no_fetch: bool,
+

+
    /// Fetch from the given node (may be specified multiple times)
+
    #[arg(long, value_name = "NID", action = clap::ArgAction::Append)]
+
    pub(super) from: Vec<NodeId>,
+

+
    /// Fetch timeout in seconds
+
    #[arg(long, short, value_name = "SECS", default_value_t = 9)]
+
    timeout: u64,
+

+
    /// Peer follow scope for this repository
+
    #[arg(
+
        long,
+
        default_value_t = Scope::All,
+
        value_parser = terminal::args::ScopeParser
+
    )]
+
    pub(super) scope: Scope,
+

+
    /// Verbose output
+
    #[arg(long, short)]
+
    pub(super) verbose: bool,
+
}
+

+
pub(super) enum Operation {
+
    List,
+
    Seed {
+
        rids: NonEmpty<RepoId>,
+
        should_fetch: bool,
+
        settings: SyncSettings,
+
        scope: Scope,
+
    },
+
}
+

+
impl From<Args> for Operation {
+
    fn from(args: Args) -> Self {
+
        let should_fetch = args.should_fetch();
+
        let timeout = args.timeout();
+
        let Args {
+
            rids, from, scope, ..
+
        } = args;
+
        match rids.and_then(NonEmpty::from_vec) {
+
            Some(rids) => Operation::Seed {
+
                rids,
+
                should_fetch,
+
                settings: SyncSettings::default().seeds(from).timeout(timeout),
+
                scope,
+
            },
+
            None => Self::List,
+
        }
+
    }
+
}
+

+
impl Args {
+
    fn timeout(&self) -> time::Duration {
+
        time::Duration::from_secs(self.timeout)
+
    }
+

+
    fn should_fetch(&self) -> bool {
+
        match (self.fetch, self.no_fetch) {
+
            (true, false) => true,
+
            (false, true) => false,
+
            // Default it to fetch
+
            (_, _) => true,
+
        }
+
    }
+
}
modified crates/radicle-cli/src/main.rs
@@ -68,6 +68,7 @@ enum Commands {
    Ls(ls::Args),
    Path(path::Args),
    Publish(publish::Args),
+
    Seed(seed::Args),
    Stats(stats::Args),
    Unfollow(unfollow::Args),
    Unseed(unseed::Args),
@@ -306,7 +307,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
            term::run_command_args::<sync::Options, _>(sync::HELP, sync::run, args.to_vec());
        }
        "seed" => {
-
            term::run_command_args::<seed::Options, _>(seed::HELP, seed::run, args.to_vec());
+
            if let Some(Commands::Seed(args)) = CliArgs::parse().command {
+
                term::run_command_fn(seed::run, args);
+
            }
        }
        "unblock" => {
            term::run_command_args::<unblock::Options, _>(
modified crates/radicle-cli/src/terminal/args.rs
@@ -5,9 +5,12 @@ use std::time;

use anyhow::anyhow;

+
use clap::builder::TypedValueParser;
+

use radicle::cob::{self, issue, patch};
use radicle::crypto;
use radicle::git::{fmt::RefString, Oid};
+
use radicle::node::policy::Scope;
use radicle::node::{Address, Alias};
use radicle::prelude::{Did, NodeId, RepoId};

@@ -203,3 +206,28 @@ pub fn cob(val: &OsString) -> anyhow::Result<cob::ObjectId> {
    let val = val.to_string_lossy();
    cob::ObjectId::from_str(&val).map_err(|_| anyhow!("invalid Object ID '{}'", val))
}
+

+
#[derive(Clone, Debug)]
+
pub(crate) struct ScopeParser;
+

+
impl TypedValueParser for ScopeParser {
+
    type Value = Scope;
+

+
    fn parse_ref(
+
        &self,
+
        cmd: &clap::Command,
+
        arg: Option<&clap::Arg>,
+
        value: &std::ffi::OsStr,
+
    ) -> Result<Self::Value, clap::Error> {
+
        <Scope as std::str::FromStr>::from_str.parse_ref(cmd, arg, value)
+
    }
+

+
    fn possible_values(
+
        &self,
+
    ) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
+
        use clap::builder::PossibleValue;
+
        Some(Box::new(
+
            [PossibleValue::new("all"), PossibleValue::new("followed")].into_iter(),
+
        ))
+
    }
+
}