From d938dcc7014c64942a2620ac1d9b64fcd7dfa0c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Fri, 14 Jul 2017 16:36:59 +0300 Subject: [PATCH] MXS-1300: Make output optional Minor refactoring to the core library to allow multiple calls from within the same program. Added --quiet option to silence output so that tests aren't so verbose. Currently this only works on UNIX based systems. --- maxctrl/common.js | 37 ++++++++---- maxctrl/core.js | 134 ++++++++++++++++++++++------------------- maxctrl/lib/alter.js | 2 +- maxctrl/lib/call.js | 4 +- maxctrl/lib/clear.js | 2 +- maxctrl/lib/create.js | 2 +- maxctrl/lib/destroy.js | 2 +- maxctrl/lib/disable.js | 2 +- maxctrl/lib/enable.js | 2 +- maxctrl/lib/link.js | 2 +- maxctrl/lib/list.js | 2 +- maxctrl/lib/rotate.js | 2 +- maxctrl/lib/set.js | 2 +- maxctrl/lib/show.js | 2 +- maxctrl/lib/start.js | 2 +- maxctrl/lib/stop.js | 2 +- maxctrl/lib/unlink.js | 2 +- maxctrl/maxctrl.js | 2 +- 18 files changed, 116 insertions(+), 89 deletions(-) diff --git a/maxctrl/common.js b/maxctrl/common.js index 7b874d9f6..8032af7d7 100644 --- a/maxctrl/common.js +++ b/maxctrl/common.js @@ -14,19 +14,27 @@ var request = require('request-promise-native'); var colors = require('colors/safe'); var Table = require('cli-table'); +var consoleLib = require('console') +var fs = require('fs') module.exports = function() { this._ = require('lodash-getpath') + this.logger = console + this.maxctrl = function(argv) { + + if (argv.quiet) { + this.logger = new consoleLib.Console(fs.createWriteStream('/dev/null'), process.stderr) + } + this.argv = argv return this } // Request a resource collection and format it as a table this.getCollection = function (resource, fields) { - doRequest(resource, function(res) { var header = [] @@ -52,7 +60,7 @@ module.exports = function() { table.push(row) }) - console.log(table.toString()) + logger.log(table.toString()) }) } @@ -86,7 +94,7 @@ module.exports = function() { table.push(row) }) - console.log(table.toString()) + logger.log(table.toString()) }) } @@ -112,7 +120,7 @@ module.exports = function() { table.push(o) }) - console.log(table.toString()) + logger.log(table.toString()) }) } @@ -147,17 +155,24 @@ module.exports = function() { .then(function(res) { if (res && cb) { // Request OK, returns data - if (!this.argv.tsv) { - console.log(colors.yellow(host) + ':') + if (!this.argv.tsv && this.argv.hosts.length > 1) { + logger.log(colors.yellow(host) + ':') } + return cb(res) } else { // Request OK, no data or data is ignored - console.log(colors.yellow(host) + ': ' + colors.green('OK')) + if (this.argv.hosts.length > 1) { + logger.log(colors.yellow(host) + ': ' + colors.green('OK')) + } else { + logger.log(colors.green('OK')) + } + return Promise.resolve() } - return Promise.resolve() }, function(err) { - console.log(colors.yellow(host) + ':') + if (this.argv.hosts.length > 1) { + logger.log(colors.yellow(host) + ':') + } if (err.response.body) { logError(JSON.stringify(err.response.body, null, 4)) } else { @@ -193,11 +208,11 @@ module.exports = function() { } this.logError = function(err) { - console.log(colors.red('Error:'), err) + this.logger.error(colors.red('Error:'), err) } this.error = function(err) { - console.log(colors.red('Error:'), err) + logger.log(colors.red('Error:'), err) this.argv.reject() } } diff --git a/maxctrl/core.js b/maxctrl/core.js index 2fb142d34..bf8fadf82 100644 --- a/maxctrl/core.js +++ b/maxctrl/core.js @@ -18,70 +18,82 @@ const maxctrl_version = '1.0.0'; require('./common.js')() -module.exports = function(argv) { +program + .version(maxctrl_version) + .group(['u', 'p', 'h', 's', 't', 'q', 'tsv'], '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' + }) + .option('q', { + alias: 'quiet', + describe: 'Output only errors', + default: 'false', + type: 'boolean' + }) + .option('tsv', { + describe: 'Print tab separated output', + default: 'false', + type: 'boolean' + }) + + .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.') + }) + +module.exports.execute = function(argv, opts) { + if (opts && opts.extra_args) { + // Add extra options to the end of the argument list + argv = argv.concat(opts.extra_args) + } return new Promise(function(resolve, reject) { program - .version(maxctrl_version) - .group(['u', 'p', 'h', 's', 't', 'tsv'], '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' - }) - .option('tsv', { - describe: 'Print tab separated output', - default: 'false', - type: 'boolean' - }) - - .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, {resolve: resolve, reject: reject}) }) } diff --git a/maxctrl/lib/alter.js b/maxctrl/lib/alter.js index 3355886ae..fb1c4732d 100644 --- a/maxctrl/lib/alter.js +++ b/maxctrl/lib/alter.js @@ -40,6 +40,6 @@ exports.builder = function(yargs) { .usage('Usage: alter ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help alter` for a list of commands.') + logger.log('Unknown command. See output of `help alter` for a list of commands.') }) } diff --git a/maxctrl/lib/call.js b/maxctrl/lib/call.js index c42833d65..6a0e4bd3a 100644 --- a/maxctrl/lib/call.js +++ b/maxctrl/lib/call.js @@ -35,13 +35,13 @@ exports.builder = function(yargs) { return maxctrl .doAsyncRequest('maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'), function(resp) { - console.log(JSON.stringify(resp, null, 4)) + logger.log(JSON.stringify(resp, null, 4)) }, { method: verb }) }) }) .usage('Usage: call ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help call` for a list of commands.') + logger.log('Unknown command. See output of `help call` for a list of commands.') }) } diff --git a/maxctrl/lib/clear.js b/maxctrl/lib/clear.js index dc9adc216..6611dfcf9 100644 --- a/maxctrl/lib/clear.js +++ b/maxctrl/lib/clear.js @@ -25,6 +25,6 @@ exports.builder = function(yargs) { .usage('Usage: clear ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help clear` for a list of commands.') + logger.log('Unknown command. See output of `help clear` for a list of commands.') }) } diff --git a/maxctrl/lib/create.js b/maxctrl/lib/create.js index 329e418b8..2721ef95c 100644 --- a/maxctrl/lib/create.js +++ b/maxctrl/lib/create.js @@ -181,6 +181,6 @@ exports.builder = function(yargs) { .usage('Usage: create ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help create` for a list of commands.') + logger.log('Unknown command. See output of `help create` for a list of commands.') }) } diff --git a/maxctrl/lib/destroy.js b/maxctrl/lib/destroy.js index 8561747f0..41e8dd462 100644 --- a/maxctrl/lib/destroy.js +++ b/maxctrl/lib/destroy.js @@ -36,6 +36,6 @@ exports.builder = function(yargs) { .usage('Usage: destroy ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help destroy` for a list of commands.') + logger.log('Unknown command. See output of `help destroy` for a list of commands.') }) } diff --git a/maxctrl/lib/disable.js b/maxctrl/lib/disable.js index 26744441f..0eed6590c 100644 --- a/maxctrl/lib/disable.js +++ b/maxctrl/lib/disable.js @@ -48,6 +48,6 @@ exports.builder = function(yargs) { .usage('Usage: disable ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help disable` for a list of commands.') + logger.log('Unknown command. See output of `help disable` for a list of commands.') }) } diff --git a/maxctrl/lib/enable.js b/maxctrl/lib/enable.js index c87943315..c51032691 100644 --- a/maxctrl/lib/enable.js +++ b/maxctrl/lib/enable.js @@ -54,6 +54,6 @@ exports.builder = function(yargs) { .usage('Usage: enable ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help enable` for a list of commands.') + logger.log('Unknown command. See output of `help enable` for a list of commands.') }) } diff --git a/maxctrl/lib/link.js b/maxctrl/lib/link.js index d7011c761..0a78942fa 100644 --- a/maxctrl/lib/link.js +++ b/maxctrl/lib/link.js @@ -44,6 +44,6 @@ exports.builder = function(yargs) { .usage('Usage: link ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help link` for a list of commands.') + logger.log('Unknown command. See output of `help link` for a list of commands.') }) } diff --git a/maxctrl/lib/list.js b/maxctrl/lib/list.js index 59fd36b8d..e2a6f0ea9 100644 --- a/maxctrl/lib/list.js +++ b/maxctrl/lib/list.js @@ -87,6 +87,6 @@ exports.builder = function(yargs) { .usage('Usage: list ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help list` for a list of commands.') + logger.log('Unknown command. See output of `help list` for a list of commands.') }) } diff --git a/maxctrl/lib/rotate.js b/maxctrl/lib/rotate.js index 5b13e5a3f..44ce3b67c 100644 --- a/maxctrl/lib/rotate.js +++ b/maxctrl/lib/rotate.js @@ -24,6 +24,6 @@ exports.builder = function(yargs) { .usage('Usage: rotate ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help rotate` for a list of commands.') + logger.log('Unknown command. See output of `help rotate` for a list of commands.') }) } diff --git a/maxctrl/lib/set.js b/maxctrl/lib/set.js index f3e870c5e..f0109a179 100644 --- a/maxctrl/lib/set.js +++ b/maxctrl/lib/set.js @@ -25,6 +25,6 @@ exports.builder = function(yargs) { .usage('Usage: set ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help set` for a list of commands.') + logger.log('Unknown command. See output of `help set` for a list of commands.') }) } diff --git a/maxctrl/lib/show.js b/maxctrl/lib/show.js index b2776d5db..0763fca1d 100644 --- a/maxctrl/lib/show.js +++ b/maxctrl/lib/show.js @@ -111,6 +111,6 @@ exports.builder = function(yargs) { .usage('Usage: show ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help show` for a list of commands.') + logger.log('Unknown command. See output of `help show` for a list of commands.') }) } diff --git a/maxctrl/lib/start.js b/maxctrl/lib/start.js index ed24b8e89..ca4fa6bff 100644 --- a/maxctrl/lib/start.js +++ b/maxctrl/lib/start.js @@ -28,6 +28,6 @@ exports.builder = function(yargs) { .usage('Usage: start ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help start` for a list of commands.') + logger.log('Unknown command. See output of `help start` for a list of commands.') }) } diff --git a/maxctrl/lib/stop.js b/maxctrl/lib/stop.js index fffd82f51..219eccc15 100644 --- a/maxctrl/lib/stop.js +++ b/maxctrl/lib/stop.js @@ -28,6 +28,6 @@ exports.builder = function(yargs) { .usage('Usage: stop ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help stop` for a list of commands.') + logger.log('Unknown command. See output of `help stop` for a list of commands.') }) } diff --git a/maxctrl/lib/unlink.js b/maxctrl/lib/unlink.js index c1aab0da2..cc076e698 100644 --- a/maxctrl/lib/unlink.js +++ b/maxctrl/lib/unlink.js @@ -44,6 +44,6 @@ exports.builder = function(yargs) { .usage('Usage: unlink ') .help() .command('*', 'the default command', {}, () => { - console.log('Unknown command. See output of `help unlink` for a list of commands.') + logger.log('Unknown command. See output of `help unlink` for a list of commands.') }) } diff --git a/maxctrl/maxctrl.js b/maxctrl/maxctrl.js index 724ef6289..fc80de456 100644 --- a/maxctrl/maxctrl.js +++ b/maxctrl/maxctrl.js @@ -22,5 +22,5 @@ if (process.argv[0] == process.execPath) { process.argv.shift() } -maxctrl(process.argv) +maxctrl.execute(process.argv) .then(function(out) {}, function(out) {})