2015-08-26 01:30:25 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent Inc.
|
|
|
|
*
|
|
|
|
* `triton packages ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var tabula = require('tabula');
|
|
|
|
|
|
|
|
var common = require('./common');
|
|
|
|
|
|
|
|
function do_packages (subcmd, opts, args, callback) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:13:17 +03:00
|
|
|
var columns = 'shortid,name,default,memory,swap,disk'.split(',');
|
2015-08-26 20:02:01 +03:00
|
|
|
if (opts.o) {
|
|
|
|
/* JSSTYLED */
|
|
|
|
columns = opts.o.trim().split(/\s*,\s*/g);
|
|
|
|
} else if (opts.long) {
|
|
|
|
columns[0] = 'id';
|
|
|
|
}
|
|
|
|
/* JSSTYLED */
|
|
|
|
var sort = opts.s.trim().split(/\s*,\s*/g);
|
2015-08-26 01:30:25 +03:00
|
|
|
|
|
|
|
var validFilters = [
|
|
|
|
'name', 'memory', 'disk', 'swap', 'lwps', 'version', 'vcpus', 'group'
|
|
|
|
];
|
|
|
|
var listOpts;
|
|
|
|
try {
|
|
|
|
listOpts = common.kvToObj(args, validFilters);
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:02:01 +03:00
|
|
|
this.triton.cloudapi.listPackages(listOpts, function (err, pkgs) {
|
2015-08-26 02:46:14 +03:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-26 01:30:25 +03:00
|
|
|
if (opts.json) {
|
2015-08-26 23:53:23 +03:00
|
|
|
common.jsonStream(pkgs);
|
2015-08-26 01:30:25 +03:00
|
|
|
} else {
|
2015-08-26 20:02:01 +03:00
|
|
|
for (var i = 0; i < pkgs.length; i++) {
|
|
|
|
var pkg = pkgs[i];
|
|
|
|
pkg.shortid = pkg.id.split('-', 1)[0];
|
2015-08-31 22:13:17 +03:00
|
|
|
if (opts.human) {
|
|
|
|
pkg.memoryHuman = common.humanSizeFromBytes({
|
|
|
|
precision: 1,
|
|
|
|
narrow: true
|
|
|
|
}, pkg.memory * 1024 * 1024);
|
|
|
|
pkg.swapHuman = common.humanSizeFromBytes({
|
|
|
|
precision: 1,
|
|
|
|
narrow: true
|
|
|
|
}, pkg.swap * 1024 * 1024);
|
|
|
|
pkg.diskHuman = common.humanSizeFromBytes({
|
|
|
|
precision: 1,
|
|
|
|
narrow: true
|
|
|
|
}, pkg.disk * 1024 * 1024);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opts.human) {
|
|
|
|
columns = columns.map(function (c) {
|
|
|
|
switch (c) {
|
|
|
|
case 'memory':
|
|
|
|
return {lookup: 'memoryHuman', name: 'MEMORY'};
|
|
|
|
case 'swap':
|
|
|
|
return {lookup: 'swapHuman', name: 'SWAP'};
|
|
|
|
case 'disk':
|
|
|
|
return {lookup: 'diskHuman', name: 'DISK'};
|
|
|
|
default:
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
});
|
2015-08-26 20:02:01 +03:00
|
|
|
}
|
|
|
|
tabula(pkgs, {
|
2015-08-26 01:30:25 +03:00
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
2015-08-26 06:53:48 +03:00
|
|
|
sort: sort
|
2015-08-26 01:30:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_packages.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
2015-08-26 20:02:01 +03:00
|
|
|
{
|
|
|
|
group: 'Output options'
|
|
|
|
},
|
2015-08-26 01:30:25 +03:00
|
|
|
{
|
|
|
|
names: ['H'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Omit table header row.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['o'],
|
|
|
|
type: 'string',
|
|
|
|
help: 'Specify fields (columns) to output.',
|
|
|
|
helpArg: 'field1,...'
|
|
|
|
},
|
2015-08-26 20:02:01 +03:00
|
|
|
{
|
|
|
|
names: ['long', 'l'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Long/wider output. Ignored if "-o ..." is used.'
|
|
|
|
},
|
2015-08-26 01:30:25 +03:00
|
|
|
{
|
|
|
|
names: ['s'],
|
|
|
|
type: 'string',
|
|
|
|
default: 'name',
|
|
|
|
help: 'Sort on the given fields. Default is "name".',
|
|
|
|
helpArg: 'field1,...'
|
|
|
|
},
|
2015-08-31 22:13:17 +03:00
|
|
|
{
|
|
|
|
names: ['human'],
|
|
|
|
type: 'bool',
|
|
|
|
help: '"Human-readable" output. Show MiB, GiB, etc units for sizes.'
|
|
|
|
},
|
2015-08-26 01:30:25 +03:00
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON output.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
do_packages.help = (
|
|
|
|
'List packgaes.\n'
|
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
|
|
|
+ ' {{name}} packages\n'
|
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
|
|
|
);
|
|
|
|
|
2015-08-26 02:19:19 +03:00
|
|
|
do_packages.aliases = ['pkgs'];
|
2015-08-26 01:30:25 +03:00
|
|
|
|
|
|
|
module.exports = do_packages;
|