-
Notifications
You must be signed in to change notification settings - Fork 1
refactor(cli): wire exitWithError() across key commands #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: fire-9/cli-framework
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,17 @@ | |
| * equivalent to `fw pr comment`. | ||
| */ | ||
|
|
||
| import { exitWithError } from "@outfitter/cli/output"; | ||
| import { | ||
| type GitHubClient, | ||
| commentHandler, | ||
| getDatabase, | ||
| loadConfig, | ||
| type FirewatchConfig, | ||
| } from "@outfitter/firewatch-core"; | ||
| import { silentLogger } from "@outfitter/firewatch-shared"; | ||
| import { Command, Option } from "commander"; | ||
|
|
||
| import { createAuthenticatedClient } from "../auth-client"; | ||
| import { applyCommonOptions } from "../query-helpers"; | ||
| import { parseRepoInput, parsePrNumber, resolveRepoOrThrow } from "../repo"; | ||
| import { parsePrNumber, resolveRepoOrThrow } from "../repo"; | ||
| import { outputStructured } from "../utils/json"; | ||
| import { shouldOutputJson } from "../utils/tty"; | ||
|
|
||
|
|
@@ -26,88 +27,51 @@ export interface CommentCommandOptions { | |
| noColor?: boolean; | ||
| } | ||
|
|
||
| interface CommentContext { | ||
| client: GitHubClient; | ||
| config: FirewatchConfig; | ||
| repo: string; | ||
| owner: string; | ||
| name: string; | ||
| outputJson: boolean; | ||
| } | ||
|
|
||
| async function createContext( | ||
| options: CommentCommandOptions | ||
| ): Promise<CommentContext> { | ||
| const config = await loadConfig(); | ||
| const repo = await resolveRepoOrThrow(options.repo); | ||
| const { owner, name } = parseRepoInput(repo); | ||
|
|
||
| const { client } = await createAuthenticatedClient(config.github_token); | ||
|
|
||
| return { | ||
| client, | ||
| config, | ||
| repo, | ||
| owner, | ||
| name, | ||
| outputJson: shouldOutputJson(options, config.output?.default_format), | ||
| }; | ||
| } | ||
|
|
||
| export async function commentAction( | ||
| pr: number, | ||
| body: string, | ||
| options: CommentCommandOptions | ||
| ): Promise<void> { | ||
| applyCommonOptions(options); | ||
| if (!body.trim()) { | ||
| console.error("Comment body cannot be empty."); | ||
| process.exit(1); | ||
| exitWithError(new Error("Comment body cannot be empty.")); | ||
| } | ||
|
|
||
| try { | ||
| const ctx = await createContext(options); | ||
| const config = await loadConfig(); | ||
| const db = getDatabase(); | ||
| const repo = await resolveRepoOrThrow(options.repo); | ||
| const outputJson = shouldOutputJson(options, config.output?.default_format); | ||
|
|
||
| const prIdResult = await ctx.client.fetchPullRequestId( | ||
| ctx.owner, | ||
| ctx.name, | ||
| pr | ||
| ); | ||
| if (prIdResult.isErr()) { | ||
| throw prIdResult.error; | ||
| } | ||
| const commentResult = await ctx.client.addIssueComment( | ||
| prIdResult.value, | ||
| body | ||
| const result = await commentHandler( | ||
| { pr, body, repo }, | ||
| { config, db, logger: silentLogger } | ||
| ); | ||
|
Comment on lines
+46
to
49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Routing Useful? React with 👍 / 👎. |
||
| if (commentResult.isErr()) { | ||
| throw commentResult.error; | ||
|
|
||
| if (result.isErr()) { | ||
| exitWithError(result.error); | ||
| } | ||
| const comment = commentResult.value; | ||
|
|
||
| const comment = result.value; | ||
| const payload = { | ||
| ok: true, | ||
| repo: ctx.repo, | ||
| repo, | ||
| pr, | ||
| action: "comment", | ||
| id: comment.id, | ||
| ...(comment.url && { url: comment.url }), | ||
| }; | ||
|
|
||
| if (ctx.outputJson) { | ||
| if (outputJson) { | ||
| await outputStructured(payload, "jsonl"); | ||
| } else { | ||
| console.log(`Added comment to ${ctx.repo}#${pr}.`); | ||
| console.log(`Added comment to ${repo}#${pr}.`); | ||
| if (comment.url) { | ||
| console.log(comment.url); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| "Comment failed:", | ||
| error instanceof Error ? error.message : error | ||
| ); | ||
| process.exit(1); | ||
| exitWithError(error instanceof Error ? error : new Error(String(error))); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initializing
dbhere introduces a new hard dependency on local cache storage for a command that only performs a GitHub mutation.getDatabase()creates directories/files under the cache path (mkdirSync+openDatabaseinpackages/core/src/cache.ts), sofw commentcan now fail before hitting GitHub in read-only/containerized environments where it previously worked.Useful? React with 👍 / 👎.