devebot-cli
Advanced tools
Comparing version
@@ -31,8 +31,12 @@ 'use strict'; | ||
devebot.on('progress', function(data) { | ||
logger.debug(' The command is processing'); | ||
}); | ||
devebot.on('success', function(data) { | ||
tui.displayResult(data.value || data.result); | ||
tui.displayCliOutput(data); | ||
}); | ||
devebot.on('failure', function(data) { | ||
tui.displayError(data.error); | ||
tui.displayCliOutput(data); | ||
}); | ||
@@ -94,3 +98,10 @@ | ||
program.parse(process.argv); | ||
if (process.argv.length <= 2) { | ||
program.outputHelp(function(helptext) { | ||
process.stdout.write(helptext); | ||
callback(); | ||
}); | ||
} | ||
})(); | ||
}; |
@@ -5,4 +5,8 @@ 'use strict'; | ||
var util = require('util'); | ||
var Table = require('cli-table2'); | ||
var Validator = require('jsonschema').Validator; | ||
var validator = new Validator(); | ||
var clientInfo = require('./appinfo.js'); | ||
var constx = require('./constx.js'); | ||
@@ -39,3 +43,3 @@ function TextUI(params) { | ||
var usage = mpfObj.stop(); | ||
status.push(util.format(' - Time: %s - Memory: %s', usage.time_text, usage.memory_text)); | ||
status.push('\n', util.format('Time: %s - Memory: %s', usage.time_text, usage.memory_text)); | ||
} | ||
@@ -51,2 +55,126 @@ | ||
this.displayCliOutput = function(output) { | ||
var valresult = validator.validate(output, constx.argumentSchema); | ||
if (valresult.errors.length > 0) { | ||
renderInvalid(output); | ||
} else { | ||
var options = {}; | ||
options.isError = (output.state == 'failed'); | ||
var info = output.details || []; | ||
if (!lodash.isArray(info)) info = [info]; | ||
info.forEach(function(infoItem) { | ||
renderBlock(infoItem, options); | ||
}); | ||
} | ||
}; | ||
var renderInvalid = function(result) { | ||
var table = new Table({ | ||
head: ['Invalid output format. Render full result object in JSON format'], | ||
colWidths: [78] | ||
}); | ||
table.push([JSON.stringify(result, null, 2)]); | ||
console.log(''); | ||
console.log(table.toString()); | ||
}; | ||
var renderBlock = function(result, options) { | ||
printTitle(result, options); | ||
switch(result.type) { | ||
case 'record': | ||
case 'object': | ||
renderRecord(result); | ||
break; | ||
case 'table': | ||
case 'grid': | ||
renderTable(result); | ||
break; | ||
case 'json': | ||
renderJson(result); | ||
break; | ||
default: | ||
renderUnknown(result); | ||
break; | ||
} | ||
}; | ||
var renderRecord = function(result) { | ||
var label = result.label; | ||
var data = result.data; | ||
var keys = Object.keys(data); | ||
var rows = []; | ||
keys.forEach(function(key) { | ||
if (label[key]){ | ||
var row = {}; | ||
row[label[key]] = data[key]; | ||
rows.push(row); | ||
} | ||
}); | ||
var table = new Table(); | ||
rows.forEach(function(row) { | ||
table.push(row); | ||
}); | ||
console.log(table.toString()); | ||
}; | ||
var renderTable = function(result) { | ||
var label = result.label; | ||
var fields = Object.keys(label); | ||
var titles = []; | ||
fields.forEach(function(field) { | ||
titles.push(label[field]); | ||
}); | ||
var data = result.data; | ||
var rows = []; | ||
data.forEach(function(object) { | ||
var row = []; | ||
for(var i=0; i<fields.length; i++) { | ||
row.push(object[fields[i]]); | ||
} | ||
rows.push(row); | ||
}); | ||
var table = new Table({ | ||
head: titles | ||
}); | ||
rows.forEach(function(row) { | ||
table.push(row); | ||
}); | ||
console.log(table.toString()); | ||
}; | ||
var renderJson = function(result) { | ||
var table = new Table({ | ||
head: ['JSON'], | ||
colWidths: [78] | ||
}); | ||
table.push([JSON.stringify(result.data, null, 2)]); | ||
console.log(table.toString()); | ||
}; | ||
var renderUnknown = function(result) { | ||
var table = new Table({ | ||
head: ['Unknown result type'], | ||
colWidths: [78] | ||
}); | ||
table.push([JSON.stringify(result.data, null, 2)]); | ||
console.log(table.toString()); | ||
}; | ||
var printTitle = function(result, options) { | ||
if (lodash.isString(result.title)) { | ||
console.log(''); | ||
var sign = (options && options.isError) ? '[x]' : '[v]'; | ||
console.log('%s %s', sign, result.title); | ||
} | ||
}; | ||
this.displayException = function(exception) { | ||
@@ -62,27 +190,4 @@ [ | ||
}; | ||
this.displayResult = function(result) { | ||
console.log(''); | ||
process.stdout.write(util.format('Command result: %s\n', JSON.stringify(result, null, 2))); | ||
}; | ||
this.displayError = function(error) { | ||
console.log(''); | ||
if (lodash.isObject(error)) { | ||
if (error.name == 'restapi_request_error' || error.name == 'restapi_invalid_status') { | ||
[ | ||
"Fatal error: Unable to find devebot service.", | ||
"", | ||
"If you're seeing this message, either the devebot service hasn't been installed,", | ||
"or it is running incorrectly.", | ||
].forEach(function(str) { process.stderr.write(util.format('%s\n', str)); }); | ||
} else { | ||
process.stderr.write(util.format('Command error: %s\n', JSON.stringify(error, null, 2))); | ||
} | ||
} else { | ||
process.stderr.write(util.format('Unknown error: %s\n', error)); | ||
} | ||
}; | ||
} | ||
module.exports = TextUI; |
{ | ||
"name": "devebot-cli", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "command line interface", | ||
@@ -33,4 +33,6 @@ "main": "lib/client.js", | ||
"bluebird": "^3.0.4", | ||
"cli-table2": "^0.1.9", | ||
"commander": "^2.9.0", | ||
"devebot-api": "^0.0.4", | ||
"jsonschema": "^1.0.2", | ||
"lodash": "^3.10.1", | ||
@@ -37,0 +39,0 @@ "userhome": "^1.0.0", |
15274
41.06%11
10%459
61.05%8
33.33%+ Added
+ Added
+ Added
+ Added
+ Added