cli/inspect: use Clap
This includes a horrible - but hopefully short-lived - hack to make `rad .` work. Co-authored-by: Lorenz Leutgeb <lorenz.leutgeb@radicle.xyz>
This commit is contained in:
parent
5ed1b8e579
commit
990e22acff
|
|
@ -75,7 +75,10 @@ const COMMANDS: &[CommandItem] = &[
|
||||||
about: crate::commands::init::ABOUT,
|
about: crate::commands::init::ABOUT,
|
||||||
},
|
},
|
||||||
CommandItem::Lexopt(crate::commands::inbox::HELP),
|
CommandItem::Lexopt(crate::commands::inbox::HELP),
|
||||||
CommandItem::Lexopt(crate::commands::inspect::HELP),
|
CommandItem::Clap {
|
||||||
|
name: "inspect",
|
||||||
|
about: crate::commands::inspect::ABOUT,
|
||||||
|
},
|
||||||
CommandItem::Clap {
|
CommandItem::Clap {
|
||||||
name: "issue",
|
name: "issue",
|
||||||
about: crate::commands::issue::ABOUT,
|
about: crate::commands::issue::ABOUT,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#![allow(clippy::or_fun_call)]
|
#![allow(clippy::or_fun_call)]
|
||||||
|
|
||||||
|
mod args;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ffi::OsString;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
|
@ -16,134 +18,40 @@ use radicle::storage::refs::RefsAt;
|
||||||
use radicle::storage::{ReadRepository, ReadStorage};
|
use radicle::storage::{ReadRepository, ReadStorage};
|
||||||
|
|
||||||
use crate::terminal as term;
|
use crate::terminal as term;
|
||||||
use crate::terminal::args::{Args, Error, Help};
|
|
||||||
use crate::terminal::json;
|
use crate::terminal::json;
|
||||||
use crate::terminal::Element;
|
use crate::terminal::Element;
|
||||||
|
|
||||||
pub const HELP: Help = Help {
|
pub use args::Args;
|
||||||
name: "inspect",
|
use args::Target;
|
||||||
description: "Inspect a Radicle repository",
|
pub(crate) use args::ABOUT;
|
||||||
version: env!("RADICLE_VERSION"),
|
|
||||||
usage: r#"
|
|
||||||
Usage
|
|
||||||
|
|
||||||
rad inspect <path> [<option>...]
|
pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
|
||||||
rad inspect <rid> [<option>...]
|
let rid = match args.repo {
|
||||||
rad inspect [<option>...]
|
Some(rid) => {
|
||||||
|
if let Ok(val) = RepoId::from_str(&rid) {
|
||||||
Inspects the given path or RID. If neither is specified,
|
val
|
||||||
the current repository is inspected.
|
} else {
|
||||||
|
radicle::rad::at(Path::new(&rid))
|
||||||
Options
|
.map(|(_, id)| id)
|
||||||
|
.context("Supplied argument is not a valid path")?
|
||||||
--rid Return the repository identifier (RID)
|
|
||||||
--payload Inspect the repository's identity payload
|
|
||||||
--refs Inspect the repository's refs on the local device
|
|
||||||
--sigrefs Inspect the values of `rad/sigrefs` for all remotes of this repository
|
|
||||||
--identity Inspect the identity document
|
|
||||||
--visibility Inspect the repository's visibility
|
|
||||||
--delegates Inspect the repository's delegates
|
|
||||||
--policy Inspect the repository's seeding policy
|
|
||||||
--history Show the history of the repository identity document
|
|
||||||
--help Print help
|
|
||||||
"#,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Eq, PartialEq)]
|
|
||||||
pub enum Target {
|
|
||||||
Refs,
|
|
||||||
Payload,
|
|
||||||
Delegates,
|
|
||||||
Identity,
|
|
||||||
Visibility,
|
|
||||||
Sigrefs,
|
|
||||||
Policy,
|
|
||||||
History,
|
|
||||||
#[default]
|
|
||||||
RepoId,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Eq, PartialEq)]
|
|
||||||
pub struct Options {
|
|
||||||
pub rid: Option<RepoId>,
|
|
||||||
pub target: Target,
|
|
||||||
}
|
|
||||||
|
|
||||||
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 rid: Option<RepoId> = None;
|
|
||||||
let mut target = Target::default();
|
|
||||||
|
|
||||||
while let Some(arg) = parser.next()? {
|
|
||||||
match arg {
|
|
||||||
Long("help") | Short('h') => {
|
|
||||||
return Err(Error::Help.into());
|
|
||||||
}
|
|
||||||
Long("refs") => {
|
|
||||||
target = Target::Refs;
|
|
||||||
}
|
|
||||||
Long("payload") => {
|
|
||||||
target = Target::Payload;
|
|
||||||
}
|
|
||||||
Long("policy") => {
|
|
||||||
target = Target::Policy;
|
|
||||||
}
|
|
||||||
Long("delegates") => {
|
|
||||||
target = Target::Delegates;
|
|
||||||
}
|
|
||||||
Long("history") => {
|
|
||||||
target = Target::History;
|
|
||||||
}
|
|
||||||
Long("identity") => {
|
|
||||||
target = Target::Identity;
|
|
||||||
}
|
|
||||||
Long("sigrefs") => {
|
|
||||||
target = Target::Sigrefs;
|
|
||||||
}
|
|
||||||
Long("rid") => {
|
|
||||||
target = Target::RepoId;
|
|
||||||
}
|
|
||||||
Long("visibility") => {
|
|
||||||
target = Target::Visibility;
|
|
||||||
}
|
|
||||||
Value(val) if rid.is_none() => {
|
|
||||||
let val = val.to_string_lossy();
|
|
||||||
|
|
||||||
if let Ok(val) = RepoId::from_str(&val) {
|
|
||||||
rid = Some(val);
|
|
||||||
} else {
|
|
||||||
rid = radicle::rad::at(Path::new(val.as_ref()))
|
|
||||||
.map(|(_, id)| Some(id))
|
|
||||||
.context("Supplied argument is not a valid path")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => anyhow::bail!(arg.unexpected()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((Options { rid, target }, vec![]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
|
|
||||||
let rid = match options.rid {
|
|
||||||
Some(rid) => rid,
|
|
||||||
None => radicle::rad::cwd()
|
None => radicle::rad::cwd()
|
||||||
.map(|(_, rid)| rid)
|
.map(|(_, rid)| rid)
|
||||||
.context("Current directory is not a Radicle repository")?,
|
.context("Current directory is not a Radicle repository")?,
|
||||||
};
|
};
|
||||||
|
|
||||||
if options.target == Target::RepoId {
|
let target = args.target.into();
|
||||||
|
|
||||||
|
if matches!(target, Target::RepoId) {
|
||||||
term::info!("{}", term::format::highlight(rid.urn()));
|
term::info!("{}", term::format::highlight(rid.urn()));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let profile = ctx.profile()?;
|
let profile = ctx.profile()?;
|
||||||
let storage = &profile.storage;
|
let storage = &profile.storage;
|
||||||
|
|
||||||
match options.target {
|
match target {
|
||||||
Target::Refs => {
|
Target::Refs => {
|
||||||
let (repo, _) = repo(rid, storage)?;
|
let (repo, _) = repo(rid, storage)?;
|
||||||
refs(&repo)?;
|
refs(&repo)?;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,97 @@
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
pub(crate) const ABOUT: &str = "Inspect a Radicle repository";
|
||||||
|
const LONG_ABOUT: &str = r#"Inspects the given path or RID. If neither is specified,
|
||||||
|
the current repository is inspected.
|
||||||
|
"#;
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
#[group(multiple = false)]
|
||||||
|
pub(super) struct TargetArgs {
|
||||||
|
/// Inspect the repository's delegates
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) delegates: bool,
|
||||||
|
|
||||||
|
/// Show the history of the repository identity document
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) history: bool,
|
||||||
|
|
||||||
|
/// Inspect the identity document
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) identity: bool,
|
||||||
|
|
||||||
|
/// Inspect the repository's identity payload
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) payload: bool,
|
||||||
|
|
||||||
|
/// Inspect the repository's seeding policy
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) policy: bool,
|
||||||
|
|
||||||
|
/// Inspect the repository's refs on the local device
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) refs: bool,
|
||||||
|
|
||||||
|
/// Return the repository identifier (RID)
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) rid: bool,
|
||||||
|
|
||||||
|
/// Inspect the values of `rad/sigrefs` for all remotes of this repository
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) sigrefs: bool,
|
||||||
|
|
||||||
|
/// Inspect the repository's visibility
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) visibility: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) enum Target {
|
||||||
|
Delegates,
|
||||||
|
History,
|
||||||
|
Identity,
|
||||||
|
Payload,
|
||||||
|
Policy,
|
||||||
|
Refs,
|
||||||
|
RepoId,
|
||||||
|
Sigrefs,
|
||||||
|
Visibility,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TargetArgs> for Target {
|
||||||
|
fn from(args: TargetArgs) -> Self {
|
||||||
|
match (
|
||||||
|
args.delegates,
|
||||||
|
args.history,
|
||||||
|
args.identity,
|
||||||
|
args.payload,
|
||||||
|
args.policy,
|
||||||
|
args.refs,
|
||||||
|
args.rid,
|
||||||
|
args.sigrefs,
|
||||||
|
args.visibility,
|
||||||
|
) {
|
||||||
|
(true, false, false, false, false, false, false, false, false) => Target::Delegates,
|
||||||
|
(false, true, false, false, false, false, false, false, false) => Target::History,
|
||||||
|
(false, false, true, false, false, false, false, false, false) => Target::Identity,
|
||||||
|
(false, false, false, true, false, false, false, false, false) => Target::Payload,
|
||||||
|
(false, false, false, false, true, false, false, false, false) => Target::Policy,
|
||||||
|
(false, false, false, false, false, true, false, false, false) => Target::Refs,
|
||||||
|
(false, false, false, false, false, false, true, false, false)
|
||||||
|
| (false, false, false, false, false, false, false, false, false) => Target::RepoId,
|
||||||
|
(false, false, false, false, false, false, false, true, false) => Target::Sigrefs,
|
||||||
|
(false, false, false, false, false, false, false, false, true) => Target::Visibility,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
|
||||||
|
pub struct Args {
|
||||||
|
/// Repository, by RID or by path
|
||||||
|
#[arg(value_name = "RID|PATH")]
|
||||||
|
pub(super) repo: Option<String>,
|
||||||
|
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub(super) target: TargetArgs,
|
||||||
|
}
|
||||||
|
|
@ -70,6 +70,7 @@ enum Commands {
|
||||||
Fork(fork::Args),
|
Fork(fork::Args),
|
||||||
Id(id::Args),
|
Id(id::Args),
|
||||||
Init(init::Args),
|
Init(init::Args),
|
||||||
|
Inspect(inspect::Args),
|
||||||
Issue(issue::Args),
|
Issue(issue::Args),
|
||||||
Ls(ls::Args),
|
Ls(ls::Args),
|
||||||
Node(node::Args),
|
Node(node::Args),
|
||||||
|
|
@ -276,11 +277,20 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"inspect" => {
|
"inspect" => {
|
||||||
term::run_command_args::<inspect::Options, _>(
|
let reconstructed_args = {
|
||||||
inspect::HELP,
|
// This is a horrible workaround to reconstruct the original
|
||||||
inspect::run,
|
// args after having them mangled by our `lexopt`-style parser
|
||||||
args.to_vec(),
|
// in `parse_args()` in case they were `rad .`.
|
||||||
);
|
// TODO: Remove this, when `rad` is fully migrated to `clap`.
|
||||||
|
vec!["rad", "inspect"]
|
||||||
|
.into_iter()
|
||||||
|
.map(OsString::from)
|
||||||
|
.chain(args.iter().cloned())
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(Commands::Inspect(args)) = CliArgs::parse_from(reconstructed_args).command {
|
||||||
|
term::run_command_fn(inspect::run, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
"issue" => {
|
"issue" => {
|
||||||
if let Some(Commands::Issue(args)) = CliArgs::parse().command {
|
if let Some(Commands::Issue(args)) = CliArgs::parse().command {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue