2015-08-26 00:25:30 +03:00
|
|
|
/*
|
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.
|
2015-08-26 00:25:30 +03:00
|
|
|
*
|
|
|
|
* `triton images ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var format = require('util').format;
|
|
|
|
var tabula = require('tabula');
|
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
var common = require('./common');
|
2015-08-26 00:25:30 +03:00
|
|
|
var errors = require('./errors');
|
|
|
|
|
2015-09-04 01:09:21 +03:00
|
|
|
// filters to pass triton.listImages
|
|
|
|
var validFilters = [
|
|
|
|
'name',
|
|
|
|
'os',
|
|
|
|
'version',
|
|
|
|
'public',
|
|
|
|
'state',
|
|
|
|
'owner',
|
|
|
|
'type'
|
|
|
|
];
|
|
|
|
|
|
|
|
// columns default without -o
|
|
|
|
var columnsDefault = 'shortid,name,version,state,flags,os,pubdate';
|
|
|
|
|
|
|
|
// columns default with -l
|
|
|
|
var columnsDefaultLong = 'id,name,version,state,flags,os,pubdate';
|
|
|
|
|
|
|
|
// sort default with -s
|
|
|
|
var sortDefault = 'published_at';
|
2015-08-26 00:25:30 +03:00
|
|
|
|
|
|
|
function do_images(subcmd, opts, args, callback) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-04 01:09:21 +03:00
|
|
|
var columns = columnsDefault;
|
2015-08-26 19:15:17 +03:00
|
|
|
if (opts.o) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = opts.o;
|
2015-08-26 19:15:17 +03:00
|
|
|
} else if (opts.long) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columnsDefaultLong;
|
2015-08-26 19:15:17 +03:00
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columns.split(',');
|
|
|
|
|
|
|
|
var sort = opts.s.split(',');
|
2015-08-26 00:25:30 +03:00
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
var listOpts;
|
|
|
|
try {
|
|
|
|
listOpts = common.kvToObj(args, validFilters);
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
2015-08-26 00:25:30 +03:00
|
|
|
}
|
|
|
|
if (opts.all) {
|
|
|
|
listOpts.state = 'all';
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
this.tritonapi.listImages(listOpts, function onRes(err, imgs, res) {
|
2015-08-26 00:25:30 +03:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.json) {
|
|
|
|
// XXX we should have a common method for all these:
|
|
|
|
// XXX sorting
|
|
|
|
// XXX if opts.o is given, then filter to just those fields?
|
2015-08-26 23:53:23 +03:00
|
|
|
common.jsonStream(imgs);
|
2015-08-26 00:25:30 +03:00
|
|
|
} else {
|
|
|
|
// Add some convenience fields
|
|
|
|
// Added fields taken from imgapi-cli.git.
|
|
|
|
for (var i = 0; i < imgs.length; i++) {
|
|
|
|
var img = imgs[i];
|
|
|
|
img.shortid = img.id.split('-', 1)[0];
|
|
|
|
if (img.published_at) {
|
|
|
|
// Just the date.
|
|
|
|
img.pubdate = img.published_at.slice(0, 10);
|
|
|
|
// Normalize on no milliseconds.
|
|
|
|
img.pub = img.published_at.replace(/\.\d+Z$/, 'Z');
|
|
|
|
}
|
|
|
|
if (img.files && img.files[0]) {
|
|
|
|
img.size = img.files[0].size;
|
|
|
|
}
|
|
|
|
var flags = [];
|
|
|
|
if (img.origin) flags.push('I');
|
|
|
|
if (img['public']) flags.push('P');
|
|
|
|
if (img.state !== 'active') flags.push('X');
|
|
|
|
img.flags = flags.length ? flags.join('') : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
tabula(imgs, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
|
|
|
sort: sort
|
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
2015-09-01 19:00:45 +03:00
|
|
|
}
|
2015-08-26 00:25:30 +03:00
|
|
|
|
|
|
|
do_images.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Filtering options'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['all', 'a'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'List all images, not just "active" ones. This ' +
|
|
|
|
'is a shortcut for the "state=all" filter.'
|
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
2015-09-02 10:03:17 +03:00
|
|
|
|
2015-08-26 00:25:30 +03:00
|
|
|
do_images.help = (
|
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
'List images.\n' +
|
|
|
|
'\n' +
|
|
|
|
'Usage:\n' +
|
2015-08-26 01:47:29 +03:00
|
|
|
' {{name}} images [<options>] [<filters>]\n' +
|
2015-08-26 00:25:30 +03:00
|
|
|
'\n' +
|
2015-12-18 01:39:32 +02:00
|
|
|
'{{options}}' +
|
|
|
|
'\n' +
|
2015-08-26 00:25:30 +03:00
|
|
|
'Filters:\n' +
|
|
|
|
' FIELD=VALUE Field equality filter. Supported fields: \n' +
|
|
|
|
' account, owner, state, name, os, and type.\n' +
|
|
|
|
' FIELD=true|false Field boolean filter. Supported fields: public.\n' +
|
|
|
|
' FIELD=~SUBSTRING Field substring filter. Supported fields: name\n' +
|
|
|
|
'\n' +
|
|
|
|
'Fields (most are self explanatory, the client adds some for convenience):\n' +
|
2015-09-02 10:03:17 +03:00
|
|
|
' shortid A short ID prefix.\n' +
|
2015-08-26 00:25:30 +03:00
|
|
|
' flags This is a set of single letter flags\n' +
|
|
|
|
' summarizing some fields. "P" indicates the\n' +
|
|
|
|
' image is public. "I" indicates an incremental\n' +
|
|
|
|
' image (i.e. has an origin). "X" indicates an\n' +
|
|
|
|
' image with a state *other* than "active".\n' +
|
|
|
|
' pubdate Short form of "published_at" with just the date\n' +
|
|
|
|
' pub Short form of "published_at" elliding milliseconds.\n' +
|
2015-12-18 01:39:32 +02:00
|
|
|
' size The number of bytes of the image file (files.0.size)\n'
|
2015-08-26 00:25:30 +03:00
|
|
|
/* END JSSTYLED */
|
|
|
|
);
|
|
|
|
|
2015-08-26 02:19:19 +03:00
|
|
|
do_images.aliases = ['imgs'];
|
|
|
|
|
2015-08-26 00:25:30 +03:00
|
|
|
module.exports = do_images;
|