Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for JSON string output from device and dg list and info commands #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/cmds/device/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
3 changes: 2 additions & 1 deletion bin/cmds/device/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
3 changes: 2 additions & 1 deletion bin/cmds/dg/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
3 changes: 2 additions & 1 deletion bin/cmds/dg/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
3 changes: 2 additions & 1 deletion bin/cmds/log/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion bin/cmds/loginkey/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
3 changes: 3 additions & 0 deletions lib/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/DeviceGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
5 changes: 5 additions & 0 deletions lib/LoginKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions lib/util/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()).
Expand Down
17 changes: 17 additions & 0 deletions lib/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
Expand Down Expand Up @@ -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';
}
Expand Down
43 changes: 38 additions & 5 deletions lib/util/UserInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
}

Expand Down