This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/lib/do_rbac/do_keys.js
Trent Mick fabe0a0841 joyent/node-triton#113 error help for usage errors
This builds on cmdln 4.x's "errHelp" facilities.
2016-06-08 14:13:21 -07:00

102 lines
2.3 KiB
JavaScript

/*
* 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 rbac keys ...`
*/
var tabula = require('tabula');
var common = require('../common');
var errors = require('../errors');
var columnsDefault = 'fingerprint,name';
var columnsDefaultLong = 'fingerprint,name,key';
var sortDefault = 'name';
function do_keys(subcmd, opts, args, cb) {
if (opts.help) {
this.do_help('help', {}, [subcmd], cb);
return;
} else if (args.length === 0) {
cb(new errors.UsageError('no USER argument given'));
return;
} else if (args.length !== 1) {
cb(new errors.UsageError('invalid args: ' + args));
return;
}
var columns = columnsDefault;
if (opts.o) {
columns = opts.o;
} else if (opts.long) {
columns = columnsDefaultLong;
}
columns = columns.split(',');
var sort = opts.s.split(',');
this.top.tritonapi.cloudapi.listUserKeys({userId: args[0]},
function (err, userKeys) {
if (err) {
cb(err);
return;
}
if (opts.json) {
common.jsonStream(userKeys);
} else if (opts.authorized_keys) {
userKeys.forEach(function (key) {
console.log(common.chomp(key.key));
});
} else {
tabula(userKeys, {
skipHeader: false,
columns: columns,
sort: sort
});
}
cb();
});
}
do_keys.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.getCliTableOptions({
includeLong: true,
sortDefault: sortDefault
})).concat([
{
names: ['authorized-keys', 'A'],
type: 'bool',
help: 'Just output public key data -- i.e. output appropriate for a ' +
'"~/.ssh/authorized_keys" file.'
}
]);
do_keys.synopses = ['{{name}} {{cmd}} [OPTIONS] USER'];
do_keys.help = [
/* BEGIN JSSTYLED */
'List RBAC user SSH keys.',
'',
'{{usage}}',
'',
'{{options}}',
'Where "USER" is an RBAC user login or id (a UUID).'
/* END JSSTYLED */
].join('\n');
module.exports = do_keys;