joyent/node-triton#156 allow all profile fields to be given on the cli

Reviewed by: Trent Mick <trent.mick@joyent.com>
Approved by: Trent Mick <trent.mick@joyent.com>
This commit is contained in:
Chris Burroughs 2016-12-22 15:13:18 -05:00
parent 68e5b68583
commit 75cb032f3a
3 changed files with 41 additions and 15 deletions

View File

@ -7,6 +7,10 @@ Known issues:
## not yet released
- [joyent/node-triton#156] Providing all required profile options as
command line flags (account, url, keyId) no longer produces an
incomplete profile error.
- PUBAPI-1171/PUBAPI-1205/PUBAPI-1351 The handling of legacy `SDC_*`
environment variables has been cleaned up. These environment
variables are used for compatibility with the node-smartdc toolset.

View File

@ -282,7 +282,8 @@ CLI.prototype.init = function (opts, args, callback) {
try {
self._profile = mod_config.loadProfile({
configDir: self.configDir,
name: self.profileName
name: self.profileName,
profileOverrides: self._cliOptsAsProfile()
});
} catch (pErr) {
/*
@ -304,7 +305,6 @@ CLI.prototype.init = function (opts, args, callback) {
}
throw pErr;
}
self._applyProfileOverrides(self._profile);
self.log.trace({profile: self._profile}, 'loaded profile');
}
return self._profile;
@ -579,7 +579,21 @@ CLI.prototype._emitCompletions = function _emitCompletions(type, cb) {
*/
CLI.prototype._applyProfileOverrides =
function _applyProfileOverrides(profile) {
var optProfile = this._cliOptsAsProfile();
for (var attr in optProfile) {
profile[attr] = optProfile[attr];
}
};
/*
* Create a profile dict from any cli override options specified.
* Unless all profile flags are specified on the cli, this profile
* will be incomplete and will need to be combined with another
* configuration source.
*/
CLI.prototype._cliOptsAsProfile = function _cliOptsAsProfile() {
var self = this;
var profile = {};
[
{oname: 'account', pname: 'account'},
{oname: 'user', pname: 'user'},
@ -598,6 +612,7 @@ CLI.prototype._applyProfileOverrides =
profile[field.pname] = self.opts[field.oname];
}
});
return profile;
};

View File

@ -267,7 +267,7 @@ function validateProfile(profile, profilePath) {
* @throws {errors.ConfigError} If the profile defined by the environment is
* invalid.
*/
function _loadEnvProfile() {
function _loadEnvProfile(profileOverrides) {
var envProfile = {
name: 'env'
};
@ -280,14 +280,6 @@ function _loadEnvProfile() {
envProfile.url = process.env.TRITON_URL || process.env.SDC_URL;
envProfile.keyId = process.env.TRITON_KEY_ID || process.env.SDC_KEY_ID;
/*
* If none of the above envvars are defined, then there is no env profile.
*/
if (!envProfile.account && !envProfile.user && !envProfile.url &&
!envProfile.keyId)
{
return null;
}
if (process.env.TRITON_TLS_INSECURE) {
envProfile.insecure = common.boolFromString(
process.env.TRITON_TLS_INSECURE, undefined, 'TRITON_TLS_INSECURE');
@ -300,12 +292,23 @@ function _loadEnvProfile() {
envProfile.insecure = true;
}
for (var attr in profileOverrides) {
envProfile[attr] = profileOverrides[attr];
}
/*
* If none of the above envvars are defined, then there is no env profile.
*/
if (!envProfile.account && !envProfile.user && !envProfile.url &&
!envProfile.keyId)
{
return null;
}
validateProfile(envProfile, 'environment variables');
return envProfile;
}
function _profileFromPath(profilePath, name) {
function _profileFromPath(profilePath, name, profileOverrides) {
if (! fs.existsSync(profilePath)) {
throw new errors.ConfigError('no such profile: ' + name);
}
@ -324,6 +327,9 @@ function _profileFromPath(profilePath, name) {
}
profile.name = name;
for (var attr in profileOverrides) {
profile[attr] = profileOverrides[attr];
}
validateProfile(profile, profilePath);
return profile;
@ -333,9 +339,10 @@ function _profileFromPath(profilePath, name) {
function loadProfile(opts) {
assert.string(opts.name, 'opts.name');
assert.optionalString(opts.configDir, 'opts.configDir');
assert.optionalObject(opts.profileOverrides, 'opts.profileOverrides');
if (opts.name === 'env') {
var envProfile = _loadEnvProfile();
var envProfile = _loadEnvProfile(opts.profileOverrides);
if (!envProfile) {
throw new errors.ConfigError('could not load "env" profile '
+ '(missing TRITON_*, or SDC_*, environment variables)');
@ -348,7 +355,7 @@ function loadProfile(opts) {
var profilePath = path.resolve(
common.tildeSync(opts.configDir), 'profiles.d',
opts.name + '.json');
return _profileFromPath(profilePath, opts.name);
return _profileFromPath(profilePath, opts.name, opts.profileOverrides);
}
}