From 38930e198d6640aa21797227749a2aa3d3908135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Fri, 30 Jun 2017 13:21:01 +0300 Subject: [PATCH] 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. --- client/maxctrl/common.js | 52 +++++++------------------------------- client/maxctrl/lib/list.js | 3 +++ client/maxctrl/lib/show.js | 3 +++ client/maxctrl/maxctrl.js | 40 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 43 deletions(-) diff --git a/client/maxctrl/common.js b/client/maxctrl/common.js index 6844f0f23..606b492d2 100644 --- a/client/maxctrl/common.js +++ b/client/maxctrl/common.js @@ -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 diff --git a/client/maxctrl/lib/list.js b/client/maxctrl/lib/list.js index fe0a4fc7c..36f80fa6b 100644 --- a/client/maxctrl/lib/list.js +++ b/client/maxctrl/lib/list.js @@ -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() } diff --git a/client/maxctrl/lib/show.js b/client/maxctrl/lib/show.js index eb845c618..1c953c1ef 100644 --- a/client/maxctrl/lib/show.js +++ b/client/maxctrl/lib/show.js @@ -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() } diff --git a/client/maxctrl/maxctrl.js b/client/maxctrl/maxctrl.js index 67068f9b6..cd0185f32 100644 --- a/client/maxctrl/maxctrl.js +++ b/client/maxctrl/maxctrl.js @@ -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