2015-09-08 19:55:48 +03:00
|
|
|
/*
|
2015-09-25 22:19:29 +03:00
|
|
|
* Copyright (c) 2015 Joyent Inc.
|
2015-09-08 19:55:48 +03:00
|
|
|
*
|
|
|
|
* `triton profiles ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var common = require('./common');
|
|
|
|
var errors = require('./errors');
|
|
|
|
var mod_config = require('./config');
|
|
|
|
var tabula = require('tabula');
|
|
|
|
|
|
|
|
|
|
|
|
var sortDefault = 'name';
|
|
|
|
var columnsDefault = 'name,curr,account,url';
|
|
|
|
var columnsDefaultLong = 'name,curr,account,url,insecure,keyId';
|
|
|
|
|
2015-09-25 22:19:29 +03:00
|
|
|
function _listProfiles(cli, opts, args, cb) {
|
2015-09-08 19:55:48 +03:00
|
|
|
var columns = columnsDefault;
|
|
|
|
if (opts.o) {
|
|
|
|
columns = opts.o;
|
|
|
|
} else if (opts.long) {
|
|
|
|
columns = columnsDefaultLong;
|
|
|
|
}
|
|
|
|
columns = columns.split(',');
|
|
|
|
|
|
|
|
var sort = opts.s.split(',');
|
|
|
|
|
|
|
|
// Load all the profiles. "env" is a special one managed by the CLI.
|
|
|
|
var profiles;
|
|
|
|
try {
|
|
|
|
profiles = mod_config.loadAllProfiles({
|
2015-09-28 22:20:21 +03:00
|
|
|
configDir: cli.configDir,
|
2015-09-25 22:19:29 +03:00
|
|
|
log: cli.log
|
2015-09-08 19:55:48 +03:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return cb(e);
|
|
|
|
}
|
2015-09-21 22:34:37 +03:00
|
|
|
|
|
|
|
// Current profile: Set 'curr' field. Apply CLI overrides.
|
|
|
|
for (i = 0; i < profiles.length; i++) {
|
|
|
|
var profile = profiles[i];
|
2015-09-25 22:19:29 +03:00
|
|
|
if (profile.name === cli.tritonapi.profile.name) {
|
|
|
|
cli._applyProfileOverrides(profile);
|
2015-09-21 22:34:37 +03:00
|
|
|
if (opts.json) {
|
|
|
|
profile.curr = true;
|
|
|
|
} else {
|
|
|
|
profile.curr = '*'; // tabular
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (opts.json) {
|
|
|
|
profile.curr = false;
|
|
|
|
} else {
|
|
|
|
profile.curr = ''; // tabular
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 19:55:48 +03:00
|
|
|
|
|
|
|
// Display.
|
|
|
|
var i;
|
|
|
|
if (opts.json) {
|
|
|
|
common.jsonStream(profiles);
|
|
|
|
} else {
|
|
|
|
tabula(profiles, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
|
|
|
sort: sort
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_profiles(subcmd, opts, args, cb) {
|
|
|
|
if (opts.help) {
|
2015-09-25 22:19:29 +03:00
|
|
|
return this.do_help('help', {}, [subcmd], cb);
|
|
|
|
} else if (args.length > 0) {
|
|
|
|
return cb(new errors.UsageError('too many args'));
|
2015-09-08 19:55:48 +03:00
|
|
|
}
|
|
|
|
|
2015-09-28 22:20:21 +03:00
|
|
|
_listProfiles(this, opts, args, cb);
|
2015-09-08 19:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
do_profiles.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
}
|
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
|
|
|
do_profiles.help = [
|
2015-09-25 22:19:29 +03:00
|
|
|
'List `triton` CLI profiles.',
|
2015-09-08 19:55:48 +03:00
|
|
|
'',
|
|
|
|
'A profile is a configured Triton CloudAPI endpoint. I.e. the',
|
|
|
|
'url, account, key, etc. information required to call a CloudAPI.',
|
|
|
|
'You can then switch between profiles with `triton -p PROFILE`',
|
|
|
|
'or the TRITON_PROFILE environment variable.',
|
|
|
|
'',
|
|
|
|
'The "CURR" column indicates which profile is the current one.',
|
|
|
|
'',
|
|
|
|
'Usage:',
|
2015-09-25 22:19:29 +03:00
|
|
|
' {{name}} profiles',
|
2015-09-08 19:55:48 +03:00
|
|
|
'',
|
|
|
|
'{{options}}'
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = do_profiles;
|