Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli/block: Use clap
✗ CI failure Lorenz Leutgeb committed 7 months ago
commit d77e9d30da6e5d859275ae0625b324246dcd4765
parent c7bff28452b9b07c65576634ec3c223ea08e0126
1 failed (1 total) View logs
3 files changed +41 -65
modified crates/radicle-cli/src/commands/block.rs
@@ -1,33 +1,36 @@
-
use std::ffi::OsString;
+
use clap::Parser;
+
use thiserror::Error;

use radicle::node::policy::Policy;
use radicle::prelude::{NodeId, RepoId};

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

-
pub const HELP: Help = Help {
-
    name: "block",
-
    description: "Block repositories or nodes from being seeded or followed",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
+
pub const ABOUT: &str = "Block repositories or nodes from being seeded or followed";

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

-
    Blocks a repository from being seeded or a node from being followed.
+
#[derive(Clone, Debug)]
+
pub enum Target {
+
    Node(NodeId),
+
    Repo(RepoId),
+
}

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

-
    --help          Print help
-
"#,
-
};
+
impl std::str::FromStr for Target {
+
    type Err = ParseTargetError;

-
enum Target {
-
    Node(NodeId),
-
    Repo(RepoId),
+
    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 {
@@ -39,58 +42,24 @@ impl std::fmt::Display for Target {
    }
}

-
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 node specified, see `rad block --help`"
-
                        )
-
                    }
-
                }
-
                _ => anyhow::bail!(arg.unexpected()),
-
            }
-
        }
-

-
        Ok((
-
            Options {
-
                target: target.ok_or(anyhow::anyhow!(
-
                    "a repository or node to block must be specified, see `rad block --help`"
-
                ))?,
-
            },
-
            vec![],
-
        ))
-
    }
+
#[derive(Parser, Debug)]
+
#[command(disable_version_flag = true)]
+
pub struct Args {
+
    /// A Repository ID or Node ID to block from seeding or following (respectively)
+
    #[arg(value_name = "RID|NID")]
+
    pub target: Target,
}

-
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 {
+
    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)?,
    };
    if updated {
-
        term::success!("Policy for {} set to 'block'", options.target);
+
        term::success!("Policy for {} set to 'block'", args.target);
    }
    Ok(())
}
modified crates/radicle-cli/src/commands/help.rs
@@ -38,7 +38,10 @@ impl CommandItem {

const COMMANDS: &[CommandItem] = &[
    CommandItem::Lexopt(crate::commands::auth::HELP),
-
    CommandItem::Lexopt(crate::commands::block::HELP),
+
    CommandItem::Clap {
+
        name: "block",
+
        about: crate::commands::block::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::checkout::HELP),
    CommandItem::Lexopt(crate::commands::clone::HELP),
    CommandItem::Lexopt(crate::commands::config::HELP),
modified crates/radicle-cli/src/main.rs
@@ -45,6 +45,8 @@ struct CliArgs {

#[derive(Subcommand, Debug)]
enum Commands {
+
    #[command(about = block::ABOUT)]
+
    Block(block::Args),
    #[command(about = radicle_cli::commands::issue::ABOUT)]
    Issue(issue::Args),
}
@@ -174,7 +176,9 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
            term::run_command_args::<auth::Options, _>(auth::HELP, auth::run, args.to_vec());
        }
        "block" => {
-
            term::run_command_args::<block::Options, _>(block::HELP, block::run, args.to_vec());
+
            if let Some(Commands::Block(args)) = CliArgs::parse().command {
+
                term::run_command_fn(block::run, args);
+
            }
        }
        "checkout" => {
            term::run_command_args::<checkout::Options, _>(