|
1 | 1 | import type { Command } from "commander"; |
2 | 2 | import type { HealthSummary } from "../../commands/health.js"; |
| 3 | +import { formatGatewayTransportErrorJson } from "../../gateway/call.js"; |
3 | 4 | import type { CostUsageSummary } from "../../infra/session-cost-usage.js"; |
4 | 5 | import type { |
5 | 6 | DiagnosticStabilityBundle, |
@@ -97,8 +98,16 @@ function loadDaemonStatusGatherModule() { |
97 | 98 | return daemonStatusGatherModuleLoader.load(); |
98 | 99 | } |
99 | 100 |
|
100 | | -function runGatewayCommand(action: () => Promise<void>, label?: string) { |
| 101 | +function runGatewayCommand(action: () => Promise<void>, label?: string, opts?: { json?: boolean }) { |
101 | 102 | return runCommandWithRuntime(defaultRuntime, action, (err) => { |
| 103 | + if (opts?.json) { |
| 104 | + const payload = formatGatewayTransportErrorJson(err); |
| 105 | + if (payload) { |
| 106 | + defaultRuntime.writeJson(payload); |
| 107 | + defaultRuntime.exit(1); |
| 108 | + return; |
| 109 | + } |
| 110 | + } |
102 | 111 | const message = String(err); |
103 | 112 | defaultRuntime.error(label ? `${label}: ${message}` : message); |
104 | 113 | defaultRuntime.exit(1); |
@@ -459,30 +468,34 @@ export function registerGatewayCli(program: Command) { |
459 | 468 | .command("health") |
460 | 469 | .description("Fetch Gateway health") |
461 | 470 | .action(async (opts, command) => { |
462 | | - await runGatewayCommand(async () => { |
463 | | - const rpcOpts = resolveGatewayRpcOptions(opts, command); |
464 | | - const [{ formatHealthChannelLines }, { styleHealthChannelLine }] = await Promise.all([ |
465 | | - loadGatewayHealthModule(), |
466 | | - loadHealthStyleModule(), |
467 | | - ]); |
468 | | - const result = await callGatewayCli("health", rpcOpts); |
469 | | - if (rpcOpts.json) { |
470 | | - defaultRuntime.writeJson(result); |
471 | | - return; |
472 | | - } |
473 | | - const rich = isRich(); |
474 | | - const obj: Record<string, unknown> = result && typeof result === "object" ? result : {}; |
475 | | - const durationMs = typeof obj.durationMs === "number" ? obj.durationMs : null; |
476 | | - defaultRuntime.log(colorize(rich, theme.heading, "Gateway Health")); |
477 | | - defaultRuntime.log( |
478 | | - `${colorize(rich, theme.success, "OK")}${durationMs != null ? ` (${durationMs}ms)` : ""}`, |
479 | | - ); |
480 | | - if (obj.channels && typeof obj.channels === "object") { |
481 | | - for (const line of formatHealthChannelLines(obj as HealthSummary)) { |
482 | | - defaultRuntime.log(styleHealthChannelLine(line, rich)); |
| 471 | + await runGatewayCommand( |
| 472 | + async () => { |
| 473 | + const rpcOpts = resolveGatewayRpcOptions(opts, command); |
| 474 | + const [{ formatHealthChannelLines }, { styleHealthChannelLine }] = await Promise.all([ |
| 475 | + loadGatewayHealthModule(), |
| 476 | + loadHealthStyleModule(), |
| 477 | + ]); |
| 478 | + const result = await callGatewayCli("health", rpcOpts); |
| 479 | + if (rpcOpts.json) { |
| 480 | + defaultRuntime.writeJson(result); |
| 481 | + return; |
483 | 482 | } |
484 | | - } |
485 | | - }); |
| 483 | + const rich = isRich(); |
| 484 | + const obj: Record<string, unknown> = result && typeof result === "object" ? result : {}; |
| 485 | + const durationMs = typeof obj.durationMs === "number" ? obj.durationMs : null; |
| 486 | + defaultRuntime.log(colorize(rich, theme.heading, "Gateway Health")); |
| 487 | + defaultRuntime.log( |
| 488 | + `${colorize(rich, theme.success, "OK")}${durationMs != null ? ` (${durationMs}ms)` : ""}`, |
| 489 | + ); |
| 490 | + if (obj.channels && typeof obj.channels === "object") { |
| 491 | + for (const line of formatHealthChannelLines(obj as HealthSummary)) { |
| 492 | + defaultRuntime.log(styleHealthChannelLine(line, rich)); |
| 493 | + } |
| 494 | + } |
| 495 | + }, |
| 496 | + undefined, |
| 497 | + { json: Boolean(opts.json) }, |
| 498 | + ); |
486 | 499 | }), |
487 | 500 | ); |
488 | 501 |
|
|
0 commit comments