Improving config handling & startup logic

This commit is contained in:
Logan Cusano
2023-05-06 16:40:15 -04:00
parent 6682d97156
commit 7871b07113
2 changed files with 18 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ const modes = require("../config/modes");
// Modules // Modules
const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleCommands.js"); const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleCommands.js");
// Utilities // Utilities
const updateConfig = require("../utilities/updateConfig"); const { updateId, updateConfigFile } = require("../utilities/updateConfig");
const updatePreset = require("../utilities/updatePresets"); const updatePreset = require("../utilities/updatePresets");
const requests = require("../utilities/httpRequests"); const requests = require("../utilities/httpRequests");
@@ -67,17 +67,17 @@ async function checkLocalIP() {
exports.checkConfig = async function checkConfig() { exports.checkConfig = async function checkConfig() {
if (!config.clientConfig.ip) { if (!config.clientConfig.ip) {
const ipAddr = await checkLocalIP(); const ipAddr = await checkLocalIP();
updateConfig('ip', ipAddr); updateConfigFile('ip', ipAddr);
} }
if(!config.clientConfig.name) { if(!config.clientConfig.name) {
const lastOctet = await checkLocalIP().spit('.')[-1]; const lastOctet = await checkLocalIP().spit('.')[-1];
const clientName = `Radio-Node-${lastOctet}`; const clientName = `Radio-Node-${lastOctet}`;
updateConfig('name', clientName); updateConfigFile('name', clientName);
} }
if(!config.clientConfig.port) { 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 // Update the client's ID if the server accepted it
if (responseObject.statusCode === 202) { if (responseObject.statusCode === 202) {
config.clientConfig.id = responseObject.body.nodeId; config.clientConfig.id = responseObject.body.nodeId;
updateConfig.updateId(responseObject.body.nodeId); updateId(responseObject.body.nodeId);
} }
}); });
} }

View File

@@ -20,10 +20,20 @@ class Options {
* @param updatedId The updated ID assigned to the bot * @param updatedId The updated ID assigned to the bot
*/ */
exports.updateId = (updatedId) => { 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 // 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 options An instance of the Objects class specified to the key being updated
* @param callback Callback when the files have been modified * @param callback Callback when the files have been modified
*/ */
function updateConfigFile(options, callback){ exports.updateConfigFile = function updateConfigFile(options, callback){
replace(options, (error, changedFiles) => { replace(options, (error, changedFiles) => {
if (error) return console.error('Error occurred:', error); if (error) return console.error('Error occurred:', error);
log.DEBUG('Modified files:', changedFiles); log.DEBUG('Modified files:', changedFiles);