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

129 lines
2.6 KiB
JavaScript
Raw Normal View History

/*
2015-08-26 02:46:14 +03:00
* Copyright 2015 Joyent Inc.
*
2015-08-26 01:15:02 +03:00
* `triton instances ...`
*/
var tabula = require('tabula');
2015-08-26 02:46:14 +03:00
var common = require('./common');
2015-08-26 02:46:14 +03:00
// to be passed as query string args to /my/machines
var validFilters = [
'name',
'image',
'state',
'memory',
'tombstone',
'credentials'
];
// valid output fields to be printed
var validFields = [
'id',
'name',
'type',
'state',
'dataset',
'memory',
'disk',
'ips',
'metadata',
'created',
'updated',
'package',
2015-08-26 03:00:50 +03:00
'image',
'ago'
2015-08-26 02:46:14 +03:00
];
2015-08-26 01:15:02 +03:00
function do_instances(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length > 1) {
2015-08-26 02:46:14 +03:00
callback(new Error('too many args: ' + args));
return;
}
var columns = opts.o.trim().split(',');
var sort = opts.s.trim().split(',');
var listOpts;
try {
listOpts = common.kvToObj(args, validFilters);
} catch (e) {
callback(e);
return;
}
2015-08-26 02:46:14 +03:00
this.triton.cloudapi.listMachines(listOpts, function (err, machines) {
if (err) {
callback(err);
return;
}
2015-08-26 03:00:50 +03:00
// add extra fields for nice output
var now = new Date();
machines.forEach(function (machine) {
var created = new Date(machine.created);
machine.ago = common.longAgo(created, now);
});
if (opts.json) {
2015-08-26 02:46:14 +03:00
console.log(common.jsonStream(machines));
} else {
2015-08-26 02:46:14 +03:00
tabula(machines, {
skipHeader: opts.H,
columns: columns,
sort: sort,
validFields: validFields
});
}
2015-08-26 02:46:14 +03:00
callback();
});
2015-08-26 02:46:14 +03:00
}
2015-08-26 01:15:02 +03:00
do_instances.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
2015-08-26 02:46:14 +03:00
{
names: ['H'],
type: 'bool',
help: 'Omit table header row.'
},
{
names: ['o'],
type: 'string',
2015-08-26 03:00:50 +03:00
default: 'id,name,state,type,image,memory,disk,ago',
2015-08-26 02:46:14 +03:00
help: 'Specify fields (columns) to output.',
helpArg: 'field1,...'
},
{
names: ['s'],
type: 'string',
default: 'name',
help: 'Sort on the given fields. Default is "name".',
helpArg: 'field1,...'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
}
];
2015-08-26 01:15:02 +03:00
do_instances.help = (
'List instances.\n'
+ '\n'
+ 'Usage:\n'
2015-08-26 01:15:02 +03:00
+ ' {{name}} instances [<filters>...]\n'
+ '\n'
+ '{{options}}'
);
2015-08-26 03:00:50 +03:00
do_instances.aliases = ['insts'];
2015-08-26 01:15:02 +03:00
module.exports = do_instances;