mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16: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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user