shortid by default for 'triton packages'

This commit is contained in:
Trent Mick 2015-08-26 10:02:01 -07:00
parent 83fbcc129c
commit d5e5b81ac3
4 changed files with 54 additions and 22 deletions

View File

@ -1,12 +1,8 @@
"shortid" instead of full UUID "id" in default output, and then allow lookup "shortid" instead of full UUID "id" in default output, and then allow lookup
by that shortid. Really nice for 80 columns. by that shortid. Really nice for 80 columns.
- images
- packages
- insts - insts
- networks - networks
triton create -n|--dry-run # does the fake delay, shows what img/pkg/etc.
image "name@version" in 'triton insts' table. Optionally? image "name@version" in 'triton insts' table. Optionally?
# DONE # DONE
@ -32,6 +28,8 @@ triton ssh ...
triton info triton info
triton create -n|--dry-run # does the fake delay, shows what img/pkg/etc.
# maybe today # maybe today

View File

@ -110,11 +110,12 @@ function do_create_instance(subcmd, opts, args, callback) {
var spinner; var spinner;
if (!opts.quiet && process.stderr.isTTY) { if (!opts.quiet && process.stderr.isTTY) {
var BORDER = 10;
spinner = bigspinner.createSpinner({ spinner = bigspinner.createSpinner({
delay: 250, delay: 250,
stream: process.stderr, stream: process.stderr,
height: process.stdout.rows - 2, height: Math.max(2, process.stdout.rows - 2 - BORDER),
width: process.stdout.columns - 1, width: Math.max(2, process.stdout.columns - 1 - BORDER),
hideCursor: true, hideCursor: true,
fontChar: '#' fontChar: '#'
}); });

View File

@ -17,8 +17,15 @@ function do_packages (subcmd, opts, args, callback) {
return; return;
} }
var columns = opts.o.trim().split(','); var columns = 'shortid,name,default,memory,disk'.split(',');
var sort = opts.s.trim().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);
var validFilters = [ var validFilters = [
'name', 'memory', 'disk', 'swap', 'lwps', 'version', 'vcpus', 'group' 'name', 'memory', 'disk', 'swap', 'lwps', 'version', 'vcpus', 'group'
@ -31,15 +38,19 @@ function do_packages (subcmd, opts, args, callback) {
return; return;
} }
this.triton.cloudapi.listPackages(listOpts, function (err, packages) { this.triton.cloudapi.listPackages(listOpts, function (err, pkgs) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
} }
if (opts.json) { if (opts.json) {
console.log(common.jsonStream(packages)); console.log(common.jsonStream(pkgs));
} else { } else {
tabula(packages, { for (var i = 0; i < pkgs.length; i++) {
var pkg = pkgs[i];
pkg.shortid = pkg.id.split('-', 1)[0];
}
tabula(pkgs, {
skipHeader: opts.H, skipHeader: opts.H,
columns: columns, columns: columns,
sort: sort sort: sort
@ -55,6 +66,9 @@ do_packages.options = [
type: 'bool', type: 'bool',
help: 'Show this help.' help: 'Show this help.'
}, },
{
group: 'Output options'
},
{ {
names: ['H'], names: ['H'],
type: 'bool', type: 'bool',
@ -63,10 +77,14 @@ do_packages.options = [
{ {
names: ['o'], names: ['o'],
type: 'string', type: 'string',
default: 'id,name,default,memory,disk',
help: 'Specify fields (columns) to output.', help: 'Specify fields (columns) to output.',
helpArg: 'field1,...' helpArg: 'field1,...'
}, },
{
names: ['long', 'l'],
type: 'bool',
help: 'Long/wider output. Ignored if "-o ..." is used.'
},
{ {
names: ['s'], names: ['s'],
type: 'string', type: 'string',

View File

@ -9,10 +9,10 @@ var assert = require('assert-plus');
var auth = require('smartdc-auth'); var auth = require('smartdc-auth');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var fs = require('fs'); var fs = require('fs');
var format = require('util').format;
var once = require('once'); var once = require('once');
var path = require('path'); var path = require('path');
var restifyClients = require('restify-clients'); var restifyClients = require('restify-clients');
var sprintf = require('util').format;
var tabula = require('tabula'); var tabula = require('tabula');
var cloudapi = require('./cloudapi2'); var cloudapi = require('./cloudapi2');
@ -199,6 +199,7 @@ Triton.prototype.getImage = function getImage(name, cb) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
var nameMatches = []; var nameMatches = [];
var shortIdMatches = []; var shortIdMatches = [];
for (var i = 0; i < imgs.length; i++) { for (var i = 0; i < imgs.length; i++) {
@ -210,6 +211,7 @@ Triton.prototype.getImage = function getImage(name, cb) {
shortIdMatches.push(img); shortIdMatches.push(img);
} }
} }
if (nameMatches.length === 1) { if (nameMatches.length === 1) {
cb(null, nameMatches[0]); cb(null, nameMatches[0]);
} else if (nameMatches.length > 1) { } else if (nameMatches.length > 1) {
@ -230,8 +232,9 @@ Triton.prototype.getImage = function getImage(name, cb) {
/** /**
* Get an active package by ID or name. If there is more than one package * Get an active package by ID, exact name, or short ID, in that order.
* with that name, then this errors out. *
* If there is more than one package with that name, then this errors out.
*/ */
Triton.prototype.getPackage = function getPackage(name, cb) { Triton.prototype.getPackage = function getPackage(name, cb) {
assert.string(name, 'name'); assert.string(name, 'name');
@ -252,21 +255,33 @@ Triton.prototype.getPackage = function getPackage(name, cb) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
var nameMatches = []; var nameMatches = [];
var shortIdMatches = [];
for (var i = 0; i < pkgs.length; i++) { for (var i = 0; i < pkgs.length; i++) {
if (pkgs[i].name === name) { var pkg = pkgs[i];
nameMatches.push(pkgs[i]); if (pkg.name === name) {
nameMatches.push(pkg);
}
if (pkg.id.slice(0, 8) === name) {
shortIdMatches.push(pkg);
} }
} }
if (nameMatches.length === 0) {
cb(new Error(format('no package with name=%s was found', if (nameMatches.length === 1) {
name)));
} else if (nameMatches.length === 1) {
cb(null, nameMatches[0]); cb(null, nameMatches[0]);
} else { } else if (nameMatches.length > 1) {
cb(new Error(format( cb(new Error(format(
'package name "%s" is ambiguous: matches %d packages', 'package name "%s" is ambiguous: matches %d packages',
name, nameMatches.length))); name, nameMatches.length)));
} else if (shortIdMatches.length === 1) {
cb(null, shortIdMatches[0]);
} else if (shortIdMatches.length === 0) {
cb(new Error(format(
'no package with name or shortId "%s" was found', name)));
} else {
cb(new Error(format('no package with name "%s" was found '
+ 'and "%s" is an ambiguous shortId', name)));
} }
}); });
} }