mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Require config files to be explicitly created (#1040)
The behaviour of creating the config file if it's missing while convenient causes confusion and problems when the tools are run in the wrong folder. They end up creating a shadow installation and misdirect the user with nonsensical error messages, instead pointing to the real problem which is that the tool can't find the configuration needed to actually run. Require a config file to be present at startup and refuse to run if this file is missing. To support the odd use-case of creating the config when missing a --create-config flag is added. To conveniently generate a config file to edit out from the "config create" command is added.
This commit is contained in:
1 parent
5df5af7ca8
commit
2c52c265cf
15 files changed
+249
-75
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import fs from "node:fs/promises";
|
||||
|
||||
import { logger } from "./logging.js";
|
||||
import { LockFile } from "./LockFile.js";
|
||||
import type { Config, ConfigLocation } from "./config/classes.js";
|
||||
import { StartupError } from "./errors.js";
|
||||
|
||||
export async function loadConfigFromArgs<T extends typeof Config<any>>(
|
||||
args: {
|
||||
config: string,
|
||||
bypassLockFile: boolean,
|
||||
createConfig: boolean,
|
||||
_: (string | number)[],
|
||||
},
|
||||
ConfigClass: T,
|
||||
location: ConfigLocation,
|
||||
) {
|
||||
const configLockPath = `${args.config}.lock`;
|
||||
if (args.bypassLockFile) {
|
||||
await fs.unlink(configLockPath).catch(() => {}); // ignore error, file might not exist
|
||||
}
|
||||
|
||||
const isCreateCommand = args._[0] === "config" && args._[1] === "create";
|
||||
const create = args.createConfig || isCreateCommand;
|
||||
|
||||
let config;
|
||||
const configLock = new LockFile(configLockPath);
|
||||
const message = `${create ? "Creating" : "Loading"} config at ${args.config}`;
|
||||
if (location === "control" && !create) {
|
||||
// Ctl uses a verbose level here to avoid spamming the console.
|
||||
// A better idea would probably be to set Ctl's default level to warn.
|
||||
logger.verbose(message);
|
||||
} else {
|
||||
logger.info(message);
|
||||
}
|
||||
|
||||
try {
|
||||
config = await ConfigClass.fromFile(location, args.config);
|
||||
|
||||
} catch (err: any) {
|
||||
if (err.code !== "ENOENT" || !create) {
|
||||
throw new StartupError(`Failed to load ${args.config}: ${err.stack ?? err.message ?? err}`);
|
||||
}
|
||||
|
||||
config = new ConfigClass(location, undefined, args.config);
|
||||
await config.save(true);
|
||||
}
|
||||
|
||||
return [config as InstanceType<T>, configLock] as const;
|
||||
}
|
||||
@@ -460,9 +460,12 @@ export class Config<
|
||||
return this.fromJSON(fields, location, filepath);
|
||||
}
|
||||
|
||||
/** Saves this config to file if there are unsaved changes */
|
||||
async save() {
|
||||
if (!this.dirty) {
|
||||
/**
|
||||
* Saves this config to file if there are unsaved changes
|
||||
* @param force - Set to true to save the file even if no changes have been made.
|
||||
*/
|
||||
async save(force = false) {
|
||||
if (!force && !this.dirty) {
|
||||
return;
|
||||
}
|
||||
if (!this.filepath) {
|
||||
|
||||
@@ -229,6 +229,7 @@ export async function handlePluginCommand(
|
||||
*/
|
||||
export function configCommand(yargs: any) {
|
||||
yargs
|
||||
.command("create", "Create a new config file if not present")
|
||||
.command("set <field> [value]", "Set config field", (yargs: any) => {
|
||||
yargs.options({
|
||||
"stdin": { describe: "read value from stdin", nargs: 0, type: "boolean" },
|
||||
@@ -258,7 +259,10 @@ export async function handleConfigCommand(
|
||||
) {
|
||||
let command = (args._ as string[])[1];
|
||||
|
||||
if (command === "list") {
|
||||
if (command === "create") {
|
||||
// By the time we get here loadConfigFromArgs will have already created the config.
|
||||
|
||||
} else if (command === "list") {
|
||||
for (const name of Object.keys(instance.constructor.fieldDefinitions)) {
|
||||
print(`${name} ${JSON.stringify(instance.get(name))}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user