Skip to content

Commit

Permalink
feat: Add action to reset the config to defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Jan 3, 2023
1 parent dc10df9 commit e7959a4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
18 changes: 16 additions & 2 deletions backend/lib/Configuration.js
Expand Up @@ -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");

Expand All @@ -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();
Expand Down Expand Up @@ -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
*/
Expand Down
10 changes: 10 additions & 0 deletions backend/lib/webserver/ValetudoRouter.js
Expand Up @@ -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");
Expand Down
17 changes: 16 additions & 1 deletion backend/lib/webserver/doc/ValetudoRouter.openapi.json
Expand Up @@ -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"
}
}
}
}
}
Expand Down

0 comments on commit e7959a4

Please sign in to comment.