diff --git a/lib/do_key/do_list.js b/lib/do_key/do_list.js index 2a64faa..e03a197 100644 --- a/lib/do_key/do_list.js +++ b/lib/do_key/do_list.js @@ -11,11 +11,17 @@ */ var assert = require('assert-plus'); +var tabula = require('tabula'); var common = require('../common'); var errors = require('../errors'); +var COLUMNS_DEFAULT = 'fingerprint,name'; +var COLUMNS_LONG = 'fingerprint,name,key'; +var SORT_DEFAULT = 'name'; + + function do_list(subcmd, opts, args, cb) { assert.func(cb, 'cb'); @@ -38,11 +44,28 @@ function do_list(subcmd, opts, args, cb) { } if (opts.json) { - console.log(JSON.stringify(keys)); - } else { + common.jsonStream(keys); + } else if (opts.authorized_keys) { keys.forEach(function (key) { console.log(common.chomp(key.key)); }); + } else { + var columns = COLUMNS_DEFAULT; + + if (opts.o) { + columns = opts.o; + } else if (opts.long) { + columns = COLUMNS_LONG; + } + + columns = columns.split(','); + var sort = opts.s.split(','); + + tabula(keys, { + skipHeader: false, + columns: columns, + sort: sort + }); } cb(); }); @@ -54,13 +77,19 @@ do_list.options = [ names: ['help', 'h'], type: 'bool', help: 'Show this help.' - }, - { - names: ['json', 'j'], - type: 'bool', - help: 'JSON stream output.' } -]; +].concat(common.getCliTableOptions({ + includeLong: true, + sortDefault: SORT_DEFAULT +})).concat([ + { + names: ['authorized-keys', 'A'], + type: 'bool', + help: 'Just output public key data -- i.e. output appropriate for a ' + + '"~/.ssh/authorized_keys" file.' + } +]); + do_list.help = [ 'Show all of an account\'s SSH keys.', '', diff --git a/test/integration/cli-keys.test.js b/test/integration/cli-keys.test.js index 97197db..172dbcb 100644 --- a/test/integration/cli-keys.test.js +++ b/test/integration/cli-keys.test.js @@ -23,17 +23,19 @@ var backoff = require('backoff'); var KEY_PATH = __dirname + '/data/id_rsa.pub'; var KEY_SIG = '66:ca:1c:09:75:99:35:69:be:91:08:25:03:c0:17:c0'; var KEY_EMAIL = 'test@localhost.local'; +var KEY_NAME = 'testkey'; var MAX_CHECK_KEY_TRIES = 10; // --- Tests test('triton key', function (tt) { tt.test(' triton key add', function (t) { - h.triton('key add ' + KEY_PATH, function (err, stdout, stderr) { + var cmd = 'key add -n ' + KEY_NAME + ' ' + KEY_PATH; + h.triton(cmd, function (err, stdout, stderr) { if (h.ifErr(t, err, 'triton key add')) return t.end(); - t.ok(stdout.match('Added key "' + KEY_SIG + '"')); + t.equal(stdout, 'Added key "' + KEY_NAME + '" (' + KEY_SIG + ')\n'); t.end(); }); }); @@ -43,7 +45,7 @@ test('triton key', function (tt) { if (h.ifErr(t, err, 'triton key get')) return t.end(); - t.ok(stdout.match(KEY_EMAIL)); + t.ok(stdout.match(KEY_EMAIL), 'test key email present'); t.end(); }); }); @@ -53,13 +55,16 @@ test('triton key', function (tt) { if (h.ifErr(t, err, 'triton key list')) return t.end(); + var keys = stdout.split('\n'); + t.ok(keys[0].match('FINGERPRINT')); + keys.shift(); + // there should always be at least two keys -- the original // account's key, and the test key these tests added - var keys = stdout.split('\n'); t.ok(keys.length > 2, 'triton key list expected key num'); var testKeys = keys.filter(function (key) { - return key.match(KEY_EMAIL); + return key.match(KEY_NAME); }); // this test is a tad dodgy, since it's plausible that there might @@ -76,7 +81,7 @@ test('triton key', function (tt) { if (h.ifErr(t, err, 'triton key delete')) return t.end(); - t.ok(stdout.match('Deleted key "' + KEY_SIG + '"')); + t.ok(stdout.match('Deleted key "' + KEY_SIG + '"'), 'key deleted'); // verify key is gone, which sometimes takes a while var call = backoff.call(function checkKey(next) {