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.
This commit is contained in:
Markus Mäkelä
2017-07-12 16:53:53 +03:00
parent 6c601955d6
commit a082871726
2 changed files with 81 additions and 58 deletions

View File

@ -160,6 +160,9 @@ module.exports = function() {
}) })
}) })
}) })
.catch(function(err) {
logError(JSON.stringify(err, null, 4))
})
} }
this.updateValue = function(resource, key, value) { this.updateValue = function(resource, key, value) {

View File

@ -11,66 +11,86 @@
* Public License. * Public License.
*/ */
require('./common.js')() require('./common.js')()
var fs = require('fs')
const maxctrl_version = '1.0.0'; const maxctrl_version = '1.0.0';
module.exports = function(argv) { 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 ' +
'<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'
})
.command(require('./lib/list.js')) // Mangle the arguments if we are being called from the command line
.command(require('./lib/show.js')) if (argv[0] == process.execPath) {
.command(require('./lib/set.js')) argv.shift()
.command(require('./lib/clear.js')) }
.command(require('./lib/enable.js'))
.command(require('./lib/disable.js')) try {
.command(require('./lib/create.js')) while (argv.length > 0) {
.command(require('./lib/destroy.js')) fs.accessSync(argv[0])
.command(require('./lib/link.js')) argv.shift()
.command(require('./lib/unlink.js')) }
.command(require('./lib/start.js')) } catch (err) { }
.command(require('./lib/stop.js'))
.command(require('./lib/alter.js')) return new Promise(function(resolve, reject) {
.command(require('./lib/rotate.js')) program
.command(require('./lib/call.js')) .version(maxctrl_version)
.help() .group(['u', 'p', 'h', 's', 't'], 'Global Options:')
.demandCommand(1, 'At least one command is required') .option('u', {
.command('*', 'the default command', {}, () => { alias:'user',
console.log('Unknown command. See output of `help` for a list of commands.') global: true,
}) default: 'admin',
.parse(argv) describe: 'Username to use',
.argv 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'
})
.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);
}
})
})
} }