Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/unblock: Use clap
Merged did:key:z6MkgFq6...nBGz opened 6 months ago

Since rad block and rad unblock share the same argument type, a new module is introduced that adds a BlockTarget.

The new argument type is then used to migrate rad unblock to clap.

7 files changed +102 -129 cef0ff57 ec1d7543
modified crates/radicle-cli/src/commands/block.rs
@@ -4,7 +4,7 @@ use radicle::node::policy::Policy;

use crate::terminal as term;

-
use args::Target;
+
use term::args::BlockTarget;

pub use args::Args;
pub(crate) use args::ABOUT;
@@ -14,8 +14,8 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    let mut policies = profile.policies_mut()?;

    let updated = match args.target {
-
        Target::Node(nid) => policies.set_follow_policy(&nid, Policy::Block)?,
-
        Target::Repo(rid) => policies.set_seed_policy(&rid, Policy::Block)?,
+
        BlockTarget::Node(nid) => policies.set_follow_policy(&nid, Policy::Block)?,
+
        BlockTarget::Repo(rid) => policies.set_seed_policy(&rid, Policy::Block)?,
    };
    if updated {
        term::success!("Policy for {} set to 'block'", args.target);
modified crates/radicle-cli/src/commands/block/args.rs
@@ -1,44 +1,9 @@
use clap::Parser;
-
use thiserror::Error;

-
use radicle::prelude::{NodeId, RepoId};
+
use crate::terminal::args::BlockTarget;

pub(crate) const ABOUT: &str = "Block repositories or nodes from being seeded or followed";

-
#[derive(Clone, Debug)]
-
pub(super) enum Target {
-
    Node(NodeId),
-
    Repo(RepoId),
-
}
-

-
#[derive(Debug, Error)]
-
#[error("invalid repository or node specified (RID parsing failed with: '{repo}', NID parsing failed with: '{node}'))")]
-
pub(super) struct ParseTargetError {
-
    repo: radicle::identity::IdError,
-
    node: radicle::crypto::PublicKeyError,
-
}
-

-
impl std::str::FromStr for Target {
-
    type Err = ParseTargetError;
-

-
    fn from_str(val: &str) -> Result<Self, Self::Err> {
-
        val.parse::<RepoId>().map(Target::Repo).or_else(|repo| {
-
            val.parse::<NodeId>()
-
                .map(Target::Node)
-
                .map_err(|node| ParseTargetError { repo, node })
-
        })
-
    }
-
}
-

-
impl std::fmt::Display for Target {
-
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-
        match self {
-
            Self::Node(nid) => nid.fmt(f),
-
            Self::Repo(rid) => rid.fmt(f),
-
        }
-
    }
-
}
-

#[derive(Parser, Debug)]
#[command(about = ABOUT, disable_version_flag = true)]
pub struct Args {
@@ -46,7 +11,7 @@ pub struct Args {
    ///
    /// [example values: rad:z3Tr6bC7ctEg2EHmLvknUr29mEDLH, z6MkiswaKJ85vafhffCGBu2gdBsYoDAyHVBWRxL3j297fwS9]
    #[arg(value_name = "RID|NID")]
-
    pub(super) target: Target,
+
    pub(super) target: BlockTarget,
}

#[cfg(test)]
modified crates/radicle-cli/src/commands/help.rs
@@ -92,7 +92,10 @@ const COMMANDS: &[CommandItem] = &[
    CommandItem::Lexopt(crate::commands::rad_self::HELP),
    CommandItem::Lexopt(crate::commands::seed::HELP),
    CommandItem::Lexopt(crate::commands::follow::HELP),
-
    CommandItem::Lexopt(crate::commands::unblock::HELP),
+
    CommandItem::Clap {
+
        name: "unblock",
+
        about: crate::commands::unblock::ABOUT,
+
    },
    CommandItem::Clap {
        name: "unfollow",
        about: crate::commands::unfollow::ABOUT,
modified crates/radicle-cli/src/commands/unblock.rs
@@ -1,98 +1,25 @@
-
use std::ffi::OsString;
-

-
use radicle::prelude::{NodeId, RepoId};
+
mod args;

use crate::terminal as term;
-
use crate::terminal::args;
-
use crate::terminal::args::{Args, Error, Help};
-

-
pub const HELP: Help = Help {
-
    name: "unblock",
-
    description: "Unblock repositories or nodes to allow them to be seeded or followed",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad unblock <rid> [<option>...]
-
    rad unblock <nid> [<option>...]
-

-
    Unblock a repository or remote to allow it to be seeded or followed.
-

-
Options

-
    --help          Print help
-
"#,
-
};
+
use term::args::BlockTarget;

-
enum Target {
-
    Node(NodeId),
-
    Repo(RepoId),
-
}
-

-
impl std::fmt::Display for Target {
-
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-
        match self {
-
            Self::Node(nid) => nid.fmt(f),
-
            Self::Repo(rid) => rid.fmt(f),
-
        }
-
    }
-
}
-

-
pub struct Options {
-
    target: Target,
-
}
-

-
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 target = None;
-

-
        while let Some(arg) = parser.next()? {
-
            match arg {
-
                Long("help") | Short('h') => {
-
                    return Err(Error::Help.into());
-
                }
-
                Value(val) if target.is_none() => {
-
                    if let Ok(rid) = args::rid(&val) {
-
                        target = Some(Target::Repo(rid));
-
                    } else if let Ok(nid) = args::nid(&val) {
-
                        target = Some(Target::Node(nid));
-
                    } else {
-
                        anyhow::bail!(
-
                            "invalid repository or remote specified, see `rad unblock --help`"
-
                        )
-
                    }
-
                }
-
                _ => anyhow::bail!(arg.unexpected()),
-
            }
-
        }
-

-
        Ok((
-
            Options {
-
                target: target.ok_or(anyhow::anyhow!(
-
                    "a repository or remote to unblock must be specified, see `rad unblock --help`"
-
                ))?,
-
            },
-
            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 policies = profile.policies_mut()?;

-
    let updated = match options.target {
-
        Target::Node(nid) => policies.unblock_nid(&nid)?,
-
        Target::Repo(rid) => policies.unblock_rid(&rid)?,
+
    let updated = match args.target {
+
        BlockTarget::Node(nid) => policies.unblock_nid(&nid)?,
+
        BlockTarget::Repo(rid) => policies.unblock_rid(&rid)?,
    };

    if updated {
-
        term::success!("The 'block' policy for {} is removed", options.target);
+
        term::success!("The 'block' policy for {} is removed", args.target);
    } else {
-
        term::info!("No 'block' policy exists for {}", options.target)
+
        term::info!("No 'block' policy exists for {}", args.target)
    }
    Ok(())
}
added crates/radicle-cli/src/commands/unblock/args.rs
@@ -0,0 +1,15 @@
+
use clap::Parser;
+

+
use crate::terminal::args::BlockTarget;
+

+
pub(crate) const ABOUT: &str =
+
    "Unblock repositories or nodes to allow them to be seeded or followed";
+

+
#[derive(Parser, Debug)]
+
#[command(about = ABOUT, disable_version_flag = true)]
+
pub struct Args {
+
    /// A Repository ID or Node ID to allow to be seeded or followed
+
    ///
+
    /// [example values: rad:z3Tr6bC7ctEg2EHmLvknUr29mEDLH, z6MkiswaKJ85vafhffCGBu2gdBsYoDAyHVBWRxL3j297fwS9]
+
    pub(super) target: BlockTarget,
+
}
modified crates/radicle-cli/src/main.rs
@@ -69,6 +69,7 @@ enum Commands {
    Path(path::Args),
    Publish(publish::Args),
    Stats(stats::Args),
+
    Unblock(unblock::Args),
    Unfollow(unfollow::Args),
    Unseed(unseed::Args),
    Watch(watch::Args),
@@ -309,11 +310,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
            term::run_command_args::<seed::Options, _>(seed::HELP, seed::run, args.to_vec());
        }
        "unblock" => {
-
            term::run_command_args::<unblock::Options, _>(
-
                unblock::HELP,
-
                unblock::run,
-
                args.to_vec(),
-
            );
+
            if let Some(Commands::Unblock(args)) = CliArgs::parse().command {
+
                term::run_command_fn(unblock::run, args);
+
            }
        }
        "unfollow" => {
            if let Some(Commands::Unfollow(args)) = CliArgs::parse().command {
modified crates/radicle-cli/src/terminal/args.rs
@@ -5,6 +5,8 @@ use std::time;

use anyhow::anyhow;

+
use thiserror::Error;
+

use radicle::cob::{self, issue, patch};
use radicle::crypto;
use radicle::git::{fmt::RefString, Oid};
@@ -33,6 +35,42 @@ pub enum Error {
    },
}

+
#[derive(Clone, Debug)]
+
pub(crate) enum BlockTarget {
+
    Node(NodeId),
+
    Repo(RepoId),
+
}
+

+
#[derive(Debug, Error)]
+
#[error("invalid repository or node specified (RID parsing failed with: '{repo}', NID parsing failed with: '{node}'))")]
+
pub(crate) struct BlockTargetParseError {
+
    repo: radicle::identity::IdError,
+
    node: radicle::crypto::PublicKeyError,
+
}
+

+
impl std::str::FromStr for BlockTarget {
+
    type Err = BlockTargetParseError;
+

+
    fn from_str(val: &str) -> Result<Self, Self::Err> {
+
        val.parse::<RepoId>()
+
            .map(BlockTarget::Repo)
+
            .or_else(|repo| {
+
                val.parse::<NodeId>()
+
                    .map(BlockTarget::Node)
+
                    .map_err(|node| BlockTargetParseError { repo, node })
+
            })
+
    }
+
}
+

+
impl std::fmt::Display for BlockTarget {
+
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+
        match self {
+
            Self::Node(nid) => nid.fmt(f),
+
            Self::Repo(rid) => rid.fmt(f),
+
        }
+
    }
+
}
+

pub struct Help {
    pub name: &'static str,
    pub description: &'static str,
@@ -203,3 +241,29 @@ 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))
}
+

+
#[cfg(test)]
+
mod test {
+
    use std::str::FromStr;
+

+
    use super::BlockTarget;
+
    use super::BlockTargetParseError;
+

+
    #[test]
+
    fn should_parse_nid() {
+
        let target = BlockTarget::from_str("z6MkiswaKJ85vafhffCGBu2gdBsYoDAyHVBWRxL3j297fwS9");
+
        assert!(target.is_ok())
+
    }
+

+
    #[test]
+
    fn should_parse_rid() {
+
        let target = BlockTarget::from_str("rad:z3Tr6bC7ctEg2EHmLvknUr29mEDLH");
+
        assert!(target.is_ok())
+
    }
+

+
    #[test]
+
    fn should_not_parse() {
+
        let err = BlockTarget::from_str("bee").unwrap_err();
+
        assert!(matches!(err, BlockTargetParseError { .. }));
+
    }
+
}