cli/ls: use Clap

This adds `conflicts_with` for `private` and `public` to simplify the logic that checks for the document visibility
(thank you Fintan)

Co-authored-by: Fintan Halpenny <fintan.halpenny@gmail.com>
This commit is contained in:
Richard Levitte 2025-10-04 07:14:04 +02:00 committed by Erik Kundt
parent 8ba3087cb9
commit 191c287955
4 changed files with 45 additions and 88 deletions

View File

@ -68,7 +68,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 {

View File

@ -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() {

View File

@ -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,
}

View File

@ -52,6 +52,7 @@ enum Commands {
Fork(fork::Args),
Init(init::Args),
Issue(issue::Args),
Ls(ls::Args),
Path(path::Args),
Publish(publish::Args),
Stats(stats::Args),
@ -249,7 +250,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());