use clap::{ArgMatches, Command};
use crate::domain::model::body::Body;
use crate::domain::model::record_ref::IssueRef;
use crate::domain::usecases::issue::{edit_issue_body, EditIssueBodyOutcome, IssueRepository};
use crate::infra::driving::cli::commands::generic::body as generic;
use crate::infra::driving::cli::errors::{die1, CliError};
use crate::infra::driving::cli::helpers::{read_body_from_stdin_or_editor, required_str};
use crate::infra::driving::cli::id_parsing::parse_issue_id;
use crate::infra::driving::cli::Context;
const NOUN: &str = "issue";
pub(super) fn subcommand() -> Command {
generic::subcommand(NOUN, "Issue ID (e.g. ISSUE-01H8MSQGXYZ12)")
}
pub(super) fn execute(matches: &ArgMatches, ctx: &Context<'_>) {
match matches.subcommand() {
Some(("show", sub)) => execute_show(sub, ctx),
Some(("edit", sub)) => execute_edit(sub, ctx),
_ => unreachable!(),
}
}
fn execute_show(sub: &ArgMatches, ctx: &Context<'_>) {
let output_fmt = ctx.output_fmt;
let (id_str, id) = parse_id(sub, ctx);
let repo = ctx.issue_repository();
let view = match repo.find_by_id(&id) {
Ok(Some(issue)) => generic::View::Shown {
body: issue.content.as_str().to_string(),
},
Ok(None) => generic::View::NotFound,
Err(e) => die1(CliError::new(e.to_string()), output_fmt),
};
generic::render(view, id_str, &id.to_string(), NOUN, output_fmt);
}
fn execute_edit(sub: &ArgMatches, ctx: &Context<'_>) {
let output_fmt = ctx.output_fmt;
let (id_str, id) = parse_id(sub, ctx);
let repo = ctx.issue_repository();
let current_body = match repo.find_by_id(&id) {
Ok(Some(issue)) => issue.content.as_str().to_string(),
Ok(None) => {
generic::render(
generic::View::NotFound,
id_str,
&id.to_string(),
NOUN,
output_fmt,
);
return;
}
Err(e) => die1(CliError::new(e.to_string()), output_fmt),
};
let new_body = read_body_from_stdin_or_editor(¤t_body, output_fmt);
let typed_body = Body::new(&new_body);
let view = match edit_issue_body(&repo, &id, typed_body) {
Ok(EditIssueBodyOutcome::Updated) => generic::View::Edited,
Ok(EditIssueBodyOutcome::NoOp) => generic::View::EditNoOp,
Err(e) => die1(CliError::new(e.to_string()), output_fmt),
};
generic::render(view, id_str, &id.to_string(), NOUN, output_fmt);
}
fn parse_id<'a>(sub: &'a ArgMatches, ctx: &Context<'_>) -> (&'a str, IssueRef) {
let output_fmt = ctx.output_fmt;
let id_str = required_str(sub, "id");
let id = parse_issue_id(id_str, ctx.issues_id_prefix()).unwrap_or_else(|e| {
die1(
CliError::new(format!("invalid issue ID '{id_str}': {e}")).kind("validation"),
output_fmt,
);
});
(id_str, id)
}