import type { CliDeps } from "./deps.js"; import { axiError } from "./errors.js"; import { readFlagFile } from "./flag-file.js"; import { flagValue } from "./flags.js"; /** * Resolve the body text a mutation should send from its `--body`/`--body-file` * flags. Shared by every command that accepts a body (issue create, issue * comment, and the edit/close commands that follow). * * The two flags are mutually exclusive: accepting both and picking a winner * would silently discard text the caller meant to send. */ export function resolveBodySource( deps: CliDeps, flags: Record, command: string, ): string | undefined { const body = flagValue(flags, "--body"); const path = flagValue(flags, "--body-file"); if (body !== undefined && path !== undefined) { throw axiError( "Flags --body and --body-file are mutually exclusive", "VALIDATION_ERROR", bodyFlagSuggestion(command), ); } if (body !== undefined) { return body; } if (path !== undefined) { return readBodyFile(deps, path, command); } return undefined; } /** As {@link resolveBodySource}, for the commands where a body is mandatory. */ export function requireBodySource( deps: CliDeps, flags: Record, command: string, ): string { const body = resolveBodySource(deps, flags, command); if (body === undefined) { throw axiError( `${command} requires --body or --body-file `, "VALIDATION_ERROR", bodyFlagSuggestion(command), ); } return body; } function bodyFlagSuggestion(command: string): string[] { return [ `Run \`gitea-axi ${command} --body \` or \`gitea-axi ${command} --body-file \``, ]; } function readBodyFile(deps: CliDeps, path: string, command: string): string { return readFlagFile(deps, path, "--body-file", bodyFlagSuggestion(command)); }