2015-12-31 04:43:46 +02: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.
|
|
|
|
*
|
|
|
|
* `triton key list ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
2016-01-05 14:13:47 +02:00
|
|
|
var tabula = require('tabula');
|
2015-12-31 04:43:46 +02:00
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var errors = require('../errors');
|
|
|
|
|
|
|
|
|
2016-01-05 14:13:47 +02:00
|
|
|
var COLUMNS_DEFAULT = 'fingerprint,name';
|
|
|
|
var COLUMNS_LONG = 'fingerprint,name,key';
|
|
|
|
var SORT_DEFAULT = 'name';
|
|
|
|
|
|
|
|
|
2015-12-31 04:43:46 +02:00
|
|
|
function do_list(subcmd, opts, args, cb) {
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length > 0) {
|
|
|
|
cb(new errors.UsageError('incorrect number of arguments'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cli = this.top;
|
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
common.cliSetupTritonApi({cli: this.top}, function onSetup(setupErr) {
|
|
|
|
if (setupErr) {
|
|
|
|
cb(setupErr);
|
2017-06-28 23:44:15 +03:00
|
|
|
return;
|
2015-12-31 04:43:46 +02:00
|
|
|
}
|
2016-12-13 20:04:41 +02:00
|
|
|
cli.tritonapi.cloudapi.listKeys({}, function onKeys(err, keys) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
2016-01-05 14:13:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
if (opts.json) {
|
|
|
|
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();
|
|
|
|
});
|
2015-12-31 04:43:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
do_list.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
2016-01-05 14:13:47 +02:00
|
|
|
}
|
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: SORT_DEFAULT
|
|
|
|
})).concat([
|
2015-12-31 04:43:46 +02:00
|
|
|
{
|
2016-01-05 14:13:47 +02:00
|
|
|
names: ['authorized-keys', 'A'],
|
2015-12-31 04:43:46 +02:00
|
|
|
type: 'bool',
|
2016-01-10 02:55:12 +02:00
|
|
|
help: 'Just output public key data, one per line -- i.e. output ' +
|
|
|
|
'appropriate for a "~/.ssh/authorized_keys" file.'
|
2015-12-31 04:43:46 +02:00
|
|
|
}
|
2016-01-05 14:13:47 +02:00
|
|
|
]);
|
|
|
|
|
2016-06-09 00:13:16 +03:00
|
|
|
do_list.synopses = ['{{name}} {{cmd}} [OPTIONS]'];
|
2015-12-31 04:43:46 +02:00
|
|
|
do_list.help = [
|
|
|
|
'Show all of an account\'s SSH keys.',
|
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{usage}}',
|
2015-12-31 04:43:46 +02:00
|
|
|
'',
|
2016-01-10 02:55:12 +02:00
|
|
|
'{{options}}'
|
2015-12-31 04:43:46 +02:00
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
do_list.aliases = ['ls'];
|
|
|
|
|
|
|
|
module.exports = do_list;
|