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

82 lines
1.9 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');
// columns default without -o
var columnsDefault = 'name,url';
// sort default with -s
var sortDefault = 'name';
2015-08-27 02:59:28 +03:00
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.split(',');
var sort = opts.s.split(',');
2015-08-27 02:59:28 +03:00
this.triton.cloudapi.listDatacenters(function (err, datacenters) {
if (err) {
callback(err);
return;
}
if (opts.json) {
console.log(JSON.stringify(datacenters));
2015-08-27 02:59:28 +03:00
} else {
/*
* datacenters are returned in the form of:
* {name: 'url', name2: 'url2', ...}
* we "normalize" them for use by tabula by making them an array
*/
var dcs = [];
Object.keys(datacenters).forEach(function (key) {
dcs.push({
name: key,
url: datacenters[key]
});
});
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.getCliTableOptions({
columnsDefault: columnsDefault,
sortDefault: sortDefault
}));
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;