refactor: consolidate nodes cli error handling

This commit is contained in:
Peter Steinberger committed 2026-01-19 00:52:31 +00:00
1 parent 1fec41b3df
commit 83511c0c09
9 files changed
+65 -112

No files matched your search

+9 -20
View File
@@ -1,6 +1,7 @@
import type { Command } from "commander";
import { defaultRuntime } from "../../runtime.js";
import { formatAge, parsePairingList } from "./format.js";
import { runNodesCommand } from "./cli-utils.js";
import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js";
import type { NodesRpcOpts } from "./types.js";
@@ -10,7 +11,7 @@ export function registerNodesPairingCommands(nodes: Command) {
.command("pending")
.description("List pending pairing requests")
.action(async (opts: NodesRpcOpts) => {
try {
await runNodesCommand("pending", async () => {
const result = (await callGatewayCli("node.pair.list", opts, {})) as unknown;
const { pending } = parsePairingList(result);
if (opts.json) {
@@ -28,10 +29,7 @@ export function registerNodesPairingCommands(nodes: Command) {
const age = typeof r.ts === "number" ? ` · ${formatAge(Date.now() - r.ts)} ago` : "";
defaultRuntime.log(`- ${r.requestId}: ${name}${repair}${ip}${age}`);
}
} catch (err) {
defaultRuntime.error(`nodes pending failed: ${String(err)}`);
defaultRuntime.exit(1);
}
});
}),
);
@@ -41,15 +39,12 @@ export function registerNodesPairingCommands(nodes: Command) {
.description("Approve a pending pairing request")
.argument("<requestId>", "Pending request id")
.action(async (requestId: string, opts: NodesRpcOpts) => {
try {
await runNodesCommand("approve", async () => {
const result = await callGatewayCli("node.pair.approve", opts, {
requestId,
});
defaultRuntime.log(JSON.stringify(result, null, 2));
} catch (err) {
defaultRuntime.error(`nodes approve failed: ${String(err)}`);
defaultRuntime.exit(1);
}
});
}),
);
@@ -59,15 +54,12 @@ export function registerNodesPairingCommands(nodes: Command) {
.description("Reject a pending pairing request")
.argument("<requestId>", "Pending request id")
.action(async (requestId: string, opts: NodesRpcOpts) => {
try {
await runNodesCommand("reject", async () => {
const result = await callGatewayCli("node.pair.reject", opts, {
requestId,
});
defaultRuntime.log(JSON.stringify(result, null, 2));
} catch (err) {
defaultRuntime.error(`nodes reject failed: ${String(err)}`);
defaultRuntime.exit(1);
}
});
}),
);
@@ -78,7 +70,7 @@ export function registerNodesPairingCommands(nodes: Command) {
.requiredOption("--node <idOrNameOrIp>", "Node id, name, or IP")
.requiredOption("--name <displayName>", "New display name")
.action(async (opts: NodesRpcOpts) => {
try {
await runNodesCommand("rename", async () => {
const nodeId = await resolveNodeId(opts, String(opts.node ?? ""));
const name = String(opts.name ?? "").trim();
if (!nodeId || !name) {
@@ -95,10 +87,7 @@ export function registerNodesPairingCommands(nodes: Command) {
return;
}
defaultRuntime.log(`node rename ok: ${nodeId} -> ${name}`);
} catch (err) {
defaultRuntime.error(`nodes rename failed: ${String(err)}`);
defaultRuntime.exit(1);
}
});
}),
);
}