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

+36 -11
View File
@@ -30,14 +30,20 @@ playerSyncCommands.add(new lib.Command({
handler: async function(args: object, ctl: Ctl) {
let players = await ctl.send(new msg.ListPlayersRequest());
players.sort((a, b) => a.name.localeCompare(b.name));
print("name | generation | size | acquired by");
for (let player of players) {
print(
`${player.name} | ${player.generation ?? "-"} | ` +
`${player.size === undefined ? "-" : lib.formatBytes(player.size)} | ` +
`${await instanceName(ctl, player.instanceId)}`
);
if (ctl.output.json) {
ctl.output.result(players);
return;
}
let rows = [];
for (let player of players) {
rows.push({
name: player.name,
generation: player.generation ?? "-",
size: player.size === undefined ? "-" : lib.formatBytes(player.size),
"acquired by": await instanceName(ctl, player.instanceId),
});
}
ctl.output.table(rows);
},
}));
@@ -47,8 +53,21 @@ playerSyncCommands.add(new lib.Command({
}],
handler: async function(args: { player: string }, ctl: Ctl) {
let response = await ctl.send(new msg.GetPlayerDataRequest(args.player));
print(`acquired by: ${await instanceName(ctl, response.instanceId)}`);
let playerData = response.playerData;
if (ctl.output.json) {
ctl.output.result({
instanceId: response.instanceId,
...playerData && {
generation: playerData.generation,
size: JSON.stringify(playerData).length,
controller: playerData.controller,
force: playerData.force,
inventories: summarizePlayerInventories(playerData),
},
});
return;
}
print(`acquired by: ${await instanceName(ctl, response.instanceId)}`);
if (!playerData) {
print(`No player data stored for ${args.player}`);
return;
@@ -81,9 +100,12 @@ playerSyncCommands.add(new lib.Command({
let content = JSON.stringify(response.playerData, null, "\t");
if (args.file) {
await fs.writeFile(args.file, content);
print(`Wrote player data for ${args.player} to ${args.file}`);
ctl.output.result(
{ file: args.file },
() => print(`Wrote player data for ${args.player} to ${args.file}`),
);
} else {
print(content);
ctl.output.result(response.playerData, () => print(content));
}
},
}));
@@ -101,7 +123,10 @@ playerSyncCommands.add(new lib.Command({
playerData.name = args.player;
playerData.generation ??= 0;
let response = await ctl.send(new msg.SetPlayerDataRequest(args.player, playerData));
print(`Stored player data for ${args.player} as generation ${response.generation}`);
ctl.output.result(
{ generation: response.generation },
() => print(`Stored player data for ${args.player} as generation ${response.generation}`),
);
},
}));
+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" });