Skip to content

Commit

Permalink
feat: Allow advanced users to customize their system ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Mar 23, 2024
1 parent 32e2973 commit a9868f7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion backend/lib/Valetudo.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions backend/lib/res/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
ConfigPath: "VALETUDO_LOG_PATH",
LogPath: "VALETUDO_LOG_PATH",
HumanReadableSystemId: "VALETUDO_HUMAN_READABLE_SYSTEM_ID",
};
17 changes: 16 additions & 1 deletion backend/lib/utils/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a9868f7

Please sign in to comment.