mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 01:36:06 -04:00
Allow config to be specified with an explicit cli argument.
This commit is contained in:
parent
eeaa9d3aa1
commit
dbe7af78ab
@ -15,7 +15,6 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import * as path from "path";
|
|
||||||
import { load } from "js-yaml";
|
import { load } from "js-yaml";
|
||||||
import { MatrixClient } from "matrix-bot-sdk";
|
import { MatrixClient } from "matrix-bot-sdk";
|
||||||
import Config from "config";
|
import Config from "config";
|
||||||
@ -171,7 +170,32 @@ const defaultConfig: IConfig = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export function read(): IConfig {
|
/**
|
||||||
const config = Config.util.extendDeep({}, defaultConfig, Config.util.toObject()) as IConfig;
|
* Grabs an explicit path provided for mjolnir's config from an arguments vector if provided, otherwise returns undefined.
|
||||||
return config;
|
* @param argv An arguments vector sourced from `process.argv`.
|
||||||
|
* @returns The path if one was provided or undefined.
|
||||||
|
*/
|
||||||
|
function configPathFromArguments(argv: string[]): undefined|string {
|
||||||
|
const configOptionIndex = argv.findIndex(arg => arg === "--mjolnir-config");
|
||||||
|
if (configOptionIndex > 0) {
|
||||||
|
const configOptionPath = argv.at(configOptionIndex + 1);
|
||||||
|
if (!configOptionPath) {
|
||||||
|
throw new Error("No path provided with option --mjolnir-config");
|
||||||
|
}
|
||||||
|
return configOptionPath;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function read(): IConfig {
|
||||||
|
const explicitConfigPath = configPathFromArguments(process.argv);
|
||||||
|
if (explicitConfigPath) {
|
||||||
|
const content = fs.readFileSync(explicitConfigPath, "utf8");
|
||||||
|
const parsed = load(content);
|
||||||
|
return Config.util.extendDeep({}, defaultConfig, parsed);
|
||||||
|
} else {
|
||||||
|
const config = Config.util.extendDeep({}, defaultConfig, Config.util.toObject()) as IConfig;
|
||||||
|
return config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user