cli/unseed: Use clap
This commit is contained in:
parent
80bc95269b
commit
01f9f3fcd4
|
|
@ -62,7 +62,10 @@ const COMMANDS: &[CommandItem] = &[
|
|||
CommandItem::Lexopt(crate::commands::follow::HELP),
|
||||
CommandItem::Lexopt(crate::commands::unblock::HELP),
|
||||
CommandItem::Lexopt(crate::commands::unfollow::HELP),
|
||||
CommandItem::Lexopt(crate::commands::unseed::HELP),
|
||||
CommandItem::Clap {
|
||||
name: "unseed",
|
||||
about: crate::commands::unseed::ABOUT,
|
||||
},
|
||||
CommandItem::Lexopt(crate::commands::remote::HELP),
|
||||
CommandItem::Clap {
|
||||
name: "stats",
|
||||
|
|
|
|||
|
|
@ -1,70 +1,13 @@
|
|||
use std::ffi::OsString;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use nonempty::NonEmpty;
|
||||
pub mod args;
|
||||
|
||||
use radicle::{prelude::*, Node};
|
||||
|
||||
use crate::terminal as term;
|
||||
use crate::terminal::args::{Args, Error, Help};
|
||||
|
||||
pub const HELP: Help = Help {
|
||||
name: "unseed",
|
||||
description: "Remove repository seeding policies",
|
||||
version: env!("RADICLE_VERSION"),
|
||||
usage: r#"
|
||||
Usage
|
||||
pub use args::Args;
|
||||
pub(crate) use args::ABOUT;
|
||||
|
||||
rad unseed <rid>... [<option>...]
|
||||
|
||||
The `unseed` command removes the seeding policy, if found,
|
||||
for the given repositories.
|
||||
|
||||
Options
|
||||
|
||||
--help Print help
|
||||
"#,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Options {
|
||||
rids: NonEmpty<RepoId>,
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
while let Some(arg) = parser.next()? {
|
||||
match &arg {
|
||||
Value(val) => {
|
||||
let rid = term::args::rid(val)?;
|
||||
rids.push(rid);
|
||||
}
|
||||
Long("help") | Short('h') => {
|
||||
return Err(Error::Help.into());
|
||||
}
|
||||
_ => {
|
||||
return Err(anyhow!(arg.unexpected()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((
|
||||
Options {
|
||||
rids: NonEmpty::from_vec(rids).ok_or(anyhow!(
|
||||
"At least one Repository ID must be provided; see `rad unseed --help`"
|
||||
))?,
|
||||
},
|
||||
vec![],
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
|
||||
pub fn run(options: Args, ctx: impl term::Context) -> anyhow::Result<()> {
|
||||
let profile = ctx.profile()?;
|
||||
let mut node = radicle::Node::new(profile.socket());
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
use clap::Parser;
|
||||
use radicle::prelude::RepoId;
|
||||
|
||||
pub(crate) const ABOUT: &str = "Remove repository seeding policies";
|
||||
|
||||
const LONG_ABOUT: &str = r#"
|
||||
The `unseed` command removes the seeding policy, if found,
|
||||
for the given repositories."#;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
|
||||
pub struct Args {
|
||||
/// ID of the repository to remove the seeding policy for (may be repeated)
|
||||
#[arg(value_name = "RID", required = true, action = clap::ArgAction::Append)]
|
||||
pub rids: Vec<RepoId>,
|
||||
}
|
||||
|
|
@ -47,6 +47,7 @@ struct CliArgs {
|
|||
enum Commands {
|
||||
Issue(issue::Args),
|
||||
Stats(stats::Args),
|
||||
Unseed(unseed::Args),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -278,7 +279,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
|
|||
);
|
||||
}
|
||||
"unseed" => {
|
||||
term::run_command_args::<unseed::Options, _>(unseed::HELP, unseed::run, args.to_vec());
|
||||
if let Some(Commands::Unseed(args)) = CliArgs::parse().command {
|
||||
term::run_command_fn(unseed::run, args);
|
||||
}
|
||||
}
|
||||
"remote" => {
|
||||
term::run_command_args::<remote::Options, _>(remote::HELP, remote::run, args.to_vec())
|
||||
|
|
|
|||
Loading…
Reference in New Issue