Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/clean: Use clap
Merged lorenz opened 7 months ago

See issue/7fb03f234030b91c38cc4f5b48bd30cf5fd6a1de.

4 files changed +39 -70 753b7aef 6fb1ebec
modified crates/radicle-cli/src/commands/clean.rs
@@ -1,86 +1,24 @@
-
use std::ffi::OsString;
+
mod args;

-
use anyhow::anyhow;
-

-
use radicle::identity::RepoId;
use radicle::storage;
use radicle::storage::WriteStorage;

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

-
pub const HELP: Help = Help {
-
    name: "clean",
-
    description: "Remove all remotes from a repository",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad clean <rid> [<option>...]
-

-
    Removes all remotes from a repository, as long as they are not the
-
    local operator or a delegate of the repository.

-
    Note that remotes will still be fetched as long as they are
-
    followed and/or the follow scope is "all".
-

-
Options
-

-
    --no-confirm        Do not ask for confirmation before removal (default: false)
-
    --help              Print help
-
"#,
-
};
-

-
pub struct Options {
-
    rid: RepoId,
-
    confirm: 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 id: Option<RepoId> = None;
-
        let mut confirm = true;
-

-
        while let Some(arg) = parser.next()? {
-
            match arg {
-
                Long("no-confirm") => {
-
                    confirm = false;
-
                }
-
                Long("help") | Short('h') => {
-
                    return Err(Error::Help.into());
-
                }
-
                Value(val) if id.is_none() => {
-
                    id = Some(term::args::rid(&val)?);
-
                }
-
                _ => anyhow::bail!(arg.unexpected()),
-
            }
-
        }
-

-
        Ok((
-
            Options {
-
                rid: id
-
                    .ok_or_else(|| anyhow!("an RID must be provided; see `rad clean --help`"))?,
-
                confirm,
-
            },
-
            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 storage = &profile.storage;
-
    let rid = options.rid;
+
    let rid = args.repo;
    let path = storage::git::paths::repository(storage, &rid);

    if !path.exists() {
        anyhow::bail!("repository {rid} was not found");
    }

-
    if !options.confirm || term::confirm(format!("Clean {rid}?")) {
+
    if args.no_confirm || term::confirm(format!("Clean {rid}?")) {
        let cleaned = storage.clean(rid)?;
        for remote in cleaned {
            term::info!("Removed {remote}");
added crates/radicle-cli/src/commands/clean/args.rs
@@ -0,0 +1,25 @@
+
use clap::Parser;
+

+
use radicle::prelude::RepoId;
+

+
pub const ABOUT: &str = "Remove all remotes from a repository";
+

+
const LONG_ABOUT: &str = r#"
+
Removes all remotes from a repository, as long as they are not the
+
local operator or a delegate of the repository.
+

+
Note that remotes will still be fetched as long as they are
+
followed and/or the follow scope is "all".
+
"#;
+

+
#[derive(Debug, Parser)]
+
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
+
pub struct Args {
+
    /// Operate on the given repository
+
    #[arg(value_name = "RID")]
+
    pub(crate) repo: RepoId,
+

+
    /// Do not ask for confirmation before removal
+
    #[arg(long)]
+
    pub(crate) no_confirm: bool,
+
}
modified crates/radicle-cli/src/commands/help.rs
@@ -56,7 +56,10 @@ const COMMANDS: &[CommandItem] = &[
    CommandItem::Lexopt(crate::commands::node::HELP),
    CommandItem::Lexopt(crate::commands::patch::HELP),
    CommandItem::Lexopt(crate::commands::path::HELP),
-
    CommandItem::Lexopt(crate::commands::clean::HELP),
+
    CommandItem::Clap {
+
        name: "clean",
+
        about: crate::commands::clean::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::rad_self::HELP),
    CommandItem::Lexopt(crate::commands::seed::HELP),
    CommandItem::Lexopt(crate::commands::follow::HELP),
modified crates/radicle-cli/src/main.rs
@@ -45,6 +45,7 @@ struct CliArgs {

#[derive(Subcommand, Debug)]
enum Commands {
+
    Clean(clean::Args),
    Issue(issue::Args),
}

@@ -247,7 +248,9 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
            );
        }
        "clean" => {
-
            term::run_command_args::<clean::Options, _>(clean::HELP, clean::run, args.to_vec());
+
            if let Some(Commands::Clean(args)) = CliArgs::parse().command {
+
                term::run_command_fn(clean::run, args);
+
            }
        }
        "self" => {
            term::run_command_args::<rad_self::Options, _>(