From fdccffdcf25724743a03083c6c256eead6410e4c Mon Sep 17 00:00:00 2001 From: David Teller Date: Wed, 7 Sep 2022 10:18:38 +0200 Subject: [PATCH] Tweaking config instructions and initialization - resolves #365 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Configuration instructions encourage the user to use a configuration file called `development.yaml` (for self-build) or `production.yaml` (for Docker build) but do not provide instructions to point Mjölnir to read from these files. This patch: - allows Mjölnir to read `production.yaml` if available, without additional instructions needed; - change the instructions for self-build to use `default.yaml`, which is read without additional instructions; - add instructions in case the user wishes to use `development.yaml`; - tweak the error message in case the config file isn't setup at all to clarify this for users. --- docs/setup_selfbuild.md | 23 +++++++++++++++++--- src/config.ts | 32 ++++++++++++++++++++++++---- test/commands/UnbanBanCommandTest.ts | 2 +- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/docs/setup_selfbuild.md b/docs/setup_selfbuild.md index 908078a..82f1aa0 100644 --- a/docs/setup_selfbuild.md +++ b/docs/setup_selfbuild.md @@ -7,9 +7,26 @@ cd mjolnir yarn install yarn build -# Copy and edit the config. It *is* recommended to change the data path. -cp config/default.yaml config/development.yaml -nano config/development.yaml +# Edit the config. +# You probably should change `dataPath`. +nano config/default.yaml node lib/index.js ``` + +Or, if you wish to use a different configuration file, e.g. `development.yaml` + +```bash +git clone https://github.com/matrix-org/mjolnir.git +cd mjolnir + +yarn install +yarn build + +# Edit the config. +# You probably should change `dataPath`. +cp config/default.yaml config/development.yaml +nano config/development.yaml + +NODE_ENV=development node lib/index.js +``` diff --git a/src/config.ts b/src/config.ts index dad9ee4..a4445a4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -170,12 +170,36 @@ const defaultConfig: IConfig = { }, }; -export function read(): IConfig { +export function read(checkConfigured = true): IConfig { const config_dir = process.env.NODE_CONFIG_DIR || "./config"; - const config_file = `${process.env.NODE_ENV || "default"}.yaml` - - const content = fs.readFileSync(path.join(config_dir, config_file), "utf8"); + let config_path; + for (let key of [ + process.env.NODE_ENV, + "production", + "default" + ]) { + if (!key) { + continue; + } + const candidate_path = path.join(config_dir, `${key}.yaml`); + if (!fs.existsSync(candidate_path)) { + continue; + } + config_path = candidate_path; + break; + } + if (!config_path) { + throw new Error("Could not find any valid configuration file"); + } + const content = fs.readFileSync(config_path, "utf8"); const parsed = load(content); const config = {...defaultConfig, ...(parsed as object)} as IConfig; + if (checkConfigured && config.accessToken === "YOUR_TOKEN_HERE") { + // A small check to simplify the error message in case + // the configuration file hasn't been modified. + throw new Error(`Invalid access token in configuration file ${config_path}. ` + + "This usually indicates that Mjölnir's configuration file was not setup " + + "or that Mjölnir is using the wrong configuration file."); + } return config; } diff --git a/test/commands/UnbanBanCommandTest.ts b/test/commands/UnbanBanCommandTest.ts index f19c897..f0184a3 100644 --- a/test/commands/UnbanBanCommandTest.ts +++ b/test/commands/UnbanBanCommandTest.ts @@ -22,7 +22,7 @@ import { read as configRead } from "../../src/config"; import { RULE_ROOM, RULE_SERVER, RULE_USER } from "../../src/models/ListRule"; function createTestMjolnir(defaultShortcode: string|null = null): Mjolnir { - const config = configRead(); + const config = configRead(false); const client = { // Mock `MatrixClient.getAccountData` . getAccountData: (eventType: string): Promise => {