2017-02-22 01:42:07 +02: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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-05-29 19:36:30 +03:00
|
|
|
* Copyright 2017 Joyent, Inc.
|
2017-02-22 01:42:07 +02:00
|
|
|
*
|
2017-03-21 03:55:59 +02:00
|
|
|
* `triton volume list ...`
|
2017-02-22 01:42:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var format = require('util').format;
|
2017-03-23 03:19:02 +02:00
|
|
|
var jsprim = require('jsprim');
|
2017-02-22 01:42:07 +02:00
|
|
|
var tabula = require('tabula');
|
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var errors = require('../errors');
|
|
|
|
|
|
|
|
var validFilters = [
|
|
|
|
'name',
|
|
|
|
'size',
|
|
|
|
'state',
|
|
|
|
'owner',
|
|
|
|
'type'
|
|
|
|
];
|
|
|
|
|
|
|
|
// columns default without -o
|
2017-03-21 03:55:59 +02:00
|
|
|
var columnsDefault = 'shortid,name,size,type,state,age';
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
// columns default with -l
|
2017-03-21 03:55:59 +02:00
|
|
|
var columnsDefaultLong = 'id,name,size,type,state,created';
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
// sort default with -s
|
2017-03-21 03:55:59 +02:00
|
|
|
var sortDefault = 'created';
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
function do_list(subcmd, opts, args, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
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 sort = opts.s.split(',');
|
|
|
|
|
|
|
|
var filterPredicate;
|
|
|
|
var listOpts;
|
|
|
|
|
|
|
|
if (args) {
|
|
|
|
try {
|
2017-03-21 03:55:59 +02:00
|
|
|
filterPredicate = common.jsonPredFromKv(args, validFilters,
|
2017-02-22 01:42:07 +02:00
|
|
|
'and');
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 03:19:02 +02:00
|
|
|
if (jsprim.deepEqual(filterPredicate, {})) {
|
2017-05-29 19:36:30 +03:00
|
|
|
filterPredicate = { ne: ['state', 'failed'] };
|
2017-02-22 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (filterPredicate) {
|
|
|
|
listOpts = {
|
|
|
|
predicate: JSON.stringify(filterPredicate)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
common.cliSetupTritonApi({cli: this.top}, function onSetup(setupErr) {
|
|
|
|
if (setupErr) {
|
|
|
|
callback(setupErr);
|
|
|
|
}
|
|
|
|
self.top.tritonapi.cloudapi.listVolumes(listOpts,
|
|
|
|
function onRes(listVolsErr, volumes, res) {
|
2017-03-21 03:55:59 +02:00
|
|
|
var now;
|
|
|
|
|
2017-02-22 01:42:07 +02:00
|
|
|
if (listVolsErr) {
|
|
|
|
return callback(listVolsErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.json) {
|
|
|
|
common.jsonStream(volumes);
|
|
|
|
} else {
|
2017-03-21 03:55:59 +02:00
|
|
|
now = new Date();
|
2017-02-22 01:42:07 +02:00
|
|
|
for (var i = 0; i < volumes.length; i++) {
|
|
|
|
var volume = volumes[i];
|
|
|
|
volume.shortid = volume.id.split('-', 1)[0];
|
2017-03-21 03:55:59 +02:00
|
|
|
volume.created = new Date(volume.create_timestamp);
|
|
|
|
volume.age = common.longAgo(volume.created, now);
|
2017-02-22 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tabula(volumes, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
|
|
|
sort: sort
|
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_list.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
}
|
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
do_list.synopses = ['{{name}} {{cmd}} [OPTIONS] [FILTERS]'];
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
do_list.help = [
|
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
'List volumes.',
|
|
|
|
,
|
|
|
|
'{{usage}}',
|
|
|
|
'',
|
|
|
|
'{{options}}'
|
|
|
|
/* END JSSTYLED */
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
do_list.aliases = ['ls'];
|
|
|
|
|
|
|
|
module.exports = do_list;
|