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)?;
if let Ok(mut issue) = issues.get_mut(&id) {
let signer = term::signer(&profile)?;
let comment_id = comment_id.unwrap_or_else(|| {
let (comment_id, _) = term::io::comment_select(&issue).unwrap();
*comment_id
});
let comment_id = match comment_id {
Some(cid) => cid,
None => *term::io::comment_select(&issue).map(|(cid, _)| cid)?,
};
issue.react(comment_id, reaction, true, &signer)?;
}
}

View File

@ -1,3 +1,4 @@
use anyhow::anyhow;
use radicle::cob::issue::Issue;
use radicle::cob::thread::{Comment, CommentId};
use radicle::crypto::ssh::keystore::MemorySigner;
@ -63,7 +64,7 @@ pub fn signer(profile: &Profile) -> anyhow::Result<Box<dyn Signer>> {
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 selection = Select::new(
"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_formatter(&|i| comments[i.index].1.body().to_owned())
.prompt()
.ok()?;
.prompt()?;
comments.get(selection).copied()
comments
.get(selection)
.copied()
.ok_or(anyhow!("failed to perform comment selection"))
}