Add sum helper to api command
Calculating sums for values is useful for monitoring and scripting purposes.
This commit is contained in:
@ -17,6 +17,13 @@ exports.desc = 'Raw REST API access'
|
|||||||
exports.handler = function() {}
|
exports.handler = function() {}
|
||||||
exports.builder = function(yargs) {
|
exports.builder = function(yargs) {
|
||||||
yargs
|
yargs
|
||||||
|
.group(['sum'], 'API options:')
|
||||||
|
.option('sum', {
|
||||||
|
describe: 'Calculate sum of API result. Only works for arrays of numbers ' +
|
||||||
|
'e.g. `api get --sum servers data[].attributes.statistics.connections`.',
|
||||||
|
type: 'boolean',
|
||||||
|
default: false
|
||||||
|
})
|
||||||
.command('get <resource> [path]', 'Get raw JSON', function(yargs) {
|
.command('get <resource> [path]', 'Get raw JSON', function(yargs) {
|
||||||
return yargs.epilog('Perform a raw REST API call. ' +
|
return yargs.epilog('Perform a raw REST API call. ' +
|
||||||
'The path definition uses JavaScript syntax to extract values. ' +
|
'The path definition uses JavaScript syntax to extract values. ' +
|
||||||
@ -27,7 +34,11 @@ exports.builder = function(yargs) {
|
|||||||
maxctrl(argv, function(host) {
|
maxctrl(argv, function(host) {
|
||||||
return doRequest(host, argv.resource, (res) => {
|
return doRequest(host, argv.resource, (res) => {
|
||||||
if (argv.path) {
|
if (argv.path) {
|
||||||
res = _.getPath(res, argv.path, '')
|
res = _.getPath(res, argv.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argv.sum && Array.isArray(res) && typeof(res[0]) == 'number') {
|
||||||
|
res = res.reduce((sum, value) => value ? sum + value : sum)
|
||||||
}
|
}
|
||||||
|
|
||||||
return JSON.stringify(res)
|
return JSON.stringify(res)
|
||||||
|
|||||||
Reference in New Issue
Block a user