tabula changes

- "datacenters" and "services" both use tabula
- comman tabula options moved to common
This commit is contained in:
Dave Eddy 2015-09-02 23:24:08 -04:00
parent 538eb7612a
commit b6e4c06742
4 changed files with 87 additions and 57 deletions

View File

@ -16,6 +16,33 @@ var p = console.log;
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
var TABULA_OPTIONS = [
{
group: 'Output options'
},
{
names: ['H'],
type: 'bool',
help: 'Omit table header row.'
},
{
names: ['o'],
type: 'string',
help: 'Specify fields (columns) to output.',
helpArg: 'field1,...'
},
{
names: ['s'],
type: 'string',
help: 'Sort on the given fields.',
helpArg: 'field1,...'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
}
];
// ---- support stuff // ---- support stuff
@ -299,7 +326,6 @@ function normShortId(s) {
} }
//---- exports //---- exports
module.exports = { module.exports = {
@ -315,6 +341,7 @@ module.exports = {
humanDurationFromMs: humanDurationFromMs, humanDurationFromMs: humanDurationFromMs,
humanSizeFromBytes: humanSizeFromBytes, humanSizeFromBytes: humanSizeFromBytes,
capitalize: capitalize, capitalize: capitalize,
normShortId: normShortId normShortId: normShortId,
TABULA_OPTIONS: TABULA_OPTIONS
}; };
// vim: set softtabstop=4 shiftwidth=4: // vim: set softtabstop=4 shiftwidth=4:

View File

@ -4,6 +4,8 @@
* `triton datacenters ...` * `triton datacenters ...`
*/ */
var tabula = require('tabula');
var common = require('./common'); var common = require('./common');
function do_datacenters(subcmd, opts, args, callback) { function do_datacenters(subcmd, opts, args, callback) {
@ -15,19 +17,37 @@ function do_datacenters(subcmd, opts, args, callback) {
return; return;
} }
var columns = (opts.o || 'name,url').split(',');
var sort = (opts.s || 'name').split(',');
this.triton.cloudapi.listDatacenters(function (err, datacenters) { this.triton.cloudapi.listDatacenters(function (err, datacenters) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
} }
if (opts.json) { /*
console.log(JSON.stringify(datacenters)); * datacenters are returned in the form of:
} else { * {name: 'url', name2: 'url2', ...}
// pretty print * we "normalize" them for use by tabula and JSON stream
* by making them an array
*/
var dcs = [];
Object.keys(datacenters).forEach(function (key) { Object.keys(datacenters).forEach(function (key) {
var val = datacenters[key]; dcs.push({
console.log('%s: %s', key, val); name: key,
url: datacenters[key]
});
});
if (opts.json) {
common.jsonStream(dcs);
} else {
tabula(dcs, {
skipHeader: opts.H,
columns: columns,
sort: sort,
dottedLookup: true
}); });
} }
callback(); callback();
@ -39,13 +59,8 @@ do_datacenters.options = [
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
help: 'Show this help.' help: 'Show this help.'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
} }
]; ].concat(common.TABULA_OPTIONS);
do_datacenters.help = ( do_datacenters.help = (
'Show datacenters information\n' 'Show datacenters information\n'
+ '\n' + '\n'

View File

@ -34,7 +34,7 @@ function do_instances(subcmd, opts, args, callback) {
columns = 'id,name,img,package,state,primaryIp,created'.split(','); columns = 'id,name,img,package,state,primaryIp,created'.split(',');
} }
/* JSSTYLED */ /* JSSTYLED */
var sort = opts.s.trim().split(/\s*,\s*/g); var sort = (opts.s || 'created').trim().split(/\s*,\s*/g);
var listOpts; var listOpts;
try { try {
@ -108,39 +108,9 @@ do_instances.options = [
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
help: 'Show this help.' help: 'Show this help.'
},
{
group: 'Output options'
},
{
names: ['H'],
type: 'bool',
help: 'Omit table header row.'
},
{
names: ['o'],
type: 'string',
help: 'Specify fields (columns) to output.',
helpArg: 'field1,...'
},
{
names: ['long', 'l'],
type: 'bool',
help: 'Long/wider output. Ignored if "-o ..." is used.'
},
{
names: ['s'],
type: 'string',
default: 'created',
help: 'Sort on the given fields. Default is "created".',
helpArg: 'field1,...'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
} }
]; ].concat(common.TABULA_OPTIONS);
do_instances.help = ( do_instances.help = (
'List instances.\n' 'List instances.\n'
+ '\n' + '\n'

View File

@ -4,6 +4,8 @@
* `triton services ...` * `triton services ...`
*/ */
var tabula = require('tabula');
var common = require('./common'); var common = require('./common');
function do_services(subcmd, opts, args, callback) { function do_services(subcmd, opts, args, callback) {
@ -15,21 +17,41 @@ function do_services(subcmd, opts, args, callback) {
return; return;
} }
var columns = (opts.o || 'name,endpoint').split(',');
var sort = (opts.s || 'name').split(',');
this.triton.cloudapi.listServices(function (err, services) { this.triton.cloudapi.listServices(function (err, services) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
} }
if (opts.json) { /*
console.log(JSON.stringify(services)); * services are returned in the form of:
} else { * {name: 'endpoint', name2: 'endpoint2', ...}
// pretty print * we "normalize" them for use by tabula and JSON stream
* by making them an array
*/
var svcs = [];
Object.keys(services).forEach(function (key) { Object.keys(services).forEach(function (key) {
var val = services[key]; svcs.push({
console.log('%s: %s', key, val); name: key,
endpoint: services[key]
});
});
if (opts.json) {
common.jsonStream(svcs);
} else {
tabula(svcs, {
skipHeader: opts.H,
columns: columns,
sort: sort,
dottedLookup: true
}); });
} }
callback(); callback();
}); });
} }
@ -39,13 +61,9 @@ do_services.options = [
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
help: 'Show this help.' help: 'Show this help.'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
} }
]; ].concat(common.TABULA_OPTIONS);
do_services.help = ( do_services.help = (
'Show services information\n' 'Show services information\n'
+ '\n' + '\n'