2015-12-31 01:25:12 +02:00
|
|
|
/*
|
2016-02-03 08:03:01 +02:00
|
|
|
* Copyright 2016 Joyent Inc.
|
2015-12-31 01:25:12 +02:00
|
|
|
*
|
2016-02-03 08:03:01 +02:00
|
|
|
* `triton profile list ...`
|
2015-12-31 01:25:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var tabula = require('tabula');
|
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var errors = require('../errors');
|
|
|
|
var mod_config = require('../config');
|
|
|
|
|
|
|
|
|
|
|
|
var sortDefault = 'name';
|
|
|
|
var columnsDefault = 'name,curr,account,user,url';
|
|
|
|
var columnsDefaultLong = 'name,curr,account,user,url,insecure,keyId';
|
|
|
|
|
|
|
|
function _listProfiles(cli, opts, args, cb) {
|
|
|
|
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({
|
|
|
|
configDir: cli.configDir,
|
2018-06-27 02:57:15 +03:00
|
|
|
log: cli.log,
|
|
|
|
profileOverrides: cli._cliOptsAsProfile()
|
2015-12-31 01:25:12 +02:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return cb(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Current profile: Set 'curr' field. Apply CLI overrides.
|
2016-02-03 08:03:01 +02:00
|
|
|
var currProfile;
|
|
|
|
try {
|
|
|
|
currProfile = cli.tritonapi.profile;
|
|
|
|
} catch (err) {
|
|
|
|
// Ignore inability to load a profile.
|
|
|
|
if (!(err instanceof errors.ConfigError)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var haveCurr = false;
|
2015-12-31 01:25:12 +02:00
|
|
|
for (i = 0; i < profiles.length; i++) {
|
|
|
|
var profile = profiles[i];
|
2016-02-03 08:03:01 +02:00
|
|
|
if (currProfile && profile.name === currProfile.name) {
|
|
|
|
haveCurr = true;
|
2015-12-31 01:25:12 +02:00
|
|
|
cli._applyProfileOverrides(profile);
|
|
|
|
if (opts.json) {
|
|
|
|
profile.curr = true;
|
|
|
|
} else {
|
|
|
|
profile.curr = '*'; // tabular
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (opts.json) {
|
|
|
|
profile.curr = false;
|
|
|
|
} else {
|
|
|
|
profile.curr = ''; // tabular
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display.
|
|
|
|
var i;
|
|
|
|
if (opts.json) {
|
|
|
|
common.jsonStream(profiles);
|
|
|
|
} else {
|
|
|
|
tabula(profiles, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
|
|
|
sort: sort
|
|
|
|
});
|
2016-02-03 08:03:01 +02:00
|
|
|
if (!haveCurr) {
|
2016-03-19 01:51:22 +02:00
|
|
|
if (profiles.length === 0) {
|
|
|
|
process.stderr.write('\nWarning: There is no current profile. '
|
2016-12-21 21:24:24 +02:00
|
|
|
+ 'Use "triton profile create" to create one,\n'
|
2018-02-10 20:17:02 +02:00
|
|
|
+ 'or set the required "SC_*" environment '
|
|
|
|
+ 'variables: see "spearhead --help".\n');
|
2016-03-19 01:51:22 +02:00
|
|
|
} else {
|
|
|
|
process.stderr.write('\nWarning: There is no current profile. '
|
2018-02-10 20:17:02 +02:00
|
|
|
+ 'Use "spearhead profile set-current ..."\n'
|
|
|
|
+ 'to set one or "spearhead profile create" to create one.\n');
|
2016-03-19 01:51:22 +02:00
|
|
|
}
|
2016-02-03 08:03:01 +02:00
|
|
|
}
|
2015-12-31 01:25:12 +02:00
|
|
|
}
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function do_list(subcmd, opts, args, cb) {
|
|
|
|
if (opts.help) {
|
|
|
|
return this.do_help('help', {}, [subcmd], cb);
|
|
|
|
} else if (args.length > 0) {
|
|
|
|
return cb(new errors.UsageError('too many args'));
|
|
|
|
}
|
|
|
|
|
|
|
|
_listProfiles(this.top, opts, args, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_list.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
}
|
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
2016-06-09 00:13:16 +03:00
|
|
|
do_list.synopses = ['{{name}} {{cmd}} [OPTIONS]'];
|
2015-12-31 01:25:12 +02:00
|
|
|
do_list.help = [
|
|
|
|
/* BEGIN JSSTYLED */
|
2018-02-10 20:17:02 +02:00
|
|
|
'List Spearhead CLI profiles.',
|
2015-12-31 01:25:12 +02:00
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{usage}}',
|
|
|
|
'',
|
|
|
|
'{{options}}',
|
2018-02-10 20:17:02 +02:00
|
|
|
'A profile is a configured Spearhead Datacenter endpoint and associated info.',
|
2015-12-31 01:25:12 +02:00
|
|
|
'I.e. the URL, account name, SSH key fingerprint, etc. information required',
|
2018-02-10 20:17:02 +02:00
|
|
|
'to call a CloudAPI endpoint in a Spearhead datacenter. You can then switch',
|
|
|
|
'between profiles with `spearhead -p PROFILE`, the SC_PROFILE environment',
|
2015-12-31 01:25:12 +02:00
|
|
|
'variable, or by setting your current profile.',
|
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'The "CURR" column indicates which profile is the current one.'
|
2015-12-31 01:25:12 +02:00
|
|
|
/* END JSSTYLED */
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
do_list.aliases = ['ls'];
|
|
|
|
|
|
|
|
module.exports = do_list;
|