Add --output json to clusterioctl

Commands print their results through a CommandOutput on the ctl link.
In json mode lists print as arrays of objects, other results as an
object, commands without a result print {} and failures print
{ error: ClassName, message } with exit code 1. log follow and
debug dump-ws print JSON lines.

Human output now goes through the shared asTable printer in every
command, results that were logged to stderr (role create, save and mod
upload/download) print to stdout, and config edit failures are logged
instead of printed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-18 13:07:18 +00:00
1 parent 35f9609865
commit 10beffe167
23 files changed
+642 -269

No files matched your search

+35
View File
@@ -2317,6 +2317,41 @@ describe("Integration of Clusterio", function() {
});
});
describe("--output json", function() {
it("prints lists as an array of objects", async function() {
const roles = JSON.parse(await execCtlOutput("--output json role list"));
assert.deepEqual(roles.find(role => role.id === 0).permissions, ["core.admin"]);
});
it("prints single results as an object", async function() {
const { id } = JSON.parse(await execCtlOutput("--output json role create json-role"));
const roles = await getControl().send(new lib.RoleListRequest());
assert.equal(roles.find(role => role.id === id)?.name, "json-role");
assert.equal(await execCtlOutput(`--output json role delete ${id}`), "{}");
});
it("prints log queries as an array", async function() {
const log = JSON.parse(await execCtlOutput("--output json log query --controller --limit 1"));
assert.equal(log.length, 1);
assert.equal(typeof log[0].message, "string");
});
it("prints errors as JSON with a non-zero exit code", async function() {
await assert.rejects(
execCtlProcess("--output json instance stop no-such-instance"),
err => {
assert.equal(err.code, 1);
assert.deepEqual(
JSON.parse(err.stdout),
{ error: "CommandError", message: "No instance named no-such-instance" },
);
return true;
},
);
await assert.rejects(
execCtlProcess("--output json user set-admin no-such-user"),
err => JSON.parse(err.stdout).error === "NotFoundError",
);
});
});
describe("user list", function() {
it("prints roles and instances as lists", async function() {
const output = await execCtlOutput("user list");