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:
@ -14,19 +14,27 @@
|
|||||||
var request = require('request-promise-native');
|
var request = require('request-promise-native');
|
||||||
var colors = require('colors/safe');
|
var colors = require('colors/safe');
|
||||||
var Table = require('cli-table');
|
var Table = require('cli-table');
|
||||||
|
var consoleLib = require('console')
|
||||||
|
var fs = require('fs')
|
||||||
|
|
||||||
module.exports = function() {
|
module.exports = function() {
|
||||||
|
|
||||||
this._ = require('lodash-getpath')
|
this._ = require('lodash-getpath')
|
||||||
|
|
||||||
|
this.logger = console
|
||||||
|
|
||||||
this.maxctrl = function(argv) {
|
this.maxctrl = function(argv) {
|
||||||
|
|
||||||
|
if (argv.quiet) {
|
||||||
|
this.logger = new consoleLib.Console(fs.createWriteStream('/dev/null'), process.stderr)
|
||||||
|
}
|
||||||
|
|
||||||
this.argv = argv
|
this.argv = argv
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request a resource collection and format it as a table
|
// Request a resource collection and format it as a table
|
||||||
this.getCollection = function (resource, fields) {
|
this.getCollection = function (resource, fields) {
|
||||||
|
|
||||||
doRequest(resource, function(res) {
|
doRequest(resource, function(res) {
|
||||||
|
|
||||||
var header = []
|
var header = []
|
||||||
@ -52,7 +60,7 @@ module.exports = function() {
|
|||||||
table.push(row)
|
table.push(row)
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(table.toString())
|
logger.log(table.toString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +94,7 @@ module.exports = function() {
|
|||||||
table.push(row)
|
table.push(row)
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(table.toString())
|
logger.log(table.toString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +120,7 @@ module.exports = function() {
|
|||||||
table.push(o)
|
table.push(o)
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(table.toString())
|
logger.log(table.toString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,17 +155,24 @@ module.exports = function() {
|
|||||||
.then(function(res) {
|
.then(function(res) {
|
||||||
if (res && cb) {
|
if (res && cb) {
|
||||||
// Request OK, returns data
|
// Request OK, returns data
|
||||||
if (!this.argv.tsv) {
|
if (!this.argv.tsv && this.argv.hosts.length > 1) {
|
||||||
console.log(colors.yellow(host) + ':')
|
logger.log(colors.yellow(host) + ':')
|
||||||
}
|
}
|
||||||
|
|
||||||
return cb(res)
|
return cb(res)
|
||||||
} else {
|
} else {
|
||||||
// Request OK, no data or data is ignored
|
// 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) {
|
}, function(err) {
|
||||||
console.log(colors.yellow(host) + ':')
|
if (this.argv.hosts.length > 1) {
|
||||||
|
logger.log(colors.yellow(host) + ':')
|
||||||
|
}
|
||||||
if (err.response.body) {
|
if (err.response.body) {
|
||||||
logError(JSON.stringify(err.response.body, null, 4))
|
logError(JSON.stringify(err.response.body, null, 4))
|
||||||
} else {
|
} else {
|
||||||
@ -193,11 +208,11 @@ module.exports = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logError = function(err) {
|
this.logError = function(err) {
|
||||||
console.log(colors.red('Error:'), err)
|
this.logger.error(colors.red('Error:'), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.error = function(err) {
|
this.error = function(err) {
|
||||||
console.log(colors.red('Error:'), err)
|
logger.log(colors.red('Error:'), err)
|
||||||
this.argv.reject()
|
this.argv.reject()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,12 +18,9 @@ const maxctrl_version = '1.0.0';
|
|||||||
|
|
||||||
require('./common.js')()
|
require('./common.js')()
|
||||||
|
|
||||||
module.exports = function(argv) {
|
program
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
|
||||||
program
|
|
||||||
.version(maxctrl_version)
|
.version(maxctrl_version)
|
||||||
.group(['u', 'p', 'h', 's', 't', 'tsv'], 'Global Options:')
|
.group(['u', 'p', 'h', 's', 't', 'q', 'tsv'], 'Global Options:')
|
||||||
.option('u', {
|
.option('u', {
|
||||||
alias:'user',
|
alias:'user',
|
||||||
global: true,
|
global: true,
|
||||||
@ -56,6 +53,12 @@ module.exports = function(argv) {
|
|||||||
default: '10000',
|
default: '10000',
|
||||||
type: 'number'
|
type: 'number'
|
||||||
})
|
})
|
||||||
|
.option('q', {
|
||||||
|
alias: 'quiet',
|
||||||
|
describe: 'Output only errors',
|
||||||
|
default: 'false',
|
||||||
|
type: 'boolean'
|
||||||
|
})
|
||||||
.option('tsv', {
|
.option('tsv', {
|
||||||
describe: 'Print tab separated output',
|
describe: 'Print tab separated output',
|
||||||
default: 'false',
|
default: 'false',
|
||||||
@ -82,6 +85,15 @@ module.exports = function(argv) {
|
|||||||
.command('*', 'the default command', {}, () => {
|
.command('*', 'the default command', {}, () => {
|
||||||
console.log('Unknown command. See output of `help` for a list of commands.')
|
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
|
||||||
.parse(argv, {resolve: resolve, reject: reject})
|
.parse(argv, {resolve: resolve, reject: reject})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,6 +40,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: alter <command>')
|
.usage('Usage: alter <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,13 +35,13 @@ exports.builder = function(yargs) {
|
|||||||
return maxctrl
|
return maxctrl
|
||||||
.doAsyncRequest('maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'),
|
.doAsyncRequest('maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'),
|
||||||
function(resp) {
|
function(resp) {
|
||||||
console.log(JSON.stringify(resp, null, 4))
|
logger.log(JSON.stringify(resp, null, 4))
|
||||||
}, { method: verb })
|
}, { method: verb })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.usage('Usage: call <command>')
|
.usage('Usage: call <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: clear <command>')
|
.usage('Usage: clear <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -181,6 +181,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: create <command>')
|
.usage('Usage: create <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,6 +36,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: destroy <command>')
|
.usage('Usage: destroy <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,6 +48,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: disable <command>')
|
.usage('Usage: disable <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,6 +54,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: enable <command>')
|
.usage('Usage: enable <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,6 +44,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: link <command>')
|
.usage('Usage: link <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -87,6 +87,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: list <command>')
|
.usage('Usage: list <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,6 +24,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: rotate <command>')
|
.usage('Usage: rotate <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: set <command>')
|
.usage('Usage: set <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -111,6 +111,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: show <command>')
|
.usage('Usage: show <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: start <command>')
|
.usage('Usage: start <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: stop <command>')
|
.usage('Usage: stop <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,6 +44,6 @@ exports.builder = function(yargs) {
|
|||||||
.usage('Usage: unlink <command>')
|
.usage('Usage: unlink <command>')
|
||||||
.help()
|
.help()
|
||||||
.command('*', 'the default command', {}, () => {
|
.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.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,5 +22,5 @@ if (process.argv[0] == process.execPath) {
|
|||||||
process.argv.shift()
|
process.argv.shift()
|
||||||
}
|
}
|
||||||
|
|
||||||
maxctrl(process.argv)
|
maxctrl.execute(process.argv)
|
||||||
.then(function(out) {}, function(out) {})
|
.then(function(out) {}, function(out) {})
|
||||||
|
|||||||
Reference in New Issue
Block a user