shortid by default for 'triton images', works for 'triton image SHORTID', 'triton create ...'

This commit is contained in:
Trent Mick 2015-08-26 09:15:17 -07:00
parent d0ee7f4153
commit 1d0fa26633
3 changed files with 42 additions and 15 deletions

View File

@ -1,8 +1,11 @@
bash completion (subcmd options)
bash completion (cached data: insts, imgs, pkgs, names and ids)
"shortid" instead of full UUID "id" in default output, and then allow lookup
by that shortid. Really nice for 80 columns.
- images
- packages
- insts
- networks
triton create -n|--dry-run # does the fake delay, shows what img/pkg/etc.
image "name@version" in 'triton insts' table. Optionally?
@ -38,6 +41,9 @@ triton info
# maybe today
bash completion (subcmd options)
bash completion (cached data: insts, imgs, pkgs, names and ids)
triton images

View File

@ -17,8 +17,13 @@ function do_images(subcmd, opts, args, callback) {
return;
}
/* JSSTYLED */
var columns = opts.o.trim().split(/\s*,\s*/g);
var columns = 'shortid,name,version,state,flags,os,pubdate'.split(',');
if (opts.o) {
/* JSSTYLED */
columns = opts.o.trim().split(/\s*,\s*/g);
} else if (opts.long) {
columns[0] = 'id';
}
/* JSSTYLED */
var sort = opts.s.trim().split(/\s*,\s*/g);
@ -104,10 +109,14 @@ do_images.options = [
{
names: ['o'],
type: 'string',
default: 'id,name,version,state,flags,os,pubdate',
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',

View File

@ -106,8 +106,10 @@ Triton.prototype._cloudapiFromProfile = function _cloudapiFromProfile(profile) {
/**
* Get an image by ID or name. If there is more than one image with that name,
* then the latest (by published_at) is returned.
* Get an image by ID, exact name, or short ID, in that order.
*
* If there is more than one image with that name, then the latest
* (by published_at) is returned.
*/
Triton.prototype.getImage = function getImage(name, cb) {
assert.string(name, 'name');
@ -129,19 +131,29 @@ Triton.prototype.getImage = function getImage(name, cb) {
return cb(err);
}
var nameMatches = [];
var shortIdMatches = [];
for (var i = 0; i < imgs.length; i++) {
if (imgs[i].name === name) {
nameMatches.push(imgs[i]);
var img = imgs[i];
if (img.name === name) {
nameMatches.push(img);
}
if (img.id.slice(0, 8) === name) {
shortIdMatches.push(img);
}
}
if (nameMatches.length === 0) {
cb(new Error(format('no image with name=%s was found',
name)));
} else if (nameMatches.length === 1) {
if (nameMatches.length === 1) {
cb(null, nameMatches[0]);
} else {
} else if (nameMatches.length > 1) {
tabula.sortArrayOfObjects(nameMatches, 'published_at');
cb(null, nameMatches[nameMatches.length - 1]);
} else if (shortIdMatches.length === 1) {
cb(null, shortIdMatches[0]);
} else if (shortIdMatches.length === 0) {
cb(new Error(format(
'no image with name or shortId "%s" was found', name)));
} else {
cb(new Error(format('no image with name "%s" was found '
+ 'and "%s" is an ambiguous shortId', name)));
}
});
}