diff --git a/lib/common.js b/lib/common.js index 5645e26..5a89619 100644 --- a/lib/common.js +++ b/lib/common.js @@ -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: diff --git a/lib/do_datacenters.js b/lib/do_datacenters.js index d84699f..aa57d47 100644 --- a/lib/do_datacenters.js +++ b/lib/do_datacenters.js @@ -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; } + /* + * 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) { + dcs.push({ + name: key, + url: datacenters[key] + }); + }); + if (opts.json) { - console.log(JSON.stringify(datacenters)); + common.jsonStream(dcs); } else { - // pretty print - Object.keys(datacenters).forEach(function (key) { - var val = datacenters[key]; - console.log('%s: %s', key, val); + 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' diff --git a/lib/do_instances.js b/lib/do_instances.js index d7ae0e0..44523d9 100644 --- a/lib/do_instances.js +++ b/lib/do_instances.js @@ -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' diff --git a/lib/do_services.js b/lib/do_services.js index cf220bd..a413012 100644 --- a/lib/do_services.js +++ b/lib/do_services.js @@ -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; } + /* + * 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) { + svcs.push({ + name: key, + endpoint: services[key] + }); + }); + if (opts.json) { - console.log(JSON.stringify(services)); + common.jsonStream(svcs); } else { - // pretty print - Object.keys(services).forEach(function (key) { - var val = services[key]; - console.log('%s: %s', key, val); + 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'