cli: fallible comment selector

The usage of the `comment_select` used `unwrap`. It would be better to have the
function be fallible instead and provide a somewhat helpful message.
This commit is contained in:
Fintan Halpenny 2025-04-16 16:01:59 +02:00
parent 7eb07e1b4d
commit 2af090ea1e
2 changed files with 11 additions and 8 deletions

View File

@ -591,10 +591,10 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let id = id.resolve(&repo.backend)?; let id = id.resolve(&repo.backend)?;
if let Ok(mut issue) = issues.get_mut(&id) { if let Ok(mut issue) = issues.get_mut(&id) {
let signer = term::signer(&profile)?; let signer = term::signer(&profile)?;
let comment_id = comment_id.unwrap_or_else(|| { let comment_id = match comment_id {
let (comment_id, _) = term::io::comment_select(&issue).unwrap(); Some(cid) => cid,
*comment_id None => *term::io::comment_select(&issue).map(|(cid, _)| cid)?,
}); };
issue.react(comment_id, reaction, true, &signer)?; issue.react(comment_id, reaction, true, &signer)?;
} }
} }

View File

@ -1,3 +1,4 @@
use anyhow::anyhow;
use radicle::cob::issue::Issue; use radicle::cob::issue::Issue;
use radicle::cob::thread::{Comment, CommentId}; use radicle::cob::thread::{Comment, CommentId};
use radicle::crypto::ssh::keystore::MemorySigner; use radicle::crypto::ssh::keystore::MemorySigner;
@ -63,7 +64,7 @@ pub fn signer(profile: &Profile) -> anyhow::Result<Box<dyn Signer>> {
Ok(signer.boxed()) Ok(signer.boxed())
} }
pub fn comment_select(issue: &Issue) -> Option<(&CommentId, &Comment)> { pub fn comment_select(issue: &Issue) -> anyhow::Result<(&CommentId, &Comment)> {
let comments = issue.comments().collect::<Vec<_>>(); let comments = issue.comments().collect::<Vec<_>>();
let selection = Select::new( let selection = Select::new(
"Which comment do you want to react to?", "Which comment do you want to react to?",
@ -71,8 +72,10 @@ pub fn comment_select(issue: &Issue) -> Option<(&CommentId, &Comment)> {
) )
.with_render_config(*CONFIG) .with_render_config(*CONFIG)
.with_formatter(&|i| comments[i.index].1.body().to_owned()) .with_formatter(&|i| comments[i.index].1.body().to_owned())
.prompt() .prompt()?;
.ok()?;
comments.get(selection).copied() comments
.get(selection)
.copied()
.ok_or(anyhow!("failed to perform comment selection"))
} }