hyperhive/hive-forge/src/verbs/comment_show.rs

54 lines
1.6 KiB
Rust

//! `comment-show <id>` — print the body (or full JSON envelope
//! when `--json` is set globally) of a single comment by id.
use anyhow::{Result, bail};
use clap::Args as ClapArgs;
use serde_json::json;
use crate::client::{Client, index};
use crate::verbs::{
attachment_json, attachment_line, comment_reactions, print_json, reaction_summary, rfc3339,
};
#[derive(ClapArgs)]
pub struct Args {
/// Comment id.
id: u64,
}
pub fn run(client: &Client, args: Args) -> Result<()> {
let (owner, name) = client.owner_repo()?;
let Some(c) = client
.api()
.issue_get_comment(owner, name, index(args.id)?)
.send()?
else {
bail!("hive-forge comment-show: comment {} not found", args.id);
};
let reactions = comment_reactions(client, owner, name, args.id)?;
if client.json_mode() {
let trimmed = json!({
"id": c.id,
"user": c.user.as_ref().and_then(|u| u.login.as_deref()),
"created_at": rfc3339(c.created_at),
"updated_at": rfc3339(c.updated_at),
"body": c.body,
"url": c.html_url,
"attachments": attachment_json(c.assets.as_deref()),
"reactions": reactions,
});
print_json(&trimmed)
} else {
let body = c.body.as_deref().unwrap_or("");
println!("{body}");
for a in c.assets.as_deref().unwrap_or_default() {
if let Some(line) = attachment_line(a) {
println!("{line}");
}
}
if let Some(summary) = reaction_summary(&reactions) {
println!("[reactions: {summary}]");
}
Ok(())
}
}