Skip to content

Commit 7769cb6

Browse files
authored
fix(cli): accept daemon --json before subcommands (#126447)
* fix(cli): accept daemon json before subcommands * test(cli): update doctor public-surface fixture * test(cli): classify daemon parent JSON output
1 parent e00f860 commit 7769cb6

6 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎docs/cli/daemon.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ openclaw daemon uninstall
2929
| `install` | `--port`, `--runtime <node>`, `--token`, `--wrapper <path>`, `--force`, `--json` |
3030
| `uninstall` | `--json` |
3131
| `start` | `--json` |
32-
| `stop` | `--json`, `--disable` (launchd only: persistently suppress KeepAlive/RunAtLoad until next start) |
32+
| `stop` | `--force`, `--json`, `--disable` (launchd only: suppress KeepAlive/RunAtLoad until next start) |
3333
| `restart` | `--force`, `--safe`, `--skip-deferral`, `--wait <duration>`, `--json` |
3434

35+
`--json` is accepted before or after every subcommand (for example, `daemon --json status` and `daemon status --json`).
36+
3537
- `status`: shows service install state (launchd/systemd/schtasks) and probes Gateway health.
3638
- `install`: installs the service; `--force` reinstalls/overwrites an existing install.
3739
- `restart --safe`: asks the running Gateway to preflight active work and schedule one coalesced restart after work drains, bounded to 5 minutes. When that budget expires, the restart is forced anyway. Plain `restart` uses the service manager directly; `--force` is the immediate override.

‎src/cli/daemon-cli/register-service-commands.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { Command } from "commander";
33
import { beforeEach, describe, expect, it, vi } from "vitest";
44
import { addGatewayServiceCommands } from "./register-service-commands.js";
5+
import { registerDaemonCli } from "./register.js";
56

67
const runDaemonInstall = vi.fn(async (_opts: unknown) => {});
78
const runDaemonRestart = vi.fn(async (_opts: unknown) => {});
@@ -121,4 +122,25 @@ describe("addGatewayServiceCommands", () => {
121122
await gateway.parseAsync(argv, { from: "user" });
122123
assert();
123124
});
125+
126+
it.each(
127+
[
128+
{ leaf: "status", runner: runDaemonStatus },
129+
{ leaf: "install", runner: runDaemonInstall },
130+
{ leaf: "uninstall", runner: runDaemonUninstall },
131+
{ leaf: "start", runner: runDaemonStart },
132+
{ leaf: "stop", runner: runDaemonStop },
133+
{ leaf: "restart", runner: runDaemonRestart },
134+
].flatMap(({ leaf, runner }) => [
135+
{ name: `daemon --json ${leaf}`, argv: ["daemon", "--json", leaf], runner },
136+
{ name: `daemon ${leaf} --json`, argv: ["daemon", leaf, "--json"], runner },
137+
]),
138+
)("forwards JSON mode for $name", async ({ argv, runner }) => {
139+
const program = new Command().enablePositionalOptions().exitOverride();
140+
registerDaemonCli(program);
141+
142+
await program.parseAsync(argv, { from: "user" });
143+
144+
expect(expectSingleDaemonCall(runner).json).toBe(true);
145+
});
124146
});

‎src/cli/daemon-cli/register-service-commands.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ function loadDaemonStatusModule() {
2020
return daemonStatusModuleLoader.load();
2121
}
2222

23+
function resolveJsonOption(cmdOpts: { json?: boolean }, command?: Command): boolean {
24+
const parentJson = inheritOptionFromParent<boolean>(command, "json", "cli");
25+
return Boolean(cmdOpts.json || parentJson);
26+
}
27+
2328
function resolveInstallOptions(
2429
cmdOpts: DaemonInstallOptions,
2530
command?: Command,
@@ -32,6 +37,7 @@ function resolveInstallOptions(
3237
force: Boolean(cmdOpts.force || parentForce),
3338
port: cmdOpts.port ?? parentPort,
3439
token: cmdOpts.token ?? parentToken,
40+
json: resolveJsonOption(cmdOpts, command),
3541
};
3642
}
3743

@@ -51,6 +57,7 @@ function resolveRestartOptions(cmdOpts: DaemonLifecycleOptions, command?: Comman
5157
...cmdOpts,
5258
force: Boolean(cmdOpts.force || parentForce),
5359
safe: Boolean(cmdOpts.safe),
60+
json: resolveJsonOption(cmdOpts, command),
5461
};
5562
}
5663

@@ -59,6 +66,7 @@ function resolveStopOptions(cmdOpts: DaemonLifecycleOptions, command?: Command)
5966
return {
6067
...cmdOpts,
6168
force: Boolean(cmdOpts.force || parentForce),
69+
json: resolveJsonOption(cmdOpts, command),
6270
};
6371
}
6472

@@ -84,7 +92,7 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
8492
probe: Boolean(cmdOpts.probe),
8593
requireRpc: Boolean(cmdOpts.requireRpc),
8694
deep: Boolean(cmdOpts.deep),
87-
json: Boolean(cmdOpts.json),
95+
json: resolveJsonOption(cmdOpts, command),
8896
});
8997
});
9098

@@ -106,18 +114,18 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
106114
.command("uninstall")
107115
.description("Uninstall the Gateway service (launchd/systemd/schtasks)")
108116
.option("--json", "Output JSON", false)
109-
.action(async (cmdOpts) => {
117+
.action(async (cmdOpts, command) => {
110118
const { runDaemonUninstall } = await loadDaemonLifecycleModule();
111-
await runDaemonUninstall(cmdOpts);
119+
await runDaemonUninstall({ ...cmdOpts, json: resolveJsonOption(cmdOpts, command) });
112120
});
113121

114122
parent
115123
.command("start")
116124
.description("Start the Gateway service (launchd/systemd/schtasks)")
117125
.option("--json", "Output JSON", false)
118-
.action(async (cmdOpts) => {
126+
.action(async (cmdOpts, command) => {
119127
const { runDaemonStart } = await loadDaemonLifecycleModule();
120-
await runDaemonStart(cmdOpts);
128+
await runDaemonStart({ ...cmdOpts, json: resolveJsonOption(cmdOpts, command) });
121129
});
122130

123131
parent

‎src/cli/daemon-cli/register.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function registerDaemonCli(program: Command) {
99
const daemon = program
1010
.command("daemon")
1111
.description("Manage the Gateway service (launchd/systemd/schtasks)")
12+
.option("--json", "Output JSON", false)
1213
.addHelpText(
1314
"after",
1415
() =>

‎src/cli/program/root-command-descriptions.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const JSON_NOT_APPLICABLE = {
4747
"transcripts",
4848
"gateway restart-handoff",
4949
"gateway diagnostics",
50-
"daemon",
5150
"system",
5251
"system heartbeat",
5352
"promos",

‎test/cli-json-stdout.e2e.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ describe("cli json stdout contract", () => {
530530
const memoryCoreDir = path.join(bundledPluginsDir, "memory-core");
531531
await fs.mkdir(memoryCoreDir, { recursive: true });
532532
await fs.writeFile(
533-
path.join(memoryCoreDir, "api.js"),
533+
path.join(memoryCoreDir, "doctor-health-api.js"),
534534
[
535535
"export function registerMemoryCoreDoctorChecks(host) {",
536536
" host.registerHealthCheck({",

0 commit comments

Comments
 (0)