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_instances.js
Trent Mick dfc3e013b6 'triton insts' default output changes
Change default short and --long output of 'triton insts' somewhat
per feedback from Angela.

I also dropped validFields and turned on dottedLookup so you can do
stuff like:
    triton insts -o shortid,name,tags.foo,metadata.root_authorized_keys
2015-08-31 13:11:34 -07:00

156 lines
3.5 KiB
JavaScript

/*
* Copyright 2015 Joyent Inc.
*
* `triton instances ...`
*/
var f = require('util').format;
var tabula = require('tabula');
var common = require('./common');
// to be passed as query string args to /my/machines
var validFilters = [
'name',
'image',
'state',
'memory',
'tombstone',
'credentials'
];
function do_instances(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
}
var columns = 'shortid,name,img,state,primaryIp,ago'.split(',');
if (opts.o) {
/* JSSTYLED */
columns = opts.o.trim().split(/\s*,\s*/g);
} else if (opts.long) {
columns = 'id,name,img,package,state,primaryIp,created'.split(',');
}
/* JSSTYLED */
var sort = opts.s.trim().split(/\s*,\s*/g);
var listOpts;
try {
listOpts = common.kvToObj(args, validFilters);
} catch (e) {
callback(e);
return;
}
var i = 0;
i++;
var images;
this.triton.listImages({useCache: true}, function (err, _images) {
if (err) {
callback(err);
return;
}
images = _images;
done();
});
i++;
var machines;
this.triton.cloudapi.listMachines(listOpts, function (err, _machines) {
if (err) {
callback(err);
return;
}
machines = _machines;
done();
});
function done() {
if (--i > 0)
return;
// map "uuid" => "image_name"
var imgmap = {};
images.forEach(function (image) {
imgmap[image.id] = f('%s@%s', image.name, image.version);
});
// Add extra fields for nice output.
// XXX FWIW, the "extra fields" for images and packages are not added
// for `opts.json`. Thoughts? We should be consistent there. --TM
var now = new Date();
machines.forEach(function (machine) {
var created = new Date(machine.created);
machine.ago = common.longAgo(created, now);
machine.img = imgmap[machine.image] || machine.image;
machine.shortid = machine.id.split('-', 1)[0];
});
if (opts.json) {
common.jsonStream(machines);
} else {
tabula(machines, {
skipHeader: opts.H,
columns: columns,
sort: sort,
dottedLookup: true
});
}
callback();
}
}
do_instances.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
{
group: 'Output options'
},
{
names: ['H'],
type: 'bool',
help: 'Omit table header row.'
},
{
names: ['o'],
type: 'string',
help: 'Specify fields (columns) to output.',
helpArg: 'field1,...'
},
{
names: ['long', 'l'],
type: 'bool',
help: 'Long/wider output. Ignored if "-o ..." is used.'
},
{
names: ['s'],
type: 'string',
default: 'created',
help: 'Sort on the given fields. Default is "created".',
helpArg: 'field1,...'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
}
];
do_instances.help = (
'List instances.\n'
+ '\n'
+ 'Usage:\n'
+ ' {{name}} instances [<filters>...]\n'
+ '\n'
+ '{{options}}'
);
do_instances.aliases = ['insts'];
module.exports = do_instances;