cli/unblock: Use clap

This commit is contained in:
Erik Kundt 2025-10-14 12:41:56 +02:00 committed by Fintan Halpenny
parent faf19af9de
commit ec1d754308
4 changed files with 34 additions and 89 deletions

View File

@ -95,7 +95,10 @@ const COMMANDS: &[CommandItem] = &[
about: crate::commands::seed::ABOUT,
},
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,

View File

@ -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
use term::args::BlockTarget;
rad unblock <rid> [<option>...]
rad unblock <nid> [<option>...]
pub use args::Args;
pub(crate) use args::ABOUT;
Unblock a repository or remote to allow it to be seeded or followed.
Options
--help Print help
"#,
};
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 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(())
}

View File

@ -0,0 +1,16 @@
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]
#[arg(value_name = "RID|NID")]
pub(super) target: BlockTarget,
}

View File

@ -70,6 +70,7 @@ enum Commands {
Publish(publish::Args),
Seed(seed::Args),
Stats(stats::Args),
Unblock(unblock::Args),
Unfollow(unfollow::Args),
Unseed(unseed::Args),
Watch(watch::Args),
@ -312,11 +313,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
}
}
"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 {