cli/stats: Use clap

This commit is contained in:
Lorenz Leutgeb 2025-09-29 09:37:01 +02:00
parent f1c7c98607
commit 80bc95269b
No known key found for this signature in database
4 changed files with 21 additions and 43 deletions

View File

@ -64,7 +64,10 @@ const COMMANDS: &[CommandItem] = &[
CommandItem::Lexopt(crate::commands::unfollow::HELP),
CommandItem::Lexopt(crate::commands::unseed::HELP),
CommandItem::Lexopt(crate::commands::remote::HELP),
CommandItem::Lexopt(crate::commands::stats::HELP),
CommandItem::Clap {
name: "stats",
about: crate::commands::stats::ABOUT,
},
CommandItem::Lexopt(crate::commands::sync::HELP),
];

View File

@ -1,4 +1,5 @@
use std::ffi::OsString;
mod args;
use std::path::Path;
use localtime::LocalDuration;
@ -13,22 +14,9 @@ use radicle_term::Element;
use serde::Serialize;
use crate::terminal as term;
use crate::terminal::args::{Args, Error, Help};
pub const HELP: Help = Help {
name: "stats",
description: "Displays aggregated repository and node metrics",
version: env!("RADICLE_VERSION"),
usage: r#"
Usage
rad stats [<option>...]
Options
--help Print help
"#,
};
pub use args::Args;
pub(crate) use args::ABOUT;
#[derive(Default, Serialize)]
#[serde(rename_all = "camelCase")]
@ -65,30 +53,7 @@ struct Stats {
nodes: NodeStats,
}
#[derive(Default, Debug, Eq, PartialEq)]
pub struct Options {}
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);
#[allow(clippy::never_loop)]
while let Some(arg) = parser.next()? {
match arg {
Long("help") | Short('h') => {
return Err(Error::Help.into());
}
_ => anyhow::bail!(arg.unexpected()),
}
}
Ok((Options {}, 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 mut stats = Stats::default();

View File

@ -0,0 +1,7 @@
use clap::Parser;
pub(crate) const ABOUT: &str = "Displays aggregated repository and node metrics";
#[derive(Debug, Parser)]
#[command(about = ABOUT, disable_version_flag = true)]
pub struct Args {}

View File

@ -46,6 +46,7 @@ struct CliArgs {
#[derive(Subcommand, Debug)]
enum Commands {
Issue(issue::Args),
Stats(stats::Args),
}
#[derive(Debug)]
@ -167,7 +168,7 @@ fn run(command: Command) -> Result<(), Option<anyhow::Error>> {
/// arguments again. This needs to be done for each migrated command
/// individually, otherwise `clap` would fail to parse on an non-migrated and
/// therefore unknown command.
fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
match exe {
"auth" => {
term::run_command_args::<auth::Options, _>(auth::HELP, auth::run, args.to_vec());
@ -283,7 +284,9 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
term::run_command_args::<remote::Options, _>(remote::HELP, remote::run, args.to_vec())
}
"stats" => {
term::run_command_args::<stats::Options, _>(stats::HELP, stats::run, args.to_vec())
if let Some(Commands::Stats(args)) = CliArgs::parse().command {
term::run_command_fn(stats::run, args);
}
}
"watch" => {
term::run_command_args::<watch::Options, _>(watch::HELP, watch::run, args.to_vec())