diff --git a/bin/cmds/device/info.js b/bin/cmds/device/info.js index 2aaa6fd2..b19c7d42 100644 --- a/bin/cmds/device/info.js +++ b/bin/cmds/device/info.js @@ -39,7 +39,8 @@ exports.describe = COMMAND_SHORT_DESCR; exports.builder = function (yargs) { const options = Options.getOptions({ [Options.DEVICE_IDENTIFIER] : true, - [Options.DEBUG] : false + [Options.DEBUG] : false, + [Options.OUTPUT] : "" }); return yargs .usage(Options.getUsage(COMMAND_SECTION, COMMAND, COMMAND_DESCRIPTION, Options.getCommandOptions(options))) diff --git a/bin/cmds/device/list.js b/bin/cmds/device/list.js index f4f66bbc..19ea6acb 100644 --- a/bin/cmds/device/list.js +++ b/bin/cmds/device/list.js @@ -62,7 +62,8 @@ exports.builder = function (yargs) { [Options.ASSIGNED] : false, [Options.ONLINE] : false, [Options.OFFLINE] : false, - [Options.DEBUG] : false + [Options.DEBUG] : false, + [Options.OUTPUT] : "" }); return yargs .usage(Options.getUsage(COMMAND_SECTION, COMMAND, COMMAND_DESCRIPTION, Options.getCommandOptions(options))) diff --git a/bin/cmds/dg/info.js b/bin/cmds/dg/info.js index ddcaff1d..716e4f75 100644 --- a/bin/cmds/dg/info.js +++ b/bin/cmds/dg/info.js @@ -43,7 +43,8 @@ exports.builder = function (yargs) { demandOption : false, describe : 'Displays additional information. Details about every Device assigned to the Device Group, Deployments and other.' }, - [Options.DEBUG] : false + [Options.DEBUG] : false, + [Options.OUTPUT] : "" }); return yargs .usage(Options.getUsage(COMMAND_SECTION, COMMAND, COMMAND_DESCRIPTION, Options.getCommandOptions(options))) diff --git a/bin/cmds/dg/list.js b/bin/cmds/dg/list.js index e2e18920..24696ef0 100644 --- a/bin/cmds/dg/list.js +++ b/bin/cmds/dg/list.js @@ -50,7 +50,8 @@ exports.builder = function (yargs) { demandOption : false, describe : 'Lists Device Groups of the specified type only.', }, - [Options.DEBUG] : false + [Options.DEBUG] : false, + [Options.OUTPUT] : "" }); return yargs .usage(Options.getUsage(COMMAND_SECTION, COMMAND, COMMAND_DESCRIPTION, Options.getCommandOptions(options))) diff --git a/bin/cmds/log/stream.js b/bin/cmds/log/stream.js index 25aebc26..2bbd1638 100644 --- a/bin/cmds/log/stream.js +++ b/bin/cmds/log/stream.js @@ -55,7 +55,8 @@ exports.builder = function (yargs) { ' Project File in the current directory, all Devices assigned to the Device Group referenced by the Project File are assumed.', Options.DEVICE_IDENTIFIER, Options.DEVICE_GROUP_IDENTIFIER, Options.DEVICE_IDENTIFIER, Options.DEVICE_GROUP_IDENTIFIER) }, - [Options.DEBUG] : false + [Options.DEBUG] : false, + [Options.OUTPUT] : "" }); return yargs diff --git a/bin/cmds/loginkey/list.js b/bin/cmds/loginkey/list.js index f4cb23b9..f862d411 100644 --- a/bin/cmds/loginkey/list.js +++ b/bin/cmds/loginkey/list.js @@ -38,7 +38,8 @@ exports.describe = COMMAND_SHORT_DESCR; exports.builder = function (yargs) { const options = Options.getOptions({ - [Options.DEBUG] : false + [Options.DEBUG] : false, + [Options.OUTPUT] : "" }); return yargs .usage(Options.getUsage(COMMAND_SECTION, COMMAND, COMMAND_DESCRIPTION, Options.getCommandOptions(options))) diff --git a/lib/Device.js b/lib/Device.js index 6dd116d0..e8bc429d 100644 --- a/lib/Device.js +++ b/lib/Device.js @@ -178,6 +178,9 @@ class Device extends Entity { filters.push(new ListHelper.ArtificialFilter((entity) => Promise.resolve(!entity.attributes.device_online))); } } + if (options._options.output == "JSON"){ + UserInteractor.PRINT_FORMAT = UserInteractor.PRINT_FORMAT_JSON + } return super._listEntities(filters); } diff --git a/lib/DeviceGroup.js b/lib/DeviceGroup.js index 2316571f..68247203 100644 --- a/lib/DeviceGroup.js +++ b/lib/DeviceGroup.js @@ -330,7 +330,10 @@ class DeviceGroup extends Entity { if (options.deviceGroupType) { filters.push(new ListHelper.DeviceGroupTypeFilter(DeviceGroups.FILTER_TYPE, options.deviceGroupType)); } - return super._listEntities(filters); + if (options._options.output == "JSON"){ + UserInteractor.PRINT_FORMAT = UserInteractor.PRINT_FORMAT_JSON + } + return super._listEntities(filters, options); } // Reboots all of the Devices associated with the Device Group. @@ -607,6 +610,9 @@ class DeviceGroup extends Entity { return this._projectConfig.checkConfig().then(() => this._identifier.initByIdFromConfig(this._projectConfig.deviceGroupId, this._projectConfig.type)); } + else if(options.output){ + UserInteractor.PRINT_FORMAT = UserInteractor.PRINT_FORMAT_JSON + } return Promise.resolve(); } } diff --git a/lib/Log.js b/lib/Log.js index 648a2fe7..303ff41e 100644 --- a/lib/Log.js +++ b/lib/Log.js @@ -43,6 +43,9 @@ class Log { if (options.debug) { this._helper.debug = true; } + if (options != {} && options._options.output == "JSON"){ + UserInteractor.PRINT_FORMAT = UserInteractor.PRINT_FORMAT_JSON + } this._projectConfig = ProjectConfig.getEntity(); this._devices = []; this._deviceIds = []; diff --git a/lib/LoginKey.js b/lib/LoginKey.js index 39da2eb5..55fe3819 100644 --- a/lib/LoginKey.js +++ b/lib/LoginKey.js @@ -28,6 +28,8 @@ const Util = require('util'); const ImpCentralApiHelper = require('./util/ImpCentralApiHelper'); const Identifier = require('./util/Identifier'); const Entity = require('./util/Entity'); +const UserInteractor = require('./util/UserInteractor'); + // This class represents Login Key impCentral API entity. // Provides methods used by impt Login Key Manipulation Commands. @@ -84,6 +86,9 @@ class LoginKey extends Entity { // Returns: Promise that resolves when the Login Key list is successfully // obtained, or rejects with an error _list(options) { + if (options._options.output == "JSON"){ + UserInteractor.PRINT_FORMAT = UserInteractor.PRINT_FORMAT_JSON + } return super._listEntities(); } diff --git a/lib/util/Entity.js b/lib/util/Entity.js index 693a0a42..d45cda08 100644 --- a/lib/util/Entity.js +++ b/lib/util/Entity.js @@ -182,6 +182,9 @@ class Entity { // // Returns: Nothing info(options) { + if (options._options.output == "JSON"){ + UserInteractor.PRINT_FORMAT = UserInteractor.PRINT_FORMAT_JSON + } this.getEntity(true). then(() => this._displayInfo(options)). then(() => UserInteractor.printSuccessStatus()). diff --git a/lib/util/Options.js b/lib/util/Options.js index ae295d0c..9f8d3487 100644 --- a/lib/util/Options.js +++ b/lib/util/Options.js @@ -616,6 +616,15 @@ class Options { default: undefined, _usage: '' }, + [Options.OUTPUT] : { + describe: 'Output in pure JSON string, Contacting Imp Central spinner disabled, and extraneous messages removed.', + alias: 's', + nargs: 1, + type : 'string', + noValue: true, + default: undefined, + _usage: '' + }, [Options.PRODUCT_IDENTIFIER] : { describe: 'Product Identifier: Product Id or Product name.' + ' If not specified, the Product referenced by Project File in the current directory is assumed' + @@ -1246,6 +1255,14 @@ class Options { return this._options[Options.PRE_FACTORY]; } + static get OUTPUT() { + return 'output'; + } + + get output() { + return this._options[Options.OUTPUT]; + } + static get PRODUCT() { return 'product'; } diff --git a/lib/util/UserInteractor.js b/lib/util/UserInteractor.js index 127f9bd5..9910c18f 100644 --- a/lib/util/UserInteractor.js +++ b/lib/util/UserInteractor.js @@ -159,8 +159,32 @@ const ERRORS = { UNEXPECTED_STATE : 'Unexpected state' }; +const PRINT_FORMAT_PRETTY = 'PRINT_FORMAT_PRETTY'; +const PRINT_FORMAT_JSON = 'PRINT_FORMAT_JSON'; + +let _PRINT_FORMAT = PRINT_FORMAT_PRETTY + // Helper class for interactions with a user. class UserInteractor { + static get PRINT_FORMAT() { + return _PRINT_FORMAT + } + + static set PRINT_FORMAT(printFormat) { + if(printFormat != PRINT_FORMAT_PRETTY && printFormat != PRINT_FORMAT_JSON){ + throw(`ERROR in set UserInteractor.PRINT_FORMAT. printFormat=${printFormat} and is not one of [UserInteractor.PRINT_FORMAT_PRETTY, UserInteractor.PRINT_FORMAT_JSON]`) + } + _PRINT_FORMAT = printFormat + } + + static get PRINT_FORMAT_PRETTY() { + return PRINT_FORMAT_PRETTY; + } + + static get PRINT_FORMAT_JSON() { + return PRINT_FORMAT_JSON; + } + static get MESSAGES() { return MESSAGES; } @@ -190,7 +214,9 @@ class UserInteractor { static printMessage(message, ...args) { UserInteractor.spinnerStop(); - console.log(message, ...args); + if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_PRETTY){ + console.log(message, ...args); + } } static printMessageWithStatus(message, ...args) { @@ -240,8 +266,13 @@ class UserInteractor { if (!data || Array.isArray(data) && data.length === 0 || Object.keys(data).length === 0) { return; } - const jsonFormatter = new JsonFormatter(); - console.log(jsonFormatter.render(data)); + if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_PRETTY){ + const jsonFormatter = new JsonFormatter(); + console.log(jsonFormatter.render(data)); + } + else { //if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_JSON){ + console.log(JSON.stringify(data,null,'\t')) + } } static processError(error) { @@ -481,9 +512,11 @@ class UserInteractor { UserInteractor._enableSpinner = value; } - static spinnerStart() { + static spinnerStart() { if (UserInteractor._enableSpinner && !UserInteractor.spinner.isSpinning()) { - UserInteractor.spinner.start(); + if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_PRETTY){ + UserInteractor.spinner.start(); + } } }