mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Commands print their results through a CommandOutput on the ctl link.
In json mode lists print as arrays of objects, other results as an
object, commands without a result print {} and failures print
{ error: ClassName, message } with exit code 1. log follow and
debug dump-ws print JSON lines.
Human output now goes through the shared asTable printer in every
command, results that were logged to stderr (role create, save and mod
upload/download) print to stdout, and config edit failures are logged
instead of printed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import * as lib from "@clusterio/lib";
|
|
import type { Ctl } from "../ctl.js";
|
|
import { print } from "./command_ops.js";
|
|
|
|
export async function getEditor(argsEditor: string) {
|
|
// eslint-disable-next-line
|
|
return argsEditor || process.env.EDITOR || process.env.VISUAL || undefined;
|
|
// needed for the process.env statements to not be flagged by eslint
|
|
// priority for editors is CLI argument > env.EDITOR > env.VISUAL
|
|
}
|
|
|
|
export async function configToKeyVal(data: string) {
|
|
let final: Record<string, string> = {};
|
|
let splitData = data.split(/\r?\n/);
|
|
// split on newlines
|
|
let filtered = splitData.filter((value) => value[0] !== "#").filter((a) => a);
|
|
// the last filter removes empty elements left by the first. Not done on one line due to readability.
|
|
for (let index in filtered) {
|
|
if (index in filtered) {
|
|
// split on the first = only, values may contain = themselves
|
|
let split = filtered[index].indexOf("=");
|
|
if (split === -1) {
|
|
// no value given, it's a empty field and therefor null
|
|
final[filtered[index].trim()] = "";
|
|
continue;
|
|
}
|
|
final[filtered[index].slice(0, split).trim()] = filtered[index].slice(split + 1).trim();
|
|
}
|
|
}
|
|
return final;
|
|
}
|
|
|
|
export function serializedConfigToString(
|
|
serializedConfig: lib.ConfigSchema,
|
|
configClass: typeof lib.ControllerConfig | typeof lib.HostConfig | typeof lib.InstanceConfig,
|
|
) {
|
|
const config = configClass.fromJSON({}, "control");
|
|
let allConfigElements = "";
|
|
for (let [name, value] of Object.entries(serializedConfig)) {
|
|
const def = (configClass.fieldDefinitions as lib.ConfigDefs<any>)[name];
|
|
try {
|
|
if (!config.canAccess(name, lib.ConfigAccess.readWrite) || def.hidden) {
|
|
continue;
|
|
}
|
|
} catch (err) {
|
|
// Field does not exist in the config class
|
|
continue;
|
|
}
|
|
if (value === null) {
|
|
value = "";
|
|
} else if (typeof value === "object") {
|
|
value = JSON.stringify(value);
|
|
}
|
|
allConfigElements += `# ${def.description ?? "No description found"}\n${name} = ${value}\n\n`;
|
|
}
|
|
return allConfigElements;
|
|
}
|
|
|
|
/** Print config fields as `name value` lines, or as an object in json mode */
|
|
export function printConfig(ctl: Ctl, config: lib.ConfigSchema, note = (name: string) => "") {
|
|
ctl.output.result(config, () => {
|
|
for (let [name, value] of Object.entries(config)) {
|
|
if (name !== "instance.inherited_fields") {
|
|
print(`${name} ${JSON.stringify(value)}${note(name)}`);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Print whether a restart is still needed after an update or install */
|
|
export function printRestartRequired(ctl: Ctl, restartRequired: boolean, action: string) {
|
|
ctl.output.result({ restartRequired }, () => {
|
|
if (restartRequired) {
|
|
print(`${action}; a restart is required to apply the changes.`);
|
|
}
|
|
});
|
|
}
|