mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Merge pull request #1230 from bbassie/feature/ctl-output-json
Add --output json to clusterioctl
This commit is contained in:
26 files changed
+1048
-347
No files matched your search
+51
-26
@@ -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.human) {
|
||||
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,24 +53,37 @@ 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 (!playerData) {
|
||||
print(`No player data stored for ${args.player}`);
|
||||
return;
|
||||
}
|
||||
print(`generation: ${playerData.generation}`);
|
||||
print(`size: ${lib.formatBytes(JSON.stringify(playerData).length)}`);
|
||||
print(`controller: ${playerData.controller}`);
|
||||
print(`force: ${playerData.force}`);
|
||||
for (let inventory of summarizePlayerInventories(playerData)) {
|
||||
print(`${inventory.name}:`);
|
||||
for (let item of inventory.items) {
|
||||
let quality = item.quality ? ` (${item.quality})` : "";
|
||||
let size = item.exportSize ? ` ${lib.formatBytes(item.exportSize)}` : "";
|
||||
print(` ${item.count} ${item.name}${quality}${size}`);
|
||||
const summary = {
|
||||
instanceId: response.instanceId,
|
||||
...playerData && {
|
||||
generation: playerData.generation,
|
||||
size: JSON.stringify(playerData).length,
|
||||
controller: playerData.controller,
|
||||
force: playerData.force,
|
||||
inventories: summarizePlayerInventories(playerData),
|
||||
},
|
||||
};
|
||||
const acquiredBy = ctl.output.human ? await instanceName(ctl, response.instanceId) : "";
|
||||
ctl.output.result(summary, () => {
|
||||
print(`acquired by: ${acquiredBy}`);
|
||||
if (!playerData) {
|
||||
print(`No player data stored for ${args.player}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
print(`generation: ${summary.generation}`);
|
||||
print(`size: ${lib.formatBytes(summary.size!)}`);
|
||||
print(`controller: ${summary.controller}`);
|
||||
print(`force: ${summary.force}`);
|
||||
for (let inventory of summary.inventories!) {
|
||||
print(`${inventory.name}:`);
|
||||
for (let item of inventory.items) {
|
||||
let quality = item.quality ? ` (${item.quality})` : "";
|
||||
let size = item.exportSize ? ` ${lib.formatBytes(item.exportSize)}` : "";
|
||||
print(` ${item.count} ${item.name}${quality}${size}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -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}`),
|
||||
);
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -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";
|
||||
@@ -303,6 +303,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);
|
||||
@@ -331,6 +332,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, () => [
|
||||
@@ -360,10 +362,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() {
|
||||
@@ -384,7 +388,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");
|
||||
@@ -421,6 +425,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" });
|
||||
|
||||
Reference in new issue
Block a user