From 75bf7beeddbf24fe88f079f8b77e00ff3076b775 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Tue, 18 Apr 2023 13:23:44 +0100 Subject: [PATCH] cli: add verbose flag to rad ls A repository may fail to load when trying to run `rad ls`, but the failure will be hidden from the output. Add a `--verbose` flag to add warning information to stdout when a repository fails to load. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-cli/src/commands/ls.rs | 44 +++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/radicle-cli/src/commands/ls.rs b/radicle-cli/src/commands/ls.rs index c34994c3..73e5e916 100644 --- a/radicle-cli/src/commands/ls.rs +++ b/radicle-cli/src/commands/ls.rs @@ -18,41 +18,69 @@ Usage Options - --help Print help + --versbose, -v Verbose output + --help Print help "#, }; -pub struct Options {} +pub struct Options { + verbose: bool, +} impl Args for Options { fn from_args(args: Vec) -> anyhow::Result<(Self, Vec)> { use lexopt::prelude::*; let mut parser = lexopt::Parser::from_args(args); + let mut verbose = false; if let Some(arg) = parser.next()? { match arg { Long("help") => { return Err(Error::Help.into()); } + Long("verbose") | Short('v') => verbose = true, _ => return Err(anyhow::anyhow!(arg.unexpected())), } } - Ok((Options {}, vec![])) + Ok((Options { verbose }, vec![])) } } -pub fn run(_options: Options, ctx: impl term::Context) -> anyhow::Result<()> { +pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { let profile = ctx.profile()?; let storage = &profile.storage; let mut table = term::Table::default(); for id in storage.repositories()? { - let Ok(repo) = storage.repository(id) else { continue }; - let Ok((_, head)) = repo.head() else { continue }; - let Ok(proj) = repo.project() else { continue }; - + let repo = match storage.repository(id) { + Ok(repo) => repo, + Err(err) => { + if options.verbose { + term::warning(&format!("failed to load project '{id}': {err}")); + } + continue; + } + }; + let head = match repo.head() { + Ok((_, head)) => head, + Err(err) => { + if options.verbose { + term::warning(&format!("failed to get head of project '{id}': {err}")); + } + continue; + } + }; + let proj = match repo.project() { + Ok(proj) => proj, + Err(err) => { + if options.verbose { + term::warning(&format!("failed to get local project '{id}': {err}")); + } + continue; + } + }; let head = term::format::oid(head); table.push([ term::format::bold(proj.name().to_owned()),