import type { Command } from "commander"; import { inheritOptionFromParent } from "../command-options.js"; import { runDaemonInstall, runDaemonRestart, runDaemonStart, runDaemonStatus, runDaemonStop, runDaemonUninstall, } from "./runners.js"; import type { DaemonInstallOptions, GatewayRpcOpts } from "./types.js"; function resolveInstallOptions( cmdOpts: DaemonInstallOptions, command?: Command, ): DaemonInstallOptions { const parentForce = inheritOptionFromParent(command, "force"); const parentPort = inheritOptionFromParent(command, "port"); const parentToken = inheritOptionFromParent(command, "token"); return { ...cmdOpts, force: Boolean(cmdOpts.force || parentForce), port: cmdOpts.port ?? parentPort, token: cmdOpts.token ?? parentToken, }; } function resolveRpcOptions(cmdOpts: GatewayRpcOpts, command?: Command): GatewayRpcOpts { const parentToken = inheritOptionFromParent(command, "token"); const parentPassword = inheritOptionFromParent(command, "password"); return { ...cmdOpts, token: cmdOpts.token ?? parentToken, password: cmdOpts.password ?? parentPassword, }; } export function addGatewayServiceCommands(parent: Command, opts?: { statusDescription?: string }) { parent .command("status") .description(opts?.statusDescription ?? "Show gateway service status + probe the Gateway") .option("--url ", "Gateway WebSocket URL (defaults to config/remote/local)") .option("--token ", "Gateway token (if required)") .option("--password ", "Gateway password (password auth)") .option("--timeout ", "Timeout in ms", "10000") .option("--no-probe", "Skip RPC probe") .option("--require-rpc", "Exit non-zero when the RPC probe fails", false) .option("--deep", "Scan system-level services", false) .option("--json", "Output JSON", false) .action(async (cmdOpts, command) => { await runDaemonStatus({ rpc: resolveRpcOptions(cmdOpts, command), probe: Boolean(cmdOpts.probe), requireRpc: Boolean(cmdOpts.requireRpc), deep: Boolean(cmdOpts.deep), json: Boolean(cmdOpts.json), }); }); parent .command("install") .description("Install the Gateway service (launchd/systemd/schtasks)") .option("--port ", "Gateway port") .option("--runtime ", "Daemon runtime (node|bun). Default: node") .option("--token ", "Gateway token (token auth)") .option("--force", "Reinstall/overwrite if already installed", false) .option("--json", "Output JSON", false) .action(async (cmdOpts, command) => { await runDaemonInstall(resolveInstallOptions(cmdOpts, command)); }); parent .command("uninstall") .description("Uninstall the Gateway service (launchd/systemd/schtasks)") .option("--json", "Output JSON", false) .action(async (cmdOpts) => { await runDaemonUninstall(cmdOpts); }); parent .command("start") .description("Start the Gateway service (launchd/systemd/schtasks)") .option("--json", "Output JSON", false) .action(async (cmdOpts) => { await runDaemonStart(cmdOpts); }); parent .command("stop") .description("Stop the Gateway service (launchd/systemd/schtasks)") .option("--json", "Output JSON", false) .action(async (cmdOpts) => { await runDaemonStop(cmdOpts); }); parent .command("restart") .description("Restart the Gateway service (launchd/systemd/schtasks)") .option("--json", "Output JSON", false) .action(async (cmdOpts) => { await runDaemonRestart(cmdOpts); }); }