From e7959a4b2478ac0772191f8b5fef6da960cbcf8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Beye?= Date: Tue, 3 Jan 2023 11:37:37 +0100 Subject: [PATCH] feat: Add action to reset the config to defaults --- backend/lib/Configuration.js | 18 ++++++++++++++++-- backend/lib/webserver/ValetudoRouter.js | 10 ++++++++++ .../webserver/doc/ValetudoRouter.openapi.json | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/backend/lib/Configuration.js b/backend/lib/Configuration.js index 3e072a498f..266431befc 100644 --- a/backend/lib/Configuration.js +++ b/backend/lib/Configuration.js @@ -15,7 +15,7 @@ class Configuration { constructor() { /** @private */ this.eventEmitter = new EventEmitter(); - this.settings = DEFAULT_SETTINGS; + this.settings = structuredClone(DEFAULT_SETTINGS); this.location = process.env.VALETUDO_CONFIG_PATH ?? path.join(os.tmpdir(), "valetudo_config.json"); @@ -38,7 +38,7 @@ class Configuration { * @param {string} key * @param {string|object} val */ - set(key, val) { //TODO: set nested + set(key, val) { this.settings[key] = val; this.persist(); @@ -124,6 +124,20 @@ class Configuration { } } + /** + * @public + */ + reset() { + Logger.info("Restoring config to default settings."); + + this.settings = structuredClone(DEFAULT_SETTINGS); + this.persist(); + + Object.keys(this.settings).forEach(key => { + this.eventEmitter.emit(CONFIG_UPDATE_EVENT, key); + }); + } + /** * @private */ diff --git a/backend/lib/webserver/ValetudoRouter.js b/backend/lib/webserver/ValetudoRouter.js index 8f97bff848..1b9f1eb739 100644 --- a/backend/lib/webserver/ValetudoRouter.js +++ b/backend/lib/webserver/ValetudoRouter.js @@ -51,6 +51,16 @@ class ValetudoRouter { } break; } + case "restoreDefaultConfiguration": { + if (this.config.get("embedded") === true) { + this.config.reset(); + } else { + // noinspection ExceptionCaughtLocallyJS + throw new Error("Refusing to restore config to defaults as we're not embedded."); + } + + break; + } default: // noinspection ExceptionCaughtLocallyJS throw new Error("Invalid action"); diff --git a/backend/lib/webserver/doc/ValetudoRouter.openapi.json b/backend/lib/webserver/doc/ValetudoRouter.openapi.json index 62c0556461..c37aa75720 100644 --- a/backend/lib/webserver/doc/ValetudoRouter.openapi.json +++ b/backend/lib/webserver/doc/ValetudoRouter.openapi.json @@ -46,10 +46,25 @@ "action": { "type": "string", "enum": [ - "dismissWelcomeDialog" + "dismissWelcomeDialog", + "restoreDefaultConfiguration" ] } } + }, + "examples": { + "dismiss_welcome_dialog": { + "description": "Dismiss the OOBE welcome dialog", + "value": { + "action": "dismissWelcomeDialog" + } + }, + "restore_default_configuration": { + "description": "Restore the default configuration", + "value": { + "action": "restoreDefaultConfiguration" + } + } } } }