From 7871b07113780690697055864f6d3ec3f3415951 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 6 May 2023 16:40:15 -0400 Subject: [PATCH] Improving config handling & startup logic --- Client/controllers/clientController.js | 10 +++++----- Client/utilities/updateConfig.js | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Client/controllers/clientController.js b/Client/controllers/clientController.js index 8e347b5..0c12f58 100644 --- a/Client/controllers/clientController.js +++ b/Client/controllers/clientController.js @@ -7,7 +7,7 @@ const modes = require("../config/modes"); // Modules const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleCommands.js"); // Utilities -const updateConfig = require("../utilities/updateConfig"); +const { updateId, updateConfigFile } = require("../utilities/updateConfig"); const updatePreset = require("../utilities/updatePresets"); const requests = require("../utilities/httpRequests"); @@ -67,17 +67,17 @@ async function checkLocalIP() { exports.checkConfig = async function checkConfig() { if (!config.clientConfig.ip) { const ipAddr = await checkLocalIP(); - updateConfig('ip', ipAddr); + updateConfigFile('ip', ipAddr); } if(!config.clientConfig.name) { const lastOctet = await checkLocalIP().spit('.')[-1]; const clientName = `Radio-Node-${lastOctet}`; - updateConfig('name', clientName); + updateConfigFile('name', clientName); } if(!config.clientConfig.port) { - updateConfig('port', 3010); + updateConfigFile('port', 3010); } } @@ -97,7 +97,7 @@ exports.checkIn = async () => { // Update the client's ID if the server accepted it if (responseObject.statusCode === 202) { config.clientConfig.id = responseObject.body.nodeId; - updateConfig.updateId(responseObject.body.nodeId); + updateId(responseObject.body.nodeId); } }); } diff --git a/Client/utilities/updateConfig.js b/Client/utilities/updateConfig.js index 22d17c4..c1b4945 100644 --- a/Client/utilities/updateConfig.js +++ b/Client/utilities/updateConfig.js @@ -20,10 +20,20 @@ class Options { * @param updatedId The updated ID assigned to the bot */ exports.updateId = (updatedId) => { - const options = new Options("id", updatedId); + this.updateConfig('id', updatedId); +} - updateConfigFile(options, (updatedFiles) => { +/** + * + * @param {string} key The config file key to update with the value + * @param {string} value The value to update the key with + */ +exports.updateConfig = function updateConfig(key, value) { + const options = new Options(key, value); + + this.updateConfigFile(options, (updatedFiles) => { // Do Something + log.DEBUG("Updated config file: ", updatedFiles); }) } @@ -33,7 +43,7 @@ exports.updateId = (updatedId) => { * @param options An instance of the Objects class specified to the key being updated * @param callback Callback when the files have been modified */ -function updateConfigFile(options, callback){ +exports.updateConfigFile = function updateConfigFile(options, callback){ replace(options, (error, changedFiles) => { if (error) return console.error('Error occurred:', error); log.DEBUG('Modified files:', changedFiles);