cli: Update `rad inspect` command

Rename `--id` flag to `--rid`. Update code to match.
This commit is contained in:
cloudhead 2023-09-02 20:34:28 +02:00
parent ba5ed58997
commit 56d6e24ae6
No known key found for this signature in database
1 changed files with 23 additions and 26 deletions

View File

@ -1,7 +1,7 @@
#![allow(clippy::or_fun_call)] #![allow(clippy::or_fun_call)]
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::OsString; use std::ffi::OsString;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use anyhow::{anyhow, Context as _}; use anyhow::{anyhow, Context as _};
@ -34,7 +34,7 @@ Usage
Options Options
--id Return the repository identifier (RID) --rid Return the repository identifier (RID)
--payload Inspect the repository's identity payload --payload Inspect the repository's identity payload
--refs Inspect the repository's refs on the local device --refs Inspect the repository's refs on the local device
--identity Inspect the identity document --identity Inspect the identity document
@ -54,12 +54,12 @@ pub enum Target {
Policy, Policy,
History, History,
#[default] #[default]
Id, RepoId,
} }
#[derive(Default, Debug, Eq, PartialEq)] #[derive(Default, Debug, Eq, PartialEq)]
pub struct Options { pub struct Options {
pub id: Option<Id>, pub rid: Option<Id>,
pub target: Target, pub target: Target,
} }
@ -68,7 +68,7 @@ impl Args for Options {
use lexopt::prelude::*; use lexopt::prelude::*;
let mut parser = lexopt::Parser::from_args(args); let mut parser = lexopt::Parser::from_args(args);
let mut id: Option<Id> = None; let mut rid: Option<Id> = None;
let mut target = Target::default(); let mut target = Target::default();
while let Some(arg) = parser.next()? { while let Some(arg) = parser.next()? {
@ -94,16 +94,16 @@ impl Args for Options {
Long("identity") => { Long("identity") => {
target = Target::Identity; target = Target::Identity;
} }
Long("id") => { Long("rid") => {
target = Target::Id; target = Target::RepoId;
} }
Value(val) if id.is_none() => { Value(val) if rid.is_none() => {
let val = val.to_string_lossy(); let val = val.to_string_lossy();
if let Ok(val) = Id::from_str(&val) { if let Ok(val) = Id::from_str(&val) {
id = Some(val); rid = Some(val);
} else if let Ok(val) = PathBuf::from_str(&val) { } else if let Ok(val) = PathBuf::from_str(&val) {
id = radicle::rad::repo(val) rid = radicle::rad::repo(val)
.map(|(_, id)| Some(id)) .map(|(_, id)| Some(id))
.context("Supplied argument is not a valid path")?; .context("Supplied argument is not a valid path")?;
} else { } else {
@ -114,23 +114,20 @@ impl Args for Options {
} }
} }
Ok((Options { id, target }, vec![])) Ok((Options { rid, target }, vec![]))
} }
} }
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let id = match options.id { let rid = match options.rid {
Some(id) => id, Some(rid) => rid,
None => { None => radicle::rad::cwd()
let (_, id) = radicle::rad::repo(Path::new(".")) .map(|(_, rid)| rid)
.context("Current directory is not a radicle project")?; .context("Current directory is not a radicle project")?,
id
}
}; };
if options.target == Target::Id { if options.target == Target::RepoId {
term::info!("{}", term::format::highlight(id.urn())); term::info!("{}", term::format::highlight(rid.urn()));
return Ok(()); return Ok(());
} }
@ -138,7 +135,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let storage = &profile.storage; let storage = &profile.storage;
let signer = term::signer(&profile)?; let signer = term::signer(&profile)?;
let repo = storage let repo = storage
.repository(id) .repository(rid)
.context("No project with the given RID exists")?; .context("No project with the given RID exists")?;
let project = Doc::<Verified>::canonical(&repo)?; let project = Doc::<Verified>::canonical(&repo)?;
@ -160,20 +157,20 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
} }
Target::Policy => { Target::Policy => {
let tracking = profile.tracking()?; let tracking = profile.tracking()?;
if let Some(repo) = tracking.repo_policy(&id)? { if let Some(repo) = tracking.repo_policy(&rid)? {
let tracking = match repo.policy { let tracking = match repo.policy {
Policy::Track => term::format::positive("tracked"), Policy::Track => term::format::positive("tracked"),
Policy::Block => term::format::negative("blocked"), Policy::Block => term::format::negative("blocked"),
}; };
println!( println!(
"Repository {} is {} with scope {}", "Repository {} is {} with scope {}",
term::format::tertiary(&id), term::format::tertiary(&rid),
tracking, tracking,
term::format::dim(format!("`{}`", repo.scope)) term::format::dim(format!("`{}`", repo.scope))
); );
} else { } else {
term::print(term::format::italic(format!( term::print(term::format::italic(format!(
"No tracking policy found for {id}" "No tracking policy found for {rid}"
))); )));
} }
} }
@ -246,7 +243,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
println!(); println!();
} }
} }
Target::Id => { Target::RepoId => {
// Handled above. // Handled above.
} }
} }