diff --git a/packages/ctl/ctl.js b/packages/ctl/ctl.js index 7c41f0fc..3966abb8 100755 --- a/packages/ctl/ctl.js +++ b/packages/ctl/ctl.js @@ -595,7 +595,7 @@ async function startControl() { type: "string", }) .command("plugin", "Manage available plugins", libSharedCommands.pluginCommand) - .command("control-config", "Manage Control config", libConfig.configCommand) + .command("control-config", "Manage Control config", libSharedCommands.configCommand) .wrap(yargs.terminalWidth()) .help(false) // Disable help to avoid triggering it on the first parse. ; @@ -681,7 +681,7 @@ async function startControl() { // Handle the control-config command before trying to connect. if (args._[0] === "control-config") { - await libConfig.handleConfigCommand(args, controlConfig, args.config); + await libSharedCommands.handleConfigCommand(args, controlConfig, args.config); return; } diff --git a/packages/lib/config/definitions.js b/packages/lib/config/definitions.js index 27edcbe7..c95c0fb3 100644 --- a/packages/lib/config/definitions.js +++ b/packages/lib/config/definitions.js @@ -1,8 +1,6 @@ // Core definitions for the configuration system "use strict"; -const fs = require("fs-extra"); - const classes = require("./classes"); @@ -379,77 +377,6 @@ function finalizeConfigs() { ControlConfig.finalize(); } -/** - * Yargs config command - * - * Can be passed to yargs.command to implement a config command. Use - * handleConfigCommand to do the requested action. - * - * @param {Object} yargs - yargs command builder. - * @memberof module:lib/config - */ -function configCommand(yargs) { - yargs - .command("set [value]", "Set config field") - .command("show ", "Show value of the given config field") - .command("list", "List all configuration fields and their values") - .demandCommand(1, "You need to specify a command to run") - .help() - .strict() - ; -} - -/** - * Handle yargs command - * - * Handle the actions that are made available by configCommand. - * - * @param {Object} args - yargs args object. - * @param {module:lib/config.Config} instance - Config instance. - * @param {string} configPath - Path to configuration file. - * @memberof module:lib/config - */ -async function handleConfigCommand(args, instance, configPath) { - let command = args._[1]; - - if (command === "list") { - for (let GroupClass of instance.constructor.groups.values()) { - for (let def of GroupClass.definitions.values()) { - let value = instance.get(def.fullName); - console.log(`${def.fullName} ${JSON.stringify(value)}`); - } - } - - } else if (command === "show") { - try { - console.log(instance.get(args.field)); - } catch (err) { - if (err instanceof classes.InvalidField) { - console.error(err.message); - } else { - throw err; - } - } - - } else if (command === "set") { - if (args.value === undefined) { - args.value = null; - } - - try { - instance.set(args.field, args.value); - await fs.outputFile(configPath, JSON.stringify(instance.serialize(), null, 4)); - } catch (err) { - if (err instanceof classes.InvalidField || err instanceof classes.InvalidValue) { - console.error(err.message); - } else { - throw err; - } - } - } -} - - module.exports = { MasterGroup, SlaveGroup, @@ -464,6 +391,4 @@ module.exports = { registerPluginConfigGroups, finalizeConfigs, - configCommand, - handleConfigCommand, }; diff --git a/packages/lib/shared_commands.js b/packages/lib/shared_commands.js index 29550cb6..0bd370ce 100644 --- a/packages/lib/shared_commands.js +++ b/packages/lib/shared_commands.js @@ -6,6 +6,9 @@ const fs = require("fs-extra"); const path = require("path"); +const libConfig = require("./config"); + + /** * Yargs plugin command * @@ -80,7 +83,80 @@ async function handlePluginCommand(args, pluginList, pluginListPath) { } } + +/** + * Yargs config command + * + * Can be passed to yargs.command to implement a config command. Use + * handleConfigCommand to do the requested action. + * + * @param {Object} yargs - yargs command builder. + */ +function configCommand(yargs) { + yargs + .command("set [value]", "Set config field") + .command("show ", "Show value of the given config field") + .command("list", "List all configuration fields and their values") + .demandCommand(1, "You need to specify a command to run") + .help() + .strict() + ; +} + +/** + * Handle yargs command + * + * Handle the actions that are made available by configCommand. + * + * @param {Object} args - yargs args object. + * @param {module:lib/config.Config} instance - Config instance. + * @param {string} configPath - Path to configuration file. + */ +async function handleConfigCommand(args, instance, configPath) { + let command = args._[1]; + + if (command === "list") { + for (let GroupClass of instance.constructor.groups.values()) { + for (let def of GroupClass.definitions.values()) { + let value = instance.get(def.fullName); + console.log(`${def.fullName} ${JSON.stringify(value)}`); + } + } + + } else if (command === "show") { + try { + console.log(instance.get(args.field)); + } catch (err) { + if (err instanceof libConfig.InvalidField) { + console.error(err.message); + } else { + throw err; + } + } + + } else if (command === "set") { + if (args.value === undefined) { + args.value = null; + } + + try { + instance.set(args.field, args.value); + await fs.outputFile(configPath, JSON.stringify(instance.serialize(), null, 4)); + } catch (err) { + if (err instanceof libConfig.InvalidField || err instanceof libConfig.InvalidValue) { + console.error(err.message); + } else { + throw err; + } + } + } +} + + module.exports = { pluginCommand, handlePluginCommand, + + configCommand, + handleConfigCommand, }; diff --git a/packages/master/master.js b/packages/master/master.js index cbdbf0ac..f880e59d 100755 --- a/packages/master/master.js +++ b/packages/master/master.js @@ -1574,7 +1574,7 @@ async function startServer() { type: "string", }) .command("plugin", "Manage available plugins", libSharedCommands.pluginCommand) - .command("config", "Manage Master config", libConfig.configCommand) + .command("config", "Manage Master config", libSharedCommands.configCommand) .command("bootstrap", "Bootstrap access to cluster", yargs => { yargs .command("create-admin ", "Create a cluster admin") @@ -1641,7 +1641,7 @@ async function startServer() { } if (command === "config") { - await libConfig.handleConfigCommand(args, masterConfig, masterConfigPath); + await libSharedCommands.handleConfigCommand(args, masterConfig, masterConfigPath); return; } else if (command === "bootstrap") { diff --git a/packages/slave/slave.js b/packages/slave/slave.js index e6746b69..37c13d7b 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -1395,7 +1395,7 @@ async function startSlave() { type: "string", }) .command("plugin", "Manage available plugins", libSharedCommands.pluginCommand) - .command("config", "Manage Slave config", libConfig.configCommand) + .command("config", "Manage Slave config", libSharedCommands.configCommand) .command("run", "Run slave") .demandCommand(1, "You need to specify a command to run") .strict() @@ -1440,7 +1440,7 @@ async function startSlave() { } if (command === "config") { - await libConfig.handleConfigCommand(args, slaveConfig, args.config); + await libSharedCommands.handleConfigCommand(args, slaveConfig, args.config); return; }