mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
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:
1 parent
35f9609865
commit
10beffe167
23 files changed
+642
-269
No files matched your search
@@ -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");
|
||||
|
||||
@@ -10,7 +10,7 @@ import events from "node:events";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { LineSplitter, logger } from "@clusterio/lib";
|
||||
|
||||
import { selectTargetCommand, initialize as initializeCtl } from "@clusterio/ctl";
|
||||
import { runCommand, initialize as initializeCtl } from "@clusterio/ctl";
|
||||
import { _listFactorioVersions } from "@clusterio/host/dist/node/src/server.js";
|
||||
import { pairHost } from "@clusterio/host";
|
||||
|
||||
@@ -242,8 +242,7 @@ export async function execCtl(command) {
|
||||
}
|
||||
|
||||
control.config = initArgs.ctlConfig;
|
||||
const targetCommand = selectTargetCommand(initArgs.args, initArgs.rootCommands);
|
||||
await targetCommand.run(initArgs.args, control);
|
||||
await runCommand(initArgs.args, initArgs.rootCommands, control);
|
||||
} finally {
|
||||
process.chdir("../..");
|
||||
inExecCtl = false;
|
||||
|
||||
Reference in new issue
Block a user