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

+27 -6
View File
@@ -4,7 +4,7 @@ import os from "node:os";
import path from "node:path";
import * as lib from "@clusterio/lib";
import { CtlHooks } from "@clusterio/ctl";
import { CommandOutput, CtlHooks } from "@clusterio/ctl";
import * as mock from "../../../test/mock.js";
import { testRoundTripJsonSerialisable, testMatrix } from "../../../test/common.js";
@@ -295,6 +295,7 @@ describe("player_sync plugin", function() {
let printed;
let responses;
const ctl = {
output: new CommandOutput(),
async send(message) {
sent.push(message);
const handler = responses.get(message.constructor);
@@ -323,6 +324,7 @@ describe("player_sync plugin", function() {
beforeEach(function() {
sent = [];
printed = [];
ctl.output = new CommandOutput();
responses = new Map([
[lib.InstanceDetailsGetRequest, request => ({ id: request.instanceId, name: "Test" })],
[msg.ListPlayersRequest, () => [
@@ -352,10 +354,12 @@ describe("player_sync plugin", function() {
it("list should print sorted entries", async function() {
await run("list");
assert.deepEqual(printed, [
"name | generation | size | acquired by",
`alice | 2 | ${lib.formatBytes(100)} | -`,
"bob | - | - | Test (1)",
const lines = printed.join("\n").split("\n").map(line => line.trimEnd());
assert.deepEqual(lines, [
"name | generation | size | acquired by",
"--------------------------------------------",
`alice | 2 | ${lib.formatBytes(100)} | -`,
"bob | - | - | Test (1)",
]);
});
it("show should print a summary", async function() {
@@ -376,7 +380,7 @@ describe("player_sync plugin", function() {
throw new lib.PermissionError("Permission denied");
});
await run("list");
assert.equal(printed[2], "bob | - | - | 1");
assert.match(printed[0], /bob +\| - +\| - +\| 1/);
printed = [];
await run("show", { player: "alice" });
assert.equal(printed[0], "acquired by: 1");
@@ -413,6 +417,23 @@ describe("player_sync plugin", function() {
new lib.CommandError("Player data must be a JSON object"),
);
});
it("should print JSON in json mode", async function() {
ctl.output = new CommandOutput("json");
await run("list");
assert.deepEqual(JSON.parse(printed[0]), [
{ name: "alice", generation: 2, size: 100 },
{ name: "bob", instanceId: 1 },
]);
printed = [];
await run("show", { player: "alice" });
const shown = JSON.parse(printed[0]);
assert.equal(shown.instanceId, 1);
assert.equal(shown.generation, 2);
assert.deepEqual(shown.inventories[0].items[0], { name: "wood", count: 5, quality: "rare" });
printed = [];
await run("export", { player: "alice" });
assert.deepEqual(JSON.parse(printed[0]), storedData);
});
it("delete and release should send requests", async function() {
await run("delete", { player: "alice" });
await run("release", { player: "alice" });