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

164 lines
4.4 KiB
JavaScript
Raw Normal View History

/*
2015-09-04 21:12:20 +03:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2015 Joyent, Inc.
*
* `triton packages ...`
*/
var tabula = require('tabula');
var common = require('./common');
// valid filters to pass to cloudapi.listPackages
var validFilters = [
'name',
'memory',
'disk',
'swap',
'lwps',
'version',
'vcpus',
'group'
];
// columns default without -o
var columnsDefault = 'shortid,name,default,memory,swap,disk';
// columns default with -l
var columnsDefaultLong = 'id,name,default,memory,swap,disk';
// sort default with -s
var sortDefault = '_groupPlus,memory';
2015-09-02 20:47:06 +03:00
function do_packages(subcmd, opts, args, callback) {
2015-09-04 20:01:55 +03:00
var i;
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
}
var columns = columnsDefault;
if (opts.o) {
columns = opts.o;
} else if (opts.long) {
columns = columnsDefaultLong;
}
columns = columns.split(',');
var rightAligned = {memory: true, disk: true, swap: true,
vcpus: true, lwps: true};
2015-09-04 20:01:55 +03:00
for (i = 0; i < columns.length; i++) {
if (rightAligned[columns[i]]) {
columns[i] = {lookup: columns[i], align: 'right'};
}
}
var sort = opts.s.split(',');
var listOpts;
try {
listOpts = common.kvToObj(args, validFilters);
} catch (e) {
callback(e);
return;
}
this.tritonapi.cloudapi.listPackages(listOpts, function (err, pkgs) {
2015-08-26 02:46:14 +03:00
if (err) {
callback(err);
return;
}
if (opts.json) {
2015-08-26 23:53:23 +03:00
common.jsonStream(pkgs);
} else {
2015-09-04 20:01:55 +03:00
for (i = 0; i < pkgs.length; i++) {
var pkg = pkgs[i];
pkg.shortid = pkg.id.split('-', 1)[0];
/*
* We take a slightly "smarter" view of "group" for default
* sorting, to accomodate usage in the JPC. More recent
* common usage is for packages to have "foo-*" naming.
* JPC includes package sets of yore *and* recent that don't
* use the "group" field. We secondarily separate those
* on a possible "foo-" prefix.
*/
pkg._groupPlus = (pkg.group || (pkg.name.indexOf('-') === -1
2015-09-01 22:03:52 +03:00
? '' : pkg.name.split('-', 1)[0]));
if (!opts.p) {
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.p) {
columns = columns.map(function (c) {
switch (c.lookup || c) {
case 'memory':
return {lookup: 'memoryHuman', name: 'MEMORY',
align: 'right'};
case 'swap':
return {lookup: 'swapHuman', name: 'SWAP',
align: 'right'};
case 'disk':
return {lookup: 'diskHuman', name: 'DISK',
align: 'right'};
default:
return c;
}
});
}
tabula(pkgs, {
skipHeader: opts.H,
columns: columns,
2015-08-26 06:53:48 +03:00
sort: sort
});
}
callback();
});
}
do_packages.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.getCliTableOptions({
includeLong: true,
sortDefault: sortDefault
})).concat([
{
names: ['p'],
type: 'bool',
help: 'Display numbers in parsable (exact) values.'
}
]);
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'];
module.exports = do_packages;