mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Cover ctl output paths and stop deleting fields before printing
user show, mod list, mod search and mod-pack list deleted fields off the received objects to trim the table, which made their toJSON throw in wire mode. They now pick the fields for the human table only and print the whole objects in json and wire mode. Add integration cases for config edit on the controller, a host and an instance, user show with instance stats, log query and host pending in human mode, mod-pack show with mods and settings, and wire output of the trimmed lists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
23187859d6
commit
f49c633e77
5 files changed
+88
-55
No files matched your search
@@ -14,6 +14,14 @@ function formatCell(value: unknown) {
|
||||
|
||||
export const asTable = asTableModule.configure({ delimiter: " | ", print: formatCell });
|
||||
|
||||
/** Copy of object with only the given fields, or all of them if fields includes "all" */
|
||||
export function pickFields(object: object, fields: string[]) {
|
||||
if (fields.includes("all")) {
|
||||
return object;
|
||||
}
|
||||
return Object.fromEntries(Object.entries(object).filter(([field]) => fields.includes(field)));
|
||||
}
|
||||
|
||||
/** Convert class instances, Sets and Maps into plain JSON values, ignoring wire format toJSON methods */
|
||||
export function toPlain(value: unknown): unknown {
|
||||
if (value instanceof Set || value instanceof Array) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import util from "util";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { logger } from "@clusterio/lib";
|
||||
import type { Ctl } from "../ctl.js";
|
||||
import { asTable, print } from "./command_ops.js";
|
||||
import { asTable, pickFields, print } from "./command_ops.js";
|
||||
const finished = util.promisify(stream.finished);
|
||||
|
||||
export const modCommands = new lib.CommandTree({ name: "mod", description: "Manage uploaded mods" });
|
||||
@@ -49,17 +49,7 @@ modCommands.add(new lib.Command({
|
||||
}],
|
||||
handler: async function(args: { fields: string[] }, ctl: Ctl) {
|
||||
let mods = await ctl.send(new lib.ModListRequest());
|
||||
if (!args.fields.includes("all")) {
|
||||
for (let entry of mods) {
|
||||
for (let field of Object.keys(entry)) {
|
||||
if (!args.fields.includes(field)) {
|
||||
// @ts-expect-error terrible hack
|
||||
delete entry[field];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ctl.output.table(mods);
|
||||
ctl.output.table(mods, mod => pickFields(mod, args.fields));
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -120,16 +110,6 @@ modCommands.add(new lib.Command({
|
||||
args.sortOrder
|
||||
));
|
||||
let results = response.results.flatMap(result => result.versions);
|
||||
if (!args.fields.includes("all")) {
|
||||
for (let entry of results) {
|
||||
for (let field of Object.keys(entry)) {
|
||||
if (!args.fields.includes(field)) {
|
||||
// @ts-expect-error terrible hack
|
||||
delete entry[field];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ctl.output.result({
|
||||
page: args.page,
|
||||
pageCount: response.pageCount,
|
||||
@@ -141,7 +121,7 @@ modCommands.add(new lib.Command({
|
||||
print(issue);
|
||||
}
|
||||
print(`page ${args.page} of ${response.pageCount} (${response.resultCount} results)`);
|
||||
print(asTable(results));
|
||||
print(asTable(results.map(mod => pickFields(mod, args.fields))));
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { logger } from "@clusterio/lib";
|
||||
import type { Ctl } from "../ctl.js";
|
||||
import { print } from "./command_ops.js";
|
||||
import { pickFields, print } from "./command_ops.js";
|
||||
|
||||
export const modPackCommands = new lib.CommandTree({ name: "mod-pack", description: "Mod Pack" });
|
||||
modPackCommands.add(new lib.Command({
|
||||
@@ -58,16 +58,7 @@ modPackCommands.add(new lib.Command({
|
||||
definition: [["list", "l"], "List mod packs in the cluster"],
|
||||
handler: async function(args: object, ctl: Ctl) {
|
||||
let modPacks = await ctl.send(new lib.ModPackListRequest());
|
||||
let fields = ["id", "name", "factorioVersion"];
|
||||
for (let entry of modPacks) {
|
||||
for (let field of Object.keys(entry)) {
|
||||
if (!fields.includes(field)) {
|
||||
// @ts-expect-error terrible hack
|
||||
delete entry[field];
|
||||
}
|
||||
}
|
||||
}
|
||||
ctl.output.table(modPacks);
|
||||
ctl.output.table(modPacks, modPack => pickFields(modPack, ["id", "name", "factorioVersion"]));
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -18,27 +18,18 @@ userCommands.add(new lib.Command({
|
||||
});
|
||||
}],
|
||||
handler: async function(args: { name: string, instanceStats: boolean }, ctl: Ctl) {
|
||||
let user = await ctl.send(new lib.UserGetRequest(args.name));
|
||||
Object.assign(user, user.playerStats);
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.playerStats;
|
||||
let instanceStats = user.instanceStats;
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.instanceStats;
|
||||
if (!args.instanceStats) {
|
||||
ctl.output.result(user, () => printProperties(user));
|
||||
return;
|
||||
}
|
||||
|
||||
let instances = await ctl.send(new lib.InstanceDetailsListRequest());
|
||||
const stats = [...instanceStats || []].map(([id, playerInstanceStats]) => ({
|
||||
id,
|
||||
name: instances.find(i => i.id === id)?.name ?? "<deleted>",
|
||||
stats: playerInstanceStats,
|
||||
}));
|
||||
ctl.output.result({ ...user, instanceStats: stats }, () => {
|
||||
printProperties(user);
|
||||
for (let { id, name, stats: playerInstanceStats } of stats) {
|
||||
const user = await ctl.send(new lib.UserGetRequest(args.name));
|
||||
const { playerStats, instanceStats, ...properties } = user;
|
||||
const instances = args.instanceStats && ctl.output.human
|
||||
? await ctl.send(new lib.InstanceDetailsListRequest())
|
||||
: [];
|
||||
ctl.output.result(user, () => {
|
||||
printProperties({ ...properties, ...playerStats });
|
||||
if (!args.instanceStats) {
|
||||
return;
|
||||
}
|
||||
for (let [id, playerInstanceStats] of instanceStats) {
|
||||
const name = instances.find(i => i.id === id)?.name ?? "<deleted>";
|
||||
print();
|
||||
print(`Instance ${name} (${id}):`);
|
||||
printProperties(playerInstanceStats);
|
||||
|
||||
@@ -2064,6 +2064,9 @@ describe("Integration of Clusterio", function() {
|
||||
const roundtrip = lib.ModPack.fromModPackString(result.stdout.trim());
|
||||
roundtrip.id = reference.id;
|
||||
assert.deepEqual(roundtrip, reference);
|
||||
const shown = await execCtlOutput("mod-pack show imported-pack");
|
||||
assert.match(shown, /empty_mod 1\.0\.0/);
|
||||
assert.match(shown, /MyDouble: 12\.25/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2317,6 +2320,46 @@ describe("Integration of Clusterio", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("config edit", function() {
|
||||
/** Write an editor script that replaces the edited file with content */
|
||||
async function writeEditor(content) {
|
||||
const file = path.resolve("temp", "test", "editor.sh");
|
||||
await fs.writeFile(file, `#!/bin/sh\ncat > "$1" <<'EOF'\n${content}\nEOF\n`, { mode: 0o755 });
|
||||
return file;
|
||||
}
|
||||
before(function() {
|
||||
if (process.platform === "win32") {
|
||||
this.skip();
|
||||
}
|
||||
});
|
||||
|
||||
it("sets controller config fields and reports failed ones", async function() {
|
||||
const config = await getControl().send(new lib.ControllerConfigGetRequest());
|
||||
const name = config["controller.name"];
|
||||
const editor = await writeEditor("controller.name = Edited-Cluster\ncontroller.no_such_field = 1");
|
||||
await execCtl(`controller config edit ${editor}`);
|
||||
const edited = await getControl().send(new lib.ControllerConfigGetRequest());
|
||||
assert.equal(edited["controller.name"], "Edited-Cluster");
|
||||
await execCtl(`controller config set controller.name ${name}`);
|
||||
});
|
||||
it("sets host config fields and reports failed ones", async function() {
|
||||
const config = await getControl().sendTo({ hostId }, new lib.HostConfigGetRequest());
|
||||
const editor = await writeEditor(`host.name = ${config["host.name"]}\nhost.no_such_field = 1`);
|
||||
await execCtl(`host config edit ${hostId} ${editor}`);
|
||||
});
|
||||
it("sets instance config fields and reports failed ones", async function() {
|
||||
const { id } = JSON.parse(await execCtlOutput("--output json instance create config-edit"));
|
||||
try {
|
||||
const editor = await writeEditor("instance.auto_start = true\ninstance.no_such_field = 1");
|
||||
await execCtl(`instance config edit ${id} ${editor}`);
|
||||
const config = await getControl().send(new lib.InstanceConfigGetRequest(id));
|
||||
assert.equal(config["instance.auto_start"], true);
|
||||
} finally {
|
||||
await execCtl(`instance delete ${id}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("--output json", function() {
|
||||
it("prints lists as an array of objects", async function() {
|
||||
const roles = JSON.parse(await execCtlOutput("--output json role list"));
|
||||
@@ -2339,6 +2382,26 @@ describe("Integration of Clusterio", function() {
|
||||
const { id } = JSON.parse(await execCtlOutput("--output json role create human-role"));
|
||||
assert.equal(await execCtlOutput(`role delete ${id}`), "Command ran successfully");
|
||||
});
|
||||
it("prints whole objects for lists that hide fields in human mode", async function() {
|
||||
const [mod] = JSON.parse(await execCtlOutput("--output json mod list"));
|
||||
assert("sha1" in mod);
|
||||
const [wireMod] = JSON.parse(await execCtlOutput("--output wire mod list"));
|
||||
assert("factorio_version" in wireMod);
|
||||
const [modPack] = JSON.parse(await execCtlOutput("--output wire mod-pack list"));
|
||||
assert("mods" in modPack);
|
||||
const user = JSON.parse(await execCtlOutput("--output wire user show test --instance-stats"));
|
||||
assert.equal(user.name, "test");
|
||||
});
|
||||
it("prints the user and instance stats in human mode", async function() {
|
||||
const output = await execCtlOutput("user show test --instance-stats");
|
||||
assert.match(output, /name +\| test/);
|
||||
});
|
||||
it("prints log queries in human mode", async function() {
|
||||
assert.notEqual(await execCtlOutput("log query --controller --limit 1"), "");
|
||||
});
|
||||
it("prints pending host requests in human mode", async function() {
|
||||
assert.notEqual(await execCtlOutput("host pending"), "");
|
||||
});
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user