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

59 lines
1.6 KiB
Rust

//! `comment-edit <id> [body sources] [repo]` — edit an existing
//! comment by id.
use anyhow::{Result, bail};
use clap::Args as ClapArgs;
use forgejo_api::structs::EditIssueCommentOption;
use serde_json::json;
use crate::body;
use crate::client::{Client, index};
use crate::verbs::print_json;
#[derive(ClapArgs)]
pub struct Args {
/// Comment id.
id: u64,
/// Inline body text.
#[arg(long, conflicts_with = "body_file")]
body: Option<String>,
/// Read body from a file. `-` means stdin.
#[arg(long = "body-file")]
body_file: Option<String>,
}
/// # Errors
///
/// Propagates any I/O error from the body input (`--body-file`,
/// stdin), any transport error from the Forgejo REST call (network
/// unreachable, 4xx/5xx response, token missing/invalid), and any
/// I/O error from writing the response to stdout.
pub fn run(client: &Client, args: Args) -> Result<()> {
let body = body::resolve_required(
args.body.as_deref(),
args.body_file.as_deref(),
"comment-edit",
)?;
let (owner, name) = client.owner_repo()?;
let Some(resp) = client
.api()
.issue_edit_comment(
owner,
name,
index(args.id)?,
EditIssueCommentOption {
body,
updated_at: None,
},
)
.send()?
else {
// Forgejo answers 204 (no content) when the edit was a no-op.
bail!("hive-forge comment-edit: comment {} not updated", args.id);
};
print_json(&json!({
"id": resp.id,
"user": resp.user.as_ref().and_then(|u| u.login.as_deref()),
"url": resp.html_url,
}))
}