Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/ls: use Clap
Merged levitte opened 6 months ago
4 files changed +45 -88 191c2879 191c2879
modified crates/radicle-cli/src/commands/help.rs
@@ -52,7 +52,10 @@ const COMMANDS: &[CommandItem] = &[
        name: "issue",
        about: crate::commands::issue::ABOUT,
    },
-
    CommandItem::Lexopt(crate::commands::ls::HELP),
+
    CommandItem::Clap {
+
        name: "ls",
+
        about: crate::commands::ls::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::node::HELP),
    CommandItem::Lexopt(crate::commands::patch::HELP),
    CommandItem::Clap {
modified crates/radicle-cli/src/commands/ls.rs
@@ -1,91 +1,15 @@
-
use std::ffi::OsString;
+
mod args;
+

+
pub use args::Args;
+
pub(crate) use args::ABOUT;

use radicle::storage::{ReadStorage, RepositoryInfo};

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

use term::Element;

-
pub const HELP: Help = Help {
-
    name: "ls",
-
    description: "List repositories",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad ls [<option>...]
-

-
    By default, this command shows you all repositories that you have forked or initialized.
-
    If you wish to see all seeded repositories, use the `--seeded` option.
-

-
Options
-

-
    --private       Show only private repositories
-
    --public        Show only public repositories
-
    --seeded, -s    Show all seeded repositories
-
    --all, -a       Show all repositories in storage
-
    --verbose, -v   Verbose output
-
    --help          Print help
-
"#,
-
};
-

-
pub struct Options {
-
    #[allow(dead_code)]
-
    verbose: bool,
-
    public: bool,
-
    private: bool,
-
    all: bool,
-
    seeded: 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 verbose = false;
-
        let mut private = false;
-
        let mut public = false;
-
        let mut all = false;
-
        let mut seeded = false;
-

-
        while let Some(arg) = parser.next()? {
-
            match arg {
-
                Long("help") | Short('h') => {
-
                    return Err(Error::Help.into());
-
                }
-
                Long("all") | Short('a') => {
-
                    all = true;
-
                }
-
                Long("seeded") | Short('s') => {
-
                    seeded = true;
-
                }
-
                Long("private") => {
-
                    private = true;
-
                }
-
                Long("public") => {
-
                    public = true;
-
                }
-
                Long("verbose") | Short('v') => verbose = true,
-
                _ => anyhow::bail!(arg.unexpected()),
-
            }
-
        }
-

-
        Ok((
-
            Options {
-
                verbose,
-
                private,
-
                public,
-
                all,
-
                seeded,
-
            },
-
            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 storage = &profile.storage;
    let repos = storage.repositories()?;
@@ -105,21 +29,21 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
        ..
    } in repos
    {
-
        if doc.is_public() && options.private && !options.public {
+
        if doc.is_public() && args.private {
            continue;
        }
-
        if !doc.is_public() && !options.private && options.public {
+
        if !doc.is_public() && args.public {
            continue;
        }
-
        if refs.is_none() && !options.all && !options.seeded {
+
        if refs.is_none() && !args.all && !args.seeded {
            continue;
        }
        let seeded = policy.is_seeding(&rid)?;

-
        if !seeded && !options.all {
+
        if !seeded && !args.all {
            continue;
        }
-
        if !seeded && options.seeded {
+
        if !seeded && args.seeded {
            continue;
        }
        let proj = match doc.project() {
added crates/radicle-cli/src/commands/ls/args.rs
@@ -0,0 +1,27 @@
+
use clap::Parser;
+

+
pub(crate) const ABOUT: &str = "List repositories";
+
const LONG_ABOUT: &str = r#"
+
By default, this command shows you all repositories that you have forked or initialized.
+
If you wish to see all seeded repositories, use the `--seeded` option.
+
"#;
+

+
#[derive(Debug, Parser)]
+
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
+
pub struct Args {
+
    /// Show only private repositories
+
    #[arg(long, conflicts_with = "public")]
+
    pub(super) private: bool,
+
    /// Show only public repositories
+
    #[arg(long)]
+
    pub(super) public: bool,
+
    /// Show all seeded repositories
+
    #[arg(short, long)]
+
    pub(super) seeded: bool,
+
    /// Show all repositories in storage
+
    #[arg(short, long)]
+
    pub(super) all: bool,
+
    /// Verbose output
+
    #[arg(short, long)]
+
    pub(super) verbose: bool,
+
}
modified crates/radicle-cli/src/main.rs
@@ -47,6 +47,7 @@ struct CliArgs {
enum Commands {
    Clean(clean::Args),
    Issue(issue::Args),
+
    Ls(ls::Args),
    Path(path::Args),
    Stats(stats::Args),
    Unfollow(unfollow::Args),
@@ -233,7 +234,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
            }
        }
        "ls" => {
-
            term::run_command_args::<ls::Options, _>(ls::HELP, ls::run, args.to_vec());
+
            if let Some(Commands::Ls(args)) = CliArgs::parse().command {
+
                term::run_command_fn(ls::run, args);
+
            }
        }
        "node" => {
            term::run_command_args::<node::Options, _>(node::HELP, node::run, args.to_vec());