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.
This commit is contained in:
Markus Mäkelä 2017-07-14 16:36:59 +03:00
parent ec5b0fea39
commit d938dcc701
18 changed files with 116 additions and 89 deletions

View File

@ -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()
}
}

View File

@ -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 ' +
'<hostname>:<port> 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 ' +
'<hostname>:<port> 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})
})
}

View File

@ -40,6 +40,6 @@ exports.builder = function(yargs) {
.usage('Usage: alter <command>')
.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.')
})
}

View File

@ -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 <command>')
.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.')
})
}

View File

@ -25,6 +25,6 @@ exports.builder = function(yargs) {
.usage('Usage: clear <command>')
.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.')
})
}

View File

@ -181,6 +181,6 @@ exports.builder = function(yargs) {
.usage('Usage: create <command>')
.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.')
})
}

View File

@ -36,6 +36,6 @@ exports.builder = function(yargs) {
.usage('Usage: destroy <command>')
.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.')
})
}

View File

@ -48,6 +48,6 @@ exports.builder = function(yargs) {
.usage('Usage: disable <command>')
.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.')
})
}

View File

@ -54,6 +54,6 @@ exports.builder = function(yargs) {
.usage('Usage: enable <command>')
.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.')
})
}

View File

@ -44,6 +44,6 @@ exports.builder = function(yargs) {
.usage('Usage: link <command>')
.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.')
})
}

View File

@ -87,6 +87,6 @@ exports.builder = function(yargs) {
.usage('Usage: list <command>')
.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.')
})
}

View File

@ -24,6 +24,6 @@ exports.builder = function(yargs) {
.usage('Usage: rotate <command>')
.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.')
})
}

View File

@ -25,6 +25,6 @@ exports.builder = function(yargs) {
.usage('Usage: set <command>')
.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.')
})
}

View File

@ -111,6 +111,6 @@ exports.builder = function(yargs) {
.usage('Usage: show <command>')
.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.')
})
}

View File

@ -28,6 +28,6 @@ exports.builder = function(yargs) {
.usage('Usage: start <command>')
.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.')
})
}

View File

@ -28,6 +28,6 @@ exports.builder = function(yargs) {
.usage('Usage: stop <command>')
.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.')
})
}

View File

@ -44,6 +44,6 @@ exports.builder = function(yargs) {
.usage('Usage: unlink <command>')
.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.')
})
}

View File

@ -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) {})