From a0828717269aa928f778162f24b2001ad7e4a3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 12 Jul 2017 16:53:53 +0300 Subject: [PATCH] MXS-1300: Refactor the way maxctrl is invoked Making the invocation explicit should allow testing without actually starting the process fromt the command line. Also returning a promise allows chaining of commands for testing and verification. --- maxctrl/common.js | 3 + maxctrl/core.js | 136 ++++++++++++++++++++++++++-------------------- 2 files changed, 81 insertions(+), 58 deletions(-) diff --git a/maxctrl/common.js b/maxctrl/common.js index 96068a42c..968414438 100644 --- a/maxctrl/common.js +++ b/maxctrl/common.js @@ -160,6 +160,9 @@ module.exports = function() { }) }) }) + .catch(function(err) { + logError(JSON.stringify(err, null, 4)) + }) } this.updateValue = function(resource, key, value) { diff --git a/maxctrl/core.js b/maxctrl/core.js index 1aa77ba0f..dd4114afb 100644 --- a/maxctrl/core.js +++ b/maxctrl/core.js @@ -11,66 +11,86 @@ * Public License. */ require('./common.js')() - +var fs = require('fs') const maxctrl_version = '1.0.0'; module.exports = function(argv) { - program - .version(maxctrl_version) - .group(['u', 'p', 'h', 's', 't'], 'Global Options:') - .option('u', { - alias:'user', - global: true, - default: 'admin', - describe: 'Username to use', - type: 'string' - }) - .option('p', { - alias: 'password', - describe: 'Password for the user', - default: 'mariadb', - type: 'string' - }) - .option('h', { - alias: 'hosts', - describe: 'List of MaxScale hosts. The hosts must be in ' + - ': format and each host must be separated by spaces.', - default: 'localhost:8989', - type: 'array' - }) - .option('s', { - alias: 'secure', - describe: 'Enable HTTPS requests', - default: 'false', - type: 'boolean' - }) - .option('t', { - alias: 'timeout', - describe: 'Request timeout in milliseconds', - default: '10000', - type: 'number' - }) - .command(require('./lib/list.js')) - .command(require('./lib/show.js')) - .command(require('./lib/set.js')) - .command(require('./lib/clear.js')) - .command(require('./lib/enable.js')) - .command(require('./lib/disable.js')) - .command(require('./lib/create.js')) - .command(require('./lib/destroy.js')) - .command(require('./lib/link.js')) - .command(require('./lib/unlink.js')) - .command(require('./lib/start.js')) - .command(require('./lib/stop.js')) - .command(require('./lib/alter.js')) - .command(require('./lib/rotate.js')) - .command(require('./lib/call.js')) - .help() - .demandCommand(1, 'At least one command is required') - .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help` for a list of commands.') - }) - .parse(argv) - .argv + // Mangle the arguments if we are being called from the command line + if (argv[0] == process.execPath) { + argv.shift() + } + + try { + while (argv.length > 0) { + fs.accessSync(argv[0]) + argv.shift() + } + } catch (err) { } + + return new Promise(function(resolve, reject) { + program + .version(maxctrl_version) + .group(['u', 'p', 'h', 's', 't'], 'Global Options:') + .option('u', { + alias:'user', + global: true, + default: 'admin', + describe: 'Username to use', + type: 'string' + }) + .option('p', { + alias: 'password', + describe: 'Password for the user', + default: 'mariadb', + type: 'string' + }) + .option('h', { + alias: 'hosts', + describe: 'List of MaxScale hosts. The hosts must be in ' + + ': format and each host must be separated by spaces.', + default: 'localhost:8989', + type: 'array' + }) + .option('s', { + alias: 'secure', + describe: 'Enable HTTPS requests', + default: 'false', + type: 'boolean' + }) + .option('t', { + alias: 'timeout', + describe: 'Request timeout in milliseconds', + default: '10000', + type: 'number' + }) + + .command(require('./lib/list.js')) + .command(require('./lib/show.js')) + .command(require('./lib/set.js')) + .command(require('./lib/clear.js')) + .command(require('./lib/enable.js')) + .command(require('./lib/disable.js')) + .command(require('./lib/create.js')) + .command(require('./lib/destroy.js')) + .command(require('./lib/link.js')) + .command(require('./lib/unlink.js')) + .command(require('./lib/start.js')) + .command(require('./lib/stop.js')) + .command(require('./lib/alter.js')) + .command(require('./lib/rotate.js')) + .command(require('./lib/call.js')) + .help() + .demandCommand(1, 'At least one command is required') + .command('*', 'the default command', {}, () => { + console.log('Unknown command. See output of `help` for a list of commands.') + }) + .parse(process.argv, function(err, argv, output) { + if (err) { + reject(output) + } else { + resolve(output); + } + }) + }) }