From 8146a313f3d5a2daa88296d47ca29ffeedb7fc53 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 11 Nov 2017 23:51:52 +0100 Subject: [PATCH] #59 interface for editing config + more docs --- client.js | 10 +++++++- lib/manager/configManager.js | 16 ++++++++++++- lib/objectOps.js | 45 ++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/client.js b/client.js index 3844b0c3..4377e69f 100644 --- a/client.js +++ b/client.js @@ -154,7 +154,15 @@ if (!command || command == "help" || command == "--help") { if(action == "list" || action == "show" || action == "display"){ configManager.displayConfig(instance); } else if(action == "edit"){ - configManager.editConfig(instance, process.argv); + let newConfigValue = ""; + process.argv.forEach((arg, i)=>{ + if(i >= 8){ + newConfigValue += " "+arg; + } else if(i >= 7){ + newConfigValue += arg; + } + }); + configManager.editConfig(instance, process.argv[6], newConfigValue); } else { usage(instance, tool); } diff --git a/lib/manager/configManager.js b/lib/manager/configManager.js index c0a50ba9..ef068ff1 100644 --- a/lib/manager/configManager.js +++ b/lib/manager/configManager.js @@ -6,7 +6,8 @@ Provides tools for managing both masters and slaves config files. const asTable = require("as-table").configure({delimiter: ' | '}); // internal requires -const config = require("./../../config"); +var config = require("./../../config"); +const objectOps = require("./../objectOps.js"); /** console.log()s each entry in the config @@ -25,3 +26,16 @@ module.exports.displayConfig = function(instance){ console.log(asTable(configArray)); } } +module.exports.editConfig = function(instance, entry, newValue){ + if(!instance || instance == "shared"){ + if(config[entry] && newValue){ + if(objectOps.isJSON(newValue)){ + console.log("Changing config entry "+entry+" from "+JSON.stringify(config[entry])+" to "+JSON.stringify(newValue)); + config[entry] = JSON.parse(newValue); + } else { + console.log("Changing config entry "+entry+" from "+config[entry]+" to '"+newValue+"'"); + config[entry] = newValue; + } + } + } +}; diff --git a/lib/objectOps.js b/lib/objectOps.js index e5d4d716..a4286c07 100644 --- a/lib/objectOps.js +++ b/lib/objectOps.js @@ -1,7 +1,44 @@ -var deepclone = function(obj){ +/** +Helper functions related to objects +@module objectOps +*/ + +/** +Deep clones an object, is actually just JSON.parse(JSON.stringify()) but looks cleaner. +@param {object} obj to clone +@returns {object} + +@example +a = {"thingy":"with a value"}; +b = a; +a == b; // true +c = objectOps.deepclone(a); +a == c; // false +*/ +module.exports.deepclone = function(obj){ return JSON.parse(JSON.stringify(obj)); } +/** +Checks if a string is valid JSON using try/catch. If passed an object it will always return true. +@param {string} string to verify +@returns {boolean} -module.exports = { - deepclone:deepclone, -}; \ No newline at end of file +@example +objectOps.isJSON("99 bottles of beer"); // false +objectOps.isJSON("99"); // true +objectOps.isJSON({"thingy":"that is an object"}); // true +objectOps.isJSON(JSON.stringify({"thingy":"that is an object"})); // true +*/ +module.exports.isJSON = function(string){ + let stringIsJson = false; + let x + try { + x = JSON.parse(string); + } catch (e){ + return false; + } + if(typeof x == "object" || typeof string == "object"){ + stringIsJson = true; + } + return stringIsJson; +}