Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli/seed: Use clap
Erik Kundt committed 6 months ago
commit 894f6a0083f2b198b5cd554041e6734c34e3c207
parent 8be038b956dea2b66eddb4eebd3a237f5e78ad6f
4 files changed +98 -143
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;
@@ -14,153 +9,34 @@ 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 {
-
            rids,
-
            fetch,
-
            scope,
-
            timeout,
-
            seeds,
-
        } => {
+
    let timeout = args.timeout();
+
    let scope = args.scope;
+
    let should_fetch = args.should_fetch();
+

+
    match args.rids {
+
        Some(rids) => {
+
            let settings = SyncSettings::default()
+
                .seeds(args.from)
+
                .timeout(timeout)
+
                .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)?,
+
        None => seeding(&profile)?,
    }

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

+
use clap::Parser;
+

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

+
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,
+
}
+

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

+
    pub(super) 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, _>(