refactor(cli): dedupe system gateway action handling

This commit is contained in:
Peter Steinberger
2026-02-21 20:21:24 +00:00
parent a04cdc0390
commit 84686db850
2 changed files with 138 additions and 48 deletions

View File

@@ -7,6 +7,7 @@ import type { GatewayRpcOpts } from "./gateway-rpc.js";
import { addGatewayClientOptions, callGatewayFromCli } from "./gateway-rpc.js";
type SystemEventOpts = GatewayRpcOpts & { text?: string; mode?: string; json?: boolean };
type SystemGatewayOpts = GatewayRpcOpts & { json?: boolean };
const normalizeWakeMode = (raw: unknown) => {
const mode = typeof raw === "string" ? raw.trim() : "";
@@ -19,6 +20,24 @@ const normalizeWakeMode = (raw: unknown) => {
throw new Error("--mode must be now or next-heartbeat");
};
async function runSystemGatewayCommand(
opts: SystemGatewayOpts,
action: () => Promise<unknown>,
successText?: string,
): Promise<void> {
try {
const result = await action();
if (opts.json || successText === undefined) {
defaultRuntime.log(JSON.stringify(result, null, 2));
} else {
defaultRuntime.log(successText);
}
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
}
export function registerSystemCli(program: Command) {
const system = program
.command("system")
@@ -37,22 +56,18 @@ export function registerSystemCli(program: Command) {
.option("--mode <mode>", "Wake mode (now|next-heartbeat)", "next-heartbeat")
.option("--json", "Output JSON", false),
).action(async (opts: SystemEventOpts) => {
try {
const text = typeof opts.text === "string" ? opts.text.trim() : "";
if (!text) {
throw new Error("--text is required");
}
const mode = normalizeWakeMode(opts.mode);
const result = await callGatewayFromCli("wake", opts, { mode, text }, { expectFinal: false });
if (opts.json) {
defaultRuntime.log(JSON.stringify(result, null, 2));
} else {
defaultRuntime.log("ok");
}
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
await runSystemGatewayCommand(
opts,
async () => {
const text = typeof opts.text === "string" ? opts.text.trim() : "";
if (!text) {
throw new Error("--text is required");
}
const mode = normalizeWakeMode(opts.mode);
return await callGatewayFromCli("wake", opts, { mode, text }, { expectFinal: false });
},
"ok",
);
});
const heartbeat = system.command("heartbeat").description("Heartbeat controls");
@@ -62,16 +77,12 @@ export function registerSystemCli(program: Command) {
.command("last")
.description("Show the last heartbeat event")
.option("--json", "Output JSON", false),
).action(async (opts: GatewayRpcOpts & { json?: boolean }) => {
try {
const result = await callGatewayFromCli("last-heartbeat", opts, undefined, {
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli("last-heartbeat", opts, undefined, {
expectFinal: false,
});
defaultRuntime.log(JSON.stringify(result, null, 2));
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
});
});
addGatewayClientOptions(
@@ -79,19 +90,15 @@ export function registerSystemCli(program: Command) {
.command("enable")
.description("Enable heartbeats")
.option("--json", "Output JSON", false),
).action(async (opts: GatewayRpcOpts & { json?: boolean }) => {
try {
const result = await callGatewayFromCli(
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli(
"set-heartbeats",
opts,
{ enabled: true },
{ expectFinal: false },
);
defaultRuntime.log(JSON.stringify(result, null, 2));
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
});
});
addGatewayClientOptions(
@@ -99,19 +106,15 @@ export function registerSystemCli(program: Command) {
.command("disable")
.description("Disable heartbeats")
.option("--json", "Output JSON", false),
).action(async (opts: GatewayRpcOpts & { json?: boolean }) => {
try {
const result = await callGatewayFromCli(
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli(
"set-heartbeats",
opts,
{ enabled: false },
{ expectFinal: false },
);
defaultRuntime.log(JSON.stringify(result, null, 2));
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
});
});
addGatewayClientOptions(
@@ -119,15 +122,11 @@ export function registerSystemCli(program: Command) {
.command("presence")
.description("List system presence entries")
.option("--json", "Output JSON", false),
).action(async (opts: GatewayRpcOpts & { json?: boolean }) => {
try {
const result = await callGatewayFromCli("system-presence", opts, undefined, {
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli("system-presence", opts, undefined, {
expectFinal: false,
});
defaultRuntime.log(JSON.stringify(result, null, 2));
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
});
});
}