mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Address review of ctl --output
Add a wire output format that prints the toJSON format sent over the link. Commands without a result print {"success": true}, or "Command ran successfully" in human mode. The missing URL check throws a StartupError, unexpected command errors are rethrown to the startup handler, and startup errors log as fatal. user list, note list and host plugin list print the objects they received instead of their table rows.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
10beffe167
commit
8550df69eb
13 files changed
+131
-112
No files matched your search
@@ -39,13 +39,25 @@ export function errorToJson(err: unknown) {
|
||||
return { error: "Error", message: String(err) };
|
||||
}
|
||||
|
||||
export const outputFormats = ["human", "json"] as const;
|
||||
/** JSON.stringify replacer for Sets and Maps that have no toJSON method */
|
||||
function wireReplacer(key: string, value: unknown) {
|
||||
if (value instanceof Set) {
|
||||
return [...value];
|
||||
}
|
||||
if (value instanceof Map) {
|
||||
return Object.fromEntries(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export const outputFormats = ["human", "json", "wire"] as const;
|
||||
export type OutputFormat = typeof outputFormats[number];
|
||||
|
||||
/**
|
||||
* Prints the results of a ctl command in the format chosen with --output
|
||||
*
|
||||
* In json mode each call to result prints one line of JSON to stdout.
|
||||
* In json and wire mode each call to result prints one line of JSON to stdout. json uses the
|
||||
* property names of the objects, wire uses their toJSON methods to give the format sent over the link.
|
||||
*/
|
||||
export class CommandOutput {
|
||||
/** True once a result has been printed */
|
||||
@@ -57,26 +69,28 @@ export class CommandOutput {
|
||||
public format: OutputFormat = "human",
|
||||
) { }
|
||||
|
||||
get json() {
|
||||
return this.format === "json";
|
||||
get human() {
|
||||
return this.format === "human";
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the result of a command
|
||||
*
|
||||
* @param data - Printed as JSON in json mode.
|
||||
* @param data - Printed as JSON in json and wire mode.
|
||||
* @param human - Prints the result in human mode, defaults to printing data as is.
|
||||
*/
|
||||
result(data: unknown, human: () => void = () => print(data)) {
|
||||
this.written = true;
|
||||
if (this.json) {
|
||||
if (this.format === "json") {
|
||||
print(JSON.stringify(toPlain(data)));
|
||||
} else if (this.format === "wire") {
|
||||
print(JSON.stringify(data, wireReplacer));
|
||||
} else {
|
||||
human();
|
||||
}
|
||||
}
|
||||
|
||||
/** Print a list of objects as a table, or as an array in json mode */
|
||||
/** Print a list of objects as a table, or as an array in json and wire mode */
|
||||
table(rows: object[]) {
|
||||
this.result(rows, () => print(asTable(rows)));
|
||||
}
|
||||
@@ -87,10 +101,10 @@ export class CommandOutput {
|
||||
print(JSON.stringify(errorToJson(err)));
|
||||
}
|
||||
|
||||
/** Print `{}` in json mode for commands that finished without printing a result */
|
||||
/** Print a success result for commands that finished without printing one */
|
||||
finish() {
|
||||
if (this.json && !this.written && !this.streaming) {
|
||||
this.result({});
|
||||
if (!this.written && !this.streaming) {
|
||||
this.result({ success: true }, () => print("Command ran successfully"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,9 +287,9 @@ hostPluginCommands.add(new lib.Command({
|
||||
handler: async function(args: { host: string }, ctl: Ctl) {
|
||||
let hostId = await lib.resolveHost(ctl, args.host);
|
||||
const plugins = await ctl.sendTo({ hostId }, new lib.PluginListRequest());
|
||||
ctl.output.table(plugins.map(p => ({
|
||||
ctl.output.result(plugins, () => print(asTable(plugins.map(p => ({
|
||||
title: p.title, version: p.version, state: p.state, npmPackage: p.npmPackage,
|
||||
})));
|
||||
})))));
|
||||
},
|
||||
}));
|
||||
hostPluginCommands.add(new lib.Command({
|
||||
|
||||
@@ -575,7 +575,7 @@ instanceCommands.add(new lib.Command({
|
||||
return;
|
||||
}
|
||||
|
||||
const human = !ctl.output.json;
|
||||
const human = ctl.output.human;
|
||||
if (human) {
|
||||
print(`Starting ${instancesToStart.length} instance(s):`);
|
||||
for (const instance of instancesToStart) {
|
||||
@@ -662,7 +662,7 @@ instanceCommands.add(new lib.Command({
|
||||
return;
|
||||
}
|
||||
|
||||
const human = !ctl.output.json;
|
||||
const human = ctl.output.human;
|
||||
if (human) {
|
||||
print(`Stopping ${instancesToStop.length} instance(s):`);
|
||||
for (const instance of instancesToStop) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import type { Ctl } from "../ctl.js";
|
||||
import { print } from "./command_ops.js";
|
||||
import { asTable, print } from "./command_ops.js";
|
||||
|
||||
export const noteCommands = new lib.CommandTree({
|
||||
name: "note", description: "Notes attached to hosts, instances, users and other resources",
|
||||
@@ -16,12 +16,12 @@ noteCommands.add(new lib.Command({
|
||||
if (args.type !== undefined) {
|
||||
notes = notes.filter(note => note.resourceType === args.type);
|
||||
}
|
||||
ctl.output.table(notes.map(note => ({
|
||||
ctl.output.result(notes, () => print(asTable(notes.map(note => ({
|
||||
type: note.resourceType,
|
||||
id: note.resourceId,
|
||||
updatedBy: note.updatedBy,
|
||||
content: note.content,
|
||||
})));
|
||||
})))));
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -55,29 +55,18 @@ userCommands.add(new lib.Command({
|
||||
});
|
||||
}],
|
||||
handler: async function(args: { stats: boolean, attributes: boolean }, ctl: Ctl) {
|
||||
let users = await ctl.send(new lib.UserListRequest());
|
||||
for (let user of users) {
|
||||
if (args.stats) {
|
||||
Object.assign(user, user.playerStats);
|
||||
}
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.playerStats;
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.isDeleted;
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.banReason;
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.instanceStats;
|
||||
if (!args.attributes) {
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.isAdmin;
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.isWhitelisted;
|
||||
// @ts-expect-error Terrible hack
|
||||
delete user.isBanned;
|
||||
}
|
||||
}
|
||||
ctl.output.table(users);
|
||||
const users = await ctl.send(new lib.UserListRequest());
|
||||
const rows = users.map(user => {
|
||||
const {
|
||||
playerStats, isDeleted, banReason, instanceStats, isAdmin, isWhitelisted, isBanned, ...row
|
||||
} = user;
|
||||
return {
|
||||
...row,
|
||||
...args.attributes && { isAdmin, isWhitelisted, isBanned },
|
||||
...args.stats && playerStats,
|
||||
};
|
||||
});
|
||||
ctl.output.result(users, () => print(asTable(rows)));
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ export function serializedConfigToString(
|
||||
export function printConfig(ctl: Ctl, config: lib.ConfigSchema, note = (name: string) => "") {
|
||||
ctl.output.result(config, () => {
|
||||
for (let [name, value] of Object.entries(config)) {
|
||||
// Shown as an (inherited) note on each field by instance config list instead
|
||||
if (name !== "instance.inherited_fields") {
|
||||
print(`${name} ${JSON.stringify(value)}${note(name)}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user