diff --git a/packages/ctl/src/command_ops.ts b/packages/ctl/src/command_ops.ts index 258eea00..54c9373f 100644 --- a/packages/ctl/src/command_ops.ts +++ b/packages/ctl/src/command_ops.ts @@ -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) { diff --git a/packages/ctl/src/commands_mod.ts b/packages/ctl/src/commands_mod.ts index 188a1b9e..f81b9abf 100644 --- a/packages/ctl/src/commands_mod.ts +++ b/packages/ctl/src/commands_mod.ts @@ -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)))); }); }, })); diff --git a/packages/ctl/src/commands_mod_pack.ts b/packages/ctl/src/commands_mod_pack.ts index 7b87aab6..6f71c0a2 100644 --- a/packages/ctl/src/commands_mod_pack.ts +++ b/packages/ctl/src/commands_mod_pack.ts @@ -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"])); }, })); diff --git a/packages/ctl/src/commands_user.ts b/packages/ctl/src/commands_user.ts index 54991509..525a45d7 100644 --- a/packages/ctl/src/commands_user.ts +++ b/packages/ctl/src/commands_user.ts @@ -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 ?? "", - 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 ?? ""; print(); print(`Instance ${name} (${id}):`); printProperties(playerInstanceStats); diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index 091319b7..2afc9926 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -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);