cli/self: Use clap

This commit is contained in:
Sebastian Martinez 2025-10-17 14:41:53 +02:00 committed by Fintan Halpenny
parent 27a85987c3
commit c1d9f04963
4 changed files with 67 additions and 122 deletions

View File

@ -95,7 +95,10 @@ const COMMANDS: &[CommandItem] = &[
name: "clean", name: "clean",
about: crate::commands::clean::ABOUT, about: crate::commands::clean::ABOUT,
}, },
CommandItem::Lexopt(crate::commands::rad_self::HELP), CommandItem::Clap {
name: "self",
about: crate::commands::rad_self::ABOUT,
},
CommandItem::Clap { CommandItem::Clap {
name: "seed", name: "seed",
about: crate::commands::seed::ABOUT, about: crate::commands::seed::ABOUT,

View File

@ -1,129 +1,41 @@
use std::ffi::OsString; #[path = "self/args.rs"]
mod args;
pub use args::Args;
pub(crate) use args::ABOUT;
use radicle::crypto::ssh; use radicle::crypto::ssh;
use radicle::node::Handle as _; use radicle::node::Handle as _;
use radicle::{Node, Profile}; use radicle::{Node, Profile};
use crate::terminal as term; use crate::terminal as term;
use crate::terminal::args::{Args, Error, Help};
use crate::terminal::Element as _; use crate::terminal::Element as _;
pub const HELP: Help = Help { pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
name: "self",
description: "Show information about your identity and device",
version: env!("RADICLE_VERSION"),
usage: r#"
Usage
rad self [<option>...]
Options
--did Show your DID
--alias Show your Node alias
--home Show your Radicle home
--config Show the location of your configuration file
--ssh-key Show your public key in OpenSSH format
--ssh-fingerprint Show your public key fingerprint in OpenSSH format
--help Show help
"#,
};
#[derive(Debug)]
enum Show {
Alias,
NodeId,
Did,
Home,
Config,
SshKey,
SshFingerprint,
All,
}
#[derive(Debug)]
pub struct Options {
show: Show,
}
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 show: Option<Show> = None;
while let Some(arg) = parser.next()? {
match arg {
Long("alias") if show.is_none() => {
show = Some(Show::Alias);
}
Long("nid") if show.is_none() => {
show = Some(Show::NodeId);
}
Long("did") if show.is_none() => {
show = Some(Show::Did);
}
Long("home") if show.is_none() => {
show = Some(Show::Home);
}
Long("config") if show.is_none() => {
show = Some(Show::Config);
}
Long("ssh-key") if show.is_none() => {
show = Some(Show::SshKey);
}
Long("ssh-fingerprint") if show.is_none() => {
show = Some(Show::SshFingerprint);
}
Long("help") | Short('h') => {
return Err(Error::Help.into());
}
_ => anyhow::bail!(arg.unexpected()),
}
}
Ok((
Options {
show: show.unwrap_or(Show::All),
},
vec![],
))
}
}
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?; let profile = ctx.profile()?;
match options.show { if args.did {
Show::Alias => { term::print(profile.did());
term::print(profile.config.alias()); } else if args.alias {
} term::print(profile.config.alias());
Show::NodeId => { } else if args.home {
crate::warning::deprecated("rad self --nid", "rad node status --only nid"); term::print(profile.home().path().display());
term::print( } else if args.ssh_key {
Node::new(profile.socket()) term::print(ssh::fmt::key(profile.id()));
.nid() } else if args.config {
.ok() term::print(profile.home.config().display());
.unwrap_or_else(|| *profile.id()), } else if args.ssh_fingerprint {
); term::print(ssh::fmt::fingerprint(profile.id()));
} } else if args.nid {
Show::Did => { crate::warning::deprecated("rad self --nid", "rad node status --only nid");
term::print(profile.did()); term::print(
} Node::new(profile.socket())
Show::Home => { .nid()
term::print(profile.home().path().display()); .ok()
} .unwrap_or_else(|| *profile.id()),
Show::Config => { );
term::print(profile.home.config().display()); } else {
} all(&profile)?
Show::SshKey => {
term::print(ssh::fmt::key(profile.id()));
}
Show::SshFingerprint => {
term::print(ssh::fmt::fingerprint(profile.id()));
}
Show::All => all(&profile)?,
} }
Ok(()) Ok(())

View File

@ -0,0 +1,30 @@
use clap::Parser;
pub(crate) const ABOUT: &str = "Show information about your identity and device";
#[derive(Debug, Parser)]
#[command(about = ABOUT, disable_version_flag = true)]
#[group(multiple = false)]
pub struct Args {
/// Show your DID
#[arg(long)]
pub(super) did: bool,
/// Show your Node alias
#[arg(long)]
pub(super) alias: bool,
/// Show your Node identifier
#[arg(long, hide(true))]
pub(super) nid: bool,
/// Show your Radicle home
#[arg(long)]
pub(super) home: bool,
/// Show the location of your configuration file
#[arg(long)]
pub(super) config: bool,
/// Show your public key in OpenSSH format
#[arg(long)]
pub(super) ssh_key: bool,
/// Show your public key fingerprint in OpenSSH format
#[arg(long)]
pub(super) ssh_fingerprint: bool,
}

View File

@ -72,6 +72,8 @@ enum Commands {
Path(path::Args), Path(path::Args),
Publish(publish::Args), Publish(publish::Args),
Seed(seed::Args), Seed(seed::Args),
#[command(name = "self")]
RadSelf(rad_self::Args),
Stats(stats::Args), Stats(stats::Args),
Unblock(unblock::Args), Unblock(unblock::Args),
Unfollow(unfollow::Args), Unfollow(unfollow::Args),
@ -303,11 +305,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
} }
} }
"self" => { "self" => {
term::run_command_args::<rad_self::Options, _>( if let Some(Commands::RadSelf(args)) = CliArgs::parse().command {
rad_self::HELP, term::run_command_fn(rad_self::run, args)
rad_self::run, }
args.to_vec(),
);
} }
"sync" => { "sync" => {
term::run_command_args::<sync::Options, _>(sync::HELP, sync::run, args.to_vec()); term::run_command_args::<sync::Options, _>(sync::HELP, sync::run, args.to_vec());