MXS-1300: Fix duplicated options

Moved the option declaration to the main source file. Added default
functions for all modules to catch unknown command invokations. Cleaned up
and exposed more ways to use the doRequest function.
This commit is contained in:
Markus Mäkelä 2017-06-30 13:21:01 +03:00
parent 63d2eee0e3
commit 38930e198d
4 changed files with 55 additions and 43 deletions

View File

@ -18,45 +18,9 @@ var Table = require('cli-table');
var assert = require('assert')
module.exports = function() {
const maxctrl_version = '1.0.0';
// Common options for all commands
this.program = require('yargs');
this.program
.group(['u', 'p', 'h', 'p', 'P', 's'], 'Global Options:')
.option('u', {
alias:'user',
global: true,
default: 'mariadb',
describe: 'Username to use',
type: 'string'
})
.option('p', {
alias: 'password',
describe: 'Password for the user',
default: 'admin',
type: 'string'
})
.option('h', {
alias: 'host',
describe: 'The hostname or address where MaxScale is located',
default: 'localhost',
type: 'string'
})
.option('P', {
alias: 'port',
describe: 'The port where MaxScale REST API listens on',
default: 8989,
type: 'number'
})
.option('s', {
alias: 'secure',
describe: 'Enable TLS encryption of connections',
default: 'false',
type: 'boolean'
})
.version(maxctrl_version)
.help()
// Request a resource collection and format it as a table
this.getCollection = function (resource, fields) {
@ -130,16 +94,18 @@ module.exports = function() {
}
// Helper for executing requests and handling their responses
this.doRequest = function(resource, cb) {
request({
uri: getUri(resource),
json: true
}, function(err, resp, res) {
this.doRequest = function(resource, cb, obj) {
args = obj || {}
args.uri = getUri(resource),
args.json = true
request(args, function(err, resp, res) {
if (err) {
// Failed to request
console.log("Error:", JSON.stringify(err, null, 4))
} else if (resp.statusCode == 200) {
// Reuqest OK, returns data
} else if (resp.statusCode == 200 && cb) {
// Request OK, returns data
cb(res)
} else if (resp.statusCode == 204) {
// Request OK, no data

View File

@ -65,5 +65,8 @@ exports.builder = function(yargs) {
{'Version': 'attributes.version'}
])
})
.command('*', 'the default command', {}, () => {
console.log("Unknown command. See output of 'help list' for a list of commands.")
})
.help()
}

View File

@ -83,5 +83,8 @@ exports.builder = function(yargs) {
{'Commands': 'attributes.commands'}
])
})
.command('*', 'the default command', {}, () => {
console.log("Unknown command. See output of 'help show' for a list of commands.")
})
.help()
}

View File

@ -14,8 +14,48 @@ require('./common.js')()
'use strict';
const maxctrl_version = '1.0.0';
program
.version(maxctrl_version)
.group(['u', 'p', 'h', 'p', 'P', 's'], 'Global Options:')
.option('u', {
alias:'user',
global: true,
default: 'mariadb',
describe: 'Username to use',
type: 'string'
})
.option('p', {
alias: 'password',
describe: 'Password for the user',
default: 'admin',
type: 'string'
})
.option('h', {
alias: 'host',
describe: 'The hostname or address where MaxScale is located',
default: 'localhost',
type: 'string'
})
.option('P', {
alias: 'port',
describe: 'The port where MaxScale REST API listens on',
default: 8989,
type: 'number'
})
.option('s', {
alias: 'secure',
describe: 'Enable TLS encryption of connections',
default: 'false',
type: 'boolean'
})
.command(require('./lib/list.js'))
.command(require('./lib/show.js'))
.recommendCommands()
.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.")
})
.argv