Allow config to be specified with an explicit cli argument.

This commit is contained in:
gnuxie 2022-11-22 14:51:45 +00:00
parent eeaa9d3aa1
commit dbe7af78ab

View File

@ -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;
}
} }