mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +09:00
Address second review of ctl --output
CommandOutput.error takes a human reporter like result, so command errors log their message in human mode and print JSON in json and wire mode. table takes a map applied to each row in human mode, which note list, host plugin list, user list and save list now use. Rename streaming to streamingLog, and print startup errors with errorToJson directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8550df69eb
commit
23187859d6
11 files changed
+55
-36
No files matched your search
@@ -62,8 +62,8 @@ export type OutputFormat = typeof outputFormats[number];
|
||||
export class CommandOutput {
|
||||
/** True once a result has been printed */
|
||||
written = false;
|
||||
/** Command prints a JSON line per item as it runs instead of a single result */
|
||||
streaming = false;
|
||||
/** Command prints log events as results while it runs and has no final result */
|
||||
streamingLog = false;
|
||||
|
||||
constructor(
|
||||
public format: OutputFormat = "human",
|
||||
@@ -90,20 +90,34 @@ export class CommandOutput {
|
||||
}
|
||||
}
|
||||
|
||||
/** 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)));
|
||||
/**
|
||||
* Print a list of objects as a table, or as an array in json and wire mode
|
||||
*
|
||||
* @param rows - Printed as is in json and wire mode.
|
||||
* @param map - Converts each row into the table row printed in human mode.
|
||||
*/
|
||||
table<T extends object>(rows: T[], map: (row: T) => object = row => row) {
|
||||
this.result(rows, () => print(asTable(rows.map(map))));
|
||||
}
|
||||
|
||||
/** Print an error as `{ "error": ClassName, "message": string }` */
|
||||
error(err: unknown) {
|
||||
/**
|
||||
* Print an error as `{ "error": ClassName, "message": string }` in json and wire mode
|
||||
*
|
||||
* @param err - Error to print.
|
||||
* @param human - Reports the error in human mode.
|
||||
*/
|
||||
error(err: unknown, human: () => void) {
|
||||
this.written = true;
|
||||
print(JSON.stringify(errorToJson(err)));
|
||||
if (this.human) {
|
||||
human();
|
||||
} else {
|
||||
print(JSON.stringify(errorToJson(err)));
|
||||
}
|
||||
}
|
||||
|
||||
/** Print a success result for commands that finished without printing one */
|
||||
finish() {
|
||||
if (!this.written && !this.streaming) {
|
||||
if (!this.written && !this.streamingLog) {
|
||||
this.result({ success: true }, () => print("Command ran successfully"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ debugCommands.add(new lib.Command({
|
||||
handler: async function(args: object, ctl: Ctl) {
|
||||
await ctl.send(new lib.DebugDumpWsRequest());
|
||||
ctl.keepOpen = true;
|
||||
ctl.output.streaming = true;
|
||||
ctl.output.streamingLog = true;
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -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.result(plugins, () => print(asTable(plugins.map(p => ({
|
||||
ctl.output.table(plugins, p => ({
|
||||
title: p.title, version: p.version, state: p.state, npmPackage: p.npmPackage,
|
||||
})))));
|
||||
}));
|
||||
},
|
||||
}));
|
||||
hostPluginCommands.add(new lib.Command({
|
||||
|
||||
@@ -9,7 +9,7 @@ import util from "util";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { logger } from "@clusterio/lib";
|
||||
import type { Ctl } from "../ctl.js";
|
||||
import { asTable, errorToJson, print } from "./command_ops.js";
|
||||
import { errorToJson, print } from "./command_ops.js";
|
||||
import { serializedConfigToString, getEditor, configToKeyVal, printConfig } from "./config_ops.js";
|
||||
|
||||
const finished = util.promisify(stream.finished);
|
||||
@@ -287,9 +287,7 @@ instanceSaveCommands.add(new lib.Command({
|
||||
let instanceId = await lib.resolveInstance(ctl, args.instance);
|
||||
let saves = await ctl.sendTo("controller", new lib.InstanceSaveDetailsListRequest());
|
||||
saves = saves.filter(save => save.instanceId === instanceId);
|
||||
ctl.output.result(saves, () => print(asTable(saves.map(
|
||||
({ mtimeMs, ...rest }) => ({ ...rest, mtime: new Date(mtimeMs).toLocaleString() })
|
||||
))));
|
||||
ctl.output.table(saves, ({ mtimeMs, ...rest }) => ({ ...rest, mtime: new Date(mtimeMs).toLocaleString() }));
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ logCommands.add(new lib.Command({
|
||||
let hostIds = args.host ? [await lib.resolveHost(ctl, args.host)] : [];
|
||||
await ctl.setLogSubscriptions({ all: args.all, controller: args.controller, hostIds, instanceIds });
|
||||
ctl.keepOpen = true;
|
||||
ctl.output.streaming = true;
|
||||
ctl.output.streamingLog = true;
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import type { Ctl } from "../ctl.js";
|
||||
import { asTable, print } from "./command_ops.js";
|
||||
import { 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.result(notes, () => print(asTable(notes.map(note => ({
|
||||
ctl.output.table(notes, note => ({
|
||||
type: note.resourceType,
|
||||
id: note.resourceId,
|
||||
updatedBy: note.updatedBy,
|
||||
content: note.content,
|
||||
})))));
|
||||
}));
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ userCommands.add(new lib.Command({
|
||||
}],
|
||||
handler: async function(args: { stats: boolean, attributes: boolean }, ctl: Ctl) {
|
||||
const users = await ctl.send(new lib.UserListRequest());
|
||||
const rows = users.map(user => {
|
||||
ctl.output.table(users, user => {
|
||||
const {
|
||||
playerStats, isDeleted, banReason, instanceStats, isAdmin, isWhitelisted, isBanned, ...row
|
||||
} = user;
|
||||
@@ -66,7 +66,6 @@ userCommands.add(new lib.Command({
|
||||
...args.stats && playerStats,
|
||||
};
|
||||
});
|
||||
ctl.output.result(users, () => print(asTable(rows)));
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
Reference in new issue
Block a user