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 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
@ -299,7 +326,6 @@ function normShortId(s) {
}
//---- exports
module.exports = {
@ -315,6 +341,7 @@ module.exports = {
humanDurationFromMs: humanDurationFromMs,
humanSizeFromBytes: humanSizeFromBytes,
capitalize: capitalize,
normShortId: normShortId
normShortId: normShortId,
TABULA_OPTIONS: TABULA_OPTIONS
};
// vim: set softtabstop=4 shiftwidth=4:

View File

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

View File

@ -34,7 +34,7 @@ function do_instances(subcmd, opts, args, callback) {
columns = 'id,name,img,package,state,primaryIp,created'.split(',');
}
/* JSSTYLED */
var sort = opts.s.trim().split(/\s*,\s*/g);
var sort = (opts.s || 'created').trim().split(/\s*,\s*/g);
var listOpts;
try {
@ -108,39 +108,9 @@ do_instances.options = [
names: ['help', 'h'],
type: 'bool',
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 = (
'List instances.\n'
+ '\n'

View File

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