This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/lib/do_datacenters.js

74 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-08-27 02:59:28 +03:00
/*
* Copyright 2015 Joyent Inc.
*
* `triton datacenters ...`
*/
var tabula = require('tabula');
2015-08-27 02:59:28 +03:00
var common = require('./common');
function do_datacenters(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length !== 0) {
callback(new Error('invalid args: ' + args));
return;
}
var columns = (opts.o || 'name,url').split(',');
var sort = (opts.s || 'name').split(',');
2015-08-27 02:59:28 +03:00
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]
});
});
2015-08-27 02:59:28 +03:00
if (opts.json) {
common.jsonStream(dcs);
2015-08-27 02:59:28 +03:00
} else {
tabula(dcs, {
skipHeader: opts.H,
columns: columns,
sort: sort,
dottedLookup: true
2015-08-27 02:59:28 +03:00
});
}
callback();
});
}
do_datacenters.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.TABULA_OPTIONS);
2015-08-27 02:59:28 +03:00
do_datacenters.help = (
'Show datacenters information\n'
+ '\n'
+ 'Usage:\n'
+ ' {{name}} datacenters\n'
+ '\n'
+ '{{options}}'
);
module.exports = do_datacenters;