diff --git a/TODO.txt b/TODO.txt index 12366aa..3632a19 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,12 +1,8 @@ "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? # DONE @@ -32,6 +28,8 @@ triton ssh ... triton info +triton create -n|--dry-run # does the fake delay, shows what img/pkg/etc. + # maybe today diff --git a/lib/do_create_instance.js b/lib/do_create_instance.js index 41dd710..0f693cd 100644 --- a/lib/do_create_instance.js +++ b/lib/do_create_instance.js @@ -110,11 +110,12 @@ function do_create_instance(subcmd, opts, args, callback) { var spinner; if (!opts.quiet && process.stderr.isTTY) { + var BORDER = 10; spinner = bigspinner.createSpinner({ delay: 250, stream: process.stderr, - height: process.stdout.rows - 2, - width: process.stdout.columns - 1, + height: Math.max(2, process.stdout.rows - 2 - BORDER), + width: Math.max(2, process.stdout.columns - 1 - BORDER), hideCursor: true, fontChar: '#' }); diff --git a/lib/do_packages.js b/lib/do_packages.js index 843c811..4113b7e 100644 --- a/lib/do_packages.js +++ b/lib/do_packages.js @@ -17,8 +17,15 @@ function do_packages (subcmd, opts, args, callback) { return; } - var columns = opts.o.trim().split(','); - var sort = opts.s.trim().split(','); + var columns = 'shortid,name,default,memory,disk'.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 = [ 'name', 'memory', 'disk', 'swap', 'lwps', 'version', 'vcpus', 'group' @@ -31,15 +38,19 @@ function do_packages (subcmd, opts, args, callback) { return; } - this.triton.cloudapi.listPackages(listOpts, function (err, packages) { + this.triton.cloudapi.listPackages(listOpts, function (err, pkgs) { if (err) { callback(err); return; } if (opts.json) { - console.log(common.jsonStream(packages)); + console.log(common.jsonStream(pkgs)); } 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, columns: columns, sort: sort @@ -55,6 +66,9 @@ do_packages.options = [ type: 'bool', help: 'Show this help.' }, + { + group: 'Output options' + }, { names: ['H'], type: 'bool', @@ -63,10 +77,14 @@ do_packages.options = [ { names: ['o'], type: 'string', - default: 'id,name,default,memory,disk', 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', diff --git a/lib/triton.js b/lib/triton.js index 34095a9..971f3ae 100644 --- a/lib/triton.js +++ b/lib/triton.js @@ -9,10 +9,10 @@ var assert = require('assert-plus'); var auth = require('smartdc-auth'); var EventEmitter = require('events').EventEmitter; var fs = require('fs'); +var format = require('util').format; var once = require('once'); var path = require('path'); var restifyClients = require('restify-clients'); -var sprintf = require('util').format; var tabula = require('tabula'); var cloudapi = require('./cloudapi2'); @@ -199,6 +199,7 @@ Triton.prototype.getImage = function getImage(name, cb) { if (err) { return cb(err); } + var nameMatches = []; var shortIdMatches = []; for (var i = 0; i < imgs.length; i++) { @@ -210,6 +211,7 @@ Triton.prototype.getImage = function getImage(name, cb) { shortIdMatches.push(img); } } + if (nameMatches.length === 1) { cb(null, nameMatches[0]); } 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 - * with that name, then this errors out. + * Get an active package by ID, exact name, or short ID, in that order. + * + * If there is more than one package with that name, then this errors out. */ Triton.prototype.getPackage = function getPackage(name, cb) { assert.string(name, 'name'); @@ -252,21 +255,33 @@ Triton.prototype.getPackage = function getPackage(name, cb) { if (err) { return cb(err); } + var nameMatches = []; + var shortIdMatches = []; for (var i = 0; i < pkgs.length; i++) { - if (pkgs[i].name === name) { - nameMatches.push(pkgs[i]); + var pkg = 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', - name))); - } else if (nameMatches.length === 1) { + + if (nameMatches.length === 1) { cb(null, nameMatches[0]); - } else { + } else if (nameMatches.length > 1) { cb(new Error(format( 'package name "%s" is ambiguous: matches %d packages', 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))); } }); }