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_profile/do_list.js
Trent Mick 264f69dc54 joyent/node-triton#250 triton profile list doesn't seem to work without full env
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Pedro Palazón Candel <pedro@joyent.com>
Approved by: Marsell Kukuljevic <marsell@joyent.com>
Approved by: Pedro Palazón Candel <pedro@joyent.com>
2018-06-26 16:57:15 -07:00

140 lines
3.8 KiB
JavaScript

/*
* Copyright 2016 Joyent Inc.
*
* `triton profile list ...`
*/
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,
log: cli.log,
profileOverrides: cli._cliOptsAsProfile()
});
} catch (e) {
return cb(e);
}
// Current profile: Set 'curr' field. Apply CLI overrides.
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;
for (i = 0; i < profiles.length; i++) {
var profile = profiles[i];
if (currProfile && profile.name === currProfile.name) {
haveCurr = true;
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
});
if (!haveCurr) {
if (profiles.length === 0) {
process.stderr.write('\nWarning: There is no current profile. '
+ 'Use "triton profile create" to create one,\n'
+ 'or set the required "SDC_*/TRITON_*" environment '
+ 'variables: see "triton --help".\n');
} else {
process.stderr.write('\nWarning: There is no current profile. '
+ 'Use "triton profile set-current ..."\n'
+ 'to set one or "triton profile create" to create one.\n');
}
}
}
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
}));
do_list.synopses = ['{{name}} {{cmd}} [OPTIONS]'];
do_list.help = [
/* BEGIN JSSTYLED */
'List Triton CLI profiles.',
'',
'{{usage}}',
'',
'{{options}}',
'A profile is a configured Triton CloudAPI endpoint and associated info.',
'I.e. the URL, account name, SSH key fingerprint, etc. information required',
'to call a CloudAPI endpoint in a Triton datacenter. You can then switch',
'between profiles with `triton -p PROFILE`, the TRITON_PROFILE environment',
'variable, or by setting your current profile.',
'',
'The "CURR" column indicates which profile is the current one.'
/* END JSSTYLED */
].join('\n');
do_list.aliases = ['ls'];
module.exports = do_list;