Refactor config to JSON data class pattern

Refactor the Config and ConfigGroup classes to match the JSON data class
pattern with toJSON, toRemote and fromJSON methods replacing the load,
init, and serialize methods.  Since fromJSON requires extra information
the messages structures passes around the plain JSON representation of
the configs instead of serialising instances of the config classes
automatically.
This commit is contained in:
Hornwitser committed 2024-01-06 19:53:32 +01:00
1 parent 27af142a87
commit 001db4befe
24 files changed
+269 -334

No files matched your search

+6 -7
View File
@@ -156,7 +156,6 @@ async function handleBootstrapCommand(
return;
}
let controlConfig = new lib.ControlConfig("control");
controlConfig.init();
controlConfig.set("control.controller_url", Controller.calculateControllerUrl(controllerConfig));
controlConfig.set(
@@ -164,7 +163,7 @@ async function handleBootstrapCommand(
userManager.signUserToken(admin.name),
);
let content = JSON.stringify(controlConfig.serialize(), null, "\t");
let content = JSON.stringify(controlConfig, null, "\t");
if (args.output === "-") {
// eslint-disable-next-line no-console
console.log(content);
@@ -322,15 +321,15 @@ async function initialize(): Promise<InitializeParameters> {
const controllerConfigPath = args.config;
logger.info(`Loading config from ${controllerConfigPath}`);
const controllerConfig = new lib.ControllerConfig("controller");
let controllerConfig = new lib.ControllerConfig("controller");
try {
let fileData = await fs.readFile(controllerConfigPath, { encoding: "utf8" });
controllerConfig.load(JSON.parse(fileData));
let jsonConfig = JSON.parse(await fs.readFile(controllerConfigPath, { encoding: "utf8" }));
controllerConfig = lib.ControllerConfig.fromJSON(jsonConfig, "controller");
} catch (err: any) {
if (err.code === "ENOENT") {
logger.info("Config not found, initializing new config");
controllerConfig.init();
controllerConfig = new lib.ControllerConfig("controller");
} else {
throw new lib.StartupError(`Failed to load ${controllerConfigPath}: ${err.message}`);
@@ -343,7 +342,7 @@ async function initialize(): Promise<InitializeParameters> {
let bytes = await asyncRandomBytes(256);
controllerConfig.set("controller.auth_secret", bytes.toString("base64"));
await lib.safeOutputFile(
controllerConfigPath, JSON.stringify(controllerConfig.serialize(), null, "\t")
controllerConfigPath, JSON.stringify(controllerConfig, null, "\t")
);
}