From a9868f720a4ad8979dbcd27af0b0c3f72a6c8a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Beye?= Date: Sat, 23 Mar 2024 19:55:41 +0100 Subject: [PATCH] feat: Allow advanced users to customize their system ID --- backend/lib/Configuration.js | 3 ++- backend/lib/Valetudo.js | 3 ++- backend/lib/res/env.js | 5 +++++ backend/lib/utils/Tools.js | 17 ++++++++++++++++- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 backend/lib/res/env.js diff --git a/backend/lib/Configuration.js b/backend/lib/Configuration.js index 3bc3f7843c0..440f4a4b061 100644 --- a/backend/lib/Configuration.js +++ b/backend/lib/Configuration.js @@ -6,6 +6,7 @@ const os = require("os"); const path = require("path"); const DEFAULT_SETTINGS = require("./res/default_config.json"); +const env = require("./res/env"); const Logger = require("./Logger"); const SCHEMAS = require("./doc/Configuration.openapi.json"); const Tools = require("./utils/Tools"); @@ -17,7 +18,7 @@ class Configuration { this.eventEmitter = new EventEmitter(); this.settings = structuredClone(DEFAULT_SETTINGS); - this.location = process.env.VALETUDO_CONFIG_PATH ?? path.join(os.tmpdir(), "valetudo_config.json"); + this.location = process.env[env.ConfigPath] ?? path.join(os.tmpdir(), "valetudo_config.json"); this.loadConfig(); } diff --git a/backend/lib/Valetudo.js b/backend/lib/Valetudo.js index 4b30139aad5..e27e355e1a4 100644 --- a/backend/lib/Valetudo.js +++ b/backend/lib/Valetudo.js @@ -1,4 +1,5 @@ const Configuration = require("./Configuration"); +const env = require("./res/env"); const fs = require("fs"); const Logger = require("./Logger"); const MqttController = require("./mqtt/MqttController"); @@ -24,7 +25,7 @@ class Valetudo { try { Logger.setLogLevel(this.config.get("logLevel")); - Logger.setLogFilePath(process.env.VALETUDO_LOG_PATH ?? path.join(os.tmpdir(), "valetudo.log")); + Logger.setLogFilePath(process.env[env.LogPath] ?? path.join(os.tmpdir(), "valetudo.log")); } catch (e) { Logger.error("Initialising Logger: " + e); } diff --git a/backend/lib/res/env.js b/backend/lib/res/env.js new file mode 100644 index 00000000000..e08143105e1 --- /dev/null +++ b/backend/lib/res/env.js @@ -0,0 +1,5 @@ +module.exports = { + ConfigPath: "VALETUDO_LOG_PATH", + LogPath: "VALETUDO_LOG_PATH", + HumanReadableSystemId: "VALETUDO_HUMAN_READABLE_SYSTEM_ID", +}; diff --git a/backend/lib/utils/Tools.js b/backend/lib/utils/Tools.js index f2acf138d40..0de66ca083a 100644 --- a/backend/lib/utils/Tools.js +++ b/backend/lib/utils/Tools.js @@ -4,6 +4,7 @@ const path = require("path"); const uuid = require("uuid"); const zooIDs = require("zoo-ids"); +const env = require("../res/env"); const LinuxTools = require("./LinuxTools"); let SYSTEM_ID; @@ -133,7 +134,21 @@ class Tools { } static GET_HUMAN_READABLE_SYSTEM_ID() { - return zooIDs.generateId(Tools.GET_SYSTEM_ID()); + /* + This is not part of the config file because: + A) You can break functionality if you configure this incorrectly (invalid string, duplicates, etc.) leading to support issues + and + B) Because this is a static method and passing the config here would require annoying refactoring + + Having it as an undocumented ENV variable should be accessible enough for those who don't require support + hand-holding if they configure it incorrectly but also inaccessible enough for those who do. + And, thirdly, it can still vanish at any time if necessary. You're welcome. + */ + if (process.env[env.HumanReadableSystemId]?.length > 0) { + return process.env[env.HumanReadableSystemId]; + } else { + return zooIDs.generateId(Tools.GET_SYSTEM_ID()); + } } static GET_ZEROCONF_HOSTNAME() {