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_networks.js

96 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-08-26 23:40:50 +03:00
/*
* Copyright 2015 Joyent Inc.
*
* `triton networks ...`
*/
var tabula = require('tabula');
var common = require('./common');
// to be passed as query string args to /my/networks
var validFilters = [
'id',
'name',
'public',
'description'
];
// columns default without -o
var columnsDefault = 'shortid,name,subnet,gateway,fabric,vlan,public';
// columns default with -l
var columnsDefaultLong = 'id,name,subnet,gateway,fabric,vlan,public';
// sort default with -s
var sortDefault = 'name';
2015-08-26 23:40:50 +03:00
function do_networks(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
2015-08-27 00:09:50 +03:00
} else if (args.length !== 0) {
callback(new Error('invalid args: ' + args));
return;
2015-08-26 23:40:50 +03:00
}
var columns = columnsDefault;
if (opts.o) {
columns = opts.o;
} else if (opts.long) {
columns = columnsDefaultLong;
2015-08-26 23:40:50 +03:00
}
columns = columns.split(',');
var sort = opts.s.split(',');
2015-08-26 23:40:50 +03:00
this.triton.cloudapi.listNetworks(function (err, networks) {
if (err) {
callback(err);
return;
}
if (opts.json) {
2015-08-26 23:53:23 +03:00
common.jsonStream(networks);
2015-08-26 23:40:50 +03:00
} else {
for (var i = 0; i < networks.length; i++) {
var net = networks[i];
net.shortid = net.id.split('-', 1)[0];
net.vlan = net.vlan_id;
}
2015-08-26 23:40:50 +03:00
tabula(networks, {
skipHeader: opts.H,
columns: columns,
sort: sort
2015-08-26 23:40:50 +03:00
});
}
callback();
});
}
do_networks.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.getCliTableOptions({
includeLong: true,
sortDefault: sortDefault
}));
do_networks.help = [
'List available networks.',
'',
'Usage:',
' {{name}} networks',
'',
'Fields (most are self explanatory, the client adds some for convenience):',
' vlan A shorter alias for "vlan_id".',
' shortid A short ID prefix.',
'',
'{{options}}'
].join('\n');
2015-08-26 23:40:50 +03:00
module.exports = do_networks;