Skip to content

Commit fdccffd

Browse files
committed
Tweaking config instructions and initialization - resolves #365
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.
1 parent 4d5447c commit fdccffd

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

docs/setup_selfbuild.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,26 @@ cd mjolnir
77
yarn install
88
yarn build
99

10-
# Copy and edit the config. It *is* recommended to change the data path.
10+
# Edit the config.
11+
# You probably should change `dataPath`.
12+
nano config/default.yaml
13+
14+
node lib/index.js
15+
```
16+
17+
Or, if you wish to use a different configuration file, e.g. `development.yaml`
18+
19+
```bash
20+
git clone https://github.com/matrix-org/mjolnir.git
21+
cd mjolnir
22+
23+
yarn install
24+
yarn build
25+
26+
# Edit the config.
27+
# You probably should change `dataPath`.
1128
cp config/default.yaml config/development.yaml
1229
nano config/development.yaml
1330

14-
node lib/index.js
31+
NODE_ENV=development node lib/index.js
1532
```

src/config.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,36 @@ const defaultConfig: IConfig = {
170170
},
171171
};
172172

173-
export function read(): IConfig {
173+
export function read(checkConfigured = true): IConfig {
174174
const config_dir = process.env.NODE_CONFIG_DIR || "./config";
175-
const config_file = `${process.env.NODE_ENV || "default"}.yaml`
176-
177-
const content = fs.readFileSync(path.join(config_dir, config_file), "utf8");
175+
let config_path;
176+
for (let key of [
177+
process.env.NODE_ENV,
178+
"production",
179+
"default"
180+
]) {
181+
if (!key) {
182+
continue;
183+
}
184+
const candidate_path = path.join(config_dir, `${key}.yaml`);
185+
if (!fs.existsSync(candidate_path)) {
186+
continue;
187+
}
188+
config_path = candidate_path;
189+
break;
190+
}
191+
if (!config_path) {
192+
throw new Error("Could not find any valid configuration file");
193+
}
194+
const content = fs.readFileSync(config_path, "utf8");
178195
const parsed = load(content);
179196
const config = {...defaultConfig, ...(parsed as object)} as IConfig;
197+
if (checkConfigured && config.accessToken === "YOUR_TOKEN_HERE") {
198+
// A small check to simplify the error message in case
199+
// the configuration file hasn't been modified.
200+
throw new Error(`Invalid access token in configuration file ${config_path}. `
201+
+ "This usually indicates that Mjölnir's configuration file was not setup "
202+
+ "or that Mjölnir is using the wrong configuration file.");
203+
}
180204
return config;
181205
}

test/commands/UnbanBanCommandTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { read as configRead } from "../../src/config";
2222
import { RULE_ROOM, RULE_SERVER, RULE_USER } from "../../src/models/ListRule";
2323

2424
function createTestMjolnir(defaultShortcode: string|null = null): Mjolnir {
25-
const config = configRead();
25+
const config = configRead(false);
2626
const client = {
2727
// Mock `MatrixClient.getAccountData` .
2828
getAccountData: (eventType: string): Promise<any> => {

0 commit comments

Comments
 (0)