cli/unfollow: Use clap
This commit is contained in:
parent
6fb1ebec45
commit
ee49e28766
|
|
@ -67,7 +67,10 @@ const COMMANDS: &[CommandItem] = &[
|
||||||
CommandItem::Lexopt(crate::commands::seed::HELP),
|
CommandItem::Lexopt(crate::commands::seed::HELP),
|
||||||
CommandItem::Lexopt(crate::commands::follow::HELP),
|
CommandItem::Lexopt(crate::commands::follow::HELP),
|
||||||
CommandItem::Lexopt(crate::commands::unblock::HELP),
|
CommandItem::Lexopt(crate::commands::unblock::HELP),
|
||||||
CommandItem::Lexopt(crate::commands::unfollow::HELP),
|
CommandItem::Clap {
|
||||||
|
name: "unfollow",
|
||||||
|
about: crate::commands::unfollow::ABOUT,
|
||||||
|
},
|
||||||
CommandItem::Clap {
|
CommandItem::Clap {
|
||||||
name: "unseed",
|
name: "unseed",
|
||||||
about: crate::commands::unseed::ABOUT,
|
about: crate::commands::unseed::ABOUT,
|
||||||
|
|
|
||||||
|
|
@ -1,77 +1,13 @@
|
||||||
use std::ffi::OsString;
|
mod args;
|
||||||
|
|
||||||
use anyhow::anyhow;
|
use radicle::node::Handle;
|
||||||
|
|
||||||
use radicle::node::{Handle, NodeId};
|
|
||||||
|
|
||||||
use crate::terminal as term;
|
use crate::terminal as term;
|
||||||
use crate::terminal::args::{Args, Error, Help};
|
|
||||||
|
|
||||||
pub const HELP: Help = Help {
|
pub use args::Args;
|
||||||
name: "unfollow",
|
pub(crate) use args::ABOUT;
|
||||||
description: "Unfollow a peer",
|
|
||||||
version: env!("RADICLE_VERSION"),
|
|
||||||
usage: r#"
|
|
||||||
Usage
|
|
||||||
|
|
||||||
rad unfollow <nid> [<option>...]
|
pub fn run(options: Args, ctx: impl term::Context) -> anyhow::Result<()> {
|
||||||
|
|
||||||
The `unfollow` command takes a Node ID (<nid>), optionally in DID format,
|
|
||||||
and removes the follow policy for that peer.
|
|
||||||
|
|
||||||
Options
|
|
||||||
|
|
||||||
--verbose, -v Verbose output
|
|
||||||
--help Print help
|
|
||||||
"#,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Options {
|
|
||||||
pub nid: NodeId,
|
|
||||||
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 nid: Option<NodeId> = None;
|
|
||||||
let mut verbose = false;
|
|
||||||
|
|
||||||
while let Some(arg) = parser.next()? {
|
|
||||||
match &arg {
|
|
||||||
Value(val) if nid.is_none() => {
|
|
||||||
if let Ok(did) = term::args::did(val) {
|
|
||||||
nid = Some(did.into());
|
|
||||||
} else if let Ok(val) = term::args::nid(val) {
|
|
||||||
nid = Some(val);
|
|
||||||
} else {
|
|
||||||
anyhow::bail!("invalid Node ID `{}` specified", val.to_string_lossy());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Long("verbose") | Short('v') => verbose = true,
|
|
||||||
Long("help") | Short('h') => {
|
|
||||||
return Err(Error::Help.into());
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(anyhow!(arg.unexpected()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((
|
|
||||||
Options {
|
|
||||||
nid: nid.ok_or_else(|| anyhow!("a Node ID must be specified"))?,
|
|
||||||
verbose,
|
|
||||||
},
|
|
||||||
vec![],
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
|
|
||||||
let profile = ctx.profile()?;
|
let profile = ctx.profile()?;
|
||||||
let mut node = radicle::Node::new(profile.socket());
|
let mut node = radicle::Node::new(profile.socket());
|
||||||
let nid = options.nid;
|
let nid = options.nid;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
use radicle::node::NodeId;
|
||||||
|
use radicle::prelude::Did;
|
||||||
|
|
||||||
|
pub(crate) const ABOUT: &str = "Unfollow a peer";
|
||||||
|
|
||||||
|
const LONG_ABOUT: &str = r#"
|
||||||
|
The `unfollow` command takes a Node ID, optionally in DID format,
|
||||||
|
and removes the follow policy for that peer."#;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
#[error("invalid Node ID specified (Node ID parsing failed with: '{nid}', DID parsing failed with: '{did}'))")]
|
||||||
|
struct NodeIdParseError {
|
||||||
|
did: radicle::identity::did::DidError,
|
||||||
|
nid: radicle::crypto::PublicKeyError,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_nid(value: &str) -> Result<NodeId, NodeIdParseError> {
|
||||||
|
value.parse::<Did>().map(NodeId::from).or_else(|did| {
|
||||||
|
value
|
||||||
|
.parse::<NodeId>()
|
||||||
|
.map_err(|nid| NodeIdParseError { nid, did })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
|
||||||
|
pub struct Args {
|
||||||
|
/// Node ID (optionally in DID format) of the peer to unfollow
|
||||||
|
#[arg(value_name = "NID", value_parser = parse_nid)]
|
||||||
|
pub(super) nid: NodeId,
|
||||||
|
|
||||||
|
/// Verbose output
|
||||||
|
#[arg(short, long)]
|
||||||
|
pub(super) verbose: bool,
|
||||||
|
}
|
||||||
|
|
@ -49,6 +49,7 @@ enum Commands {
|
||||||
Issue(issue::Args),
|
Issue(issue::Args),
|
||||||
Path(path::Args),
|
Path(path::Args),
|
||||||
Stats(stats::Args),
|
Stats(stats::Args),
|
||||||
|
Unfollow(unfollow::Args),
|
||||||
Unseed(unseed::Args),
|
Unseed(unseed::Args),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -278,11 +279,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
"unfollow" => {
|
"unfollow" => {
|
||||||
term::run_command_args::<unfollow::Options, _>(
|
if let Some(Commands::Unfollow(args)) = CliArgs::parse().command {
|
||||||
unfollow::HELP,
|
term::run_command_fn(unfollow::run, args);
|
||||||
unfollow::run,
|
}
|
||||||
args.to_vec(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
"unseed" => {
|
"unseed" => {
|
||||||
if let Some(Commands::Unseed(args)) = CliArgs::parse().command {
|
if let Some(Commands::Unseed(args)) = CliArgs::parse().command {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue