From 75cb032f3a4d143ca9fe65e5d4d02ad040c537fb Mon Sep 17 00:00:00 2001 From: Chris Burroughs Date: Thu, 22 Dec 2016 15:13:18 -0500 Subject: [PATCH] joyent/node-triton#156 allow all profile fields to be given on the cli Reviewed by: Trent Mick Approved by: Trent Mick --- CHANGES.md | 4 ++++ lib/cli.js | 21 ++++++++++++++++++--- lib/config.js | 31 +++++++++++++++++++------------ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index fa473a7..6adc554 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/lib/cli.js b/lib/cli.js index 9064a96..a8f3cc5 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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; @@ -578,8 +578,22 @@ CLI.prototype._emitCompletions = function _emitCompletions(type, cb) { * Apply overrides from CLI options to the given profile object *in place*. */ CLI.prototype._applyProfileOverrides = - function _applyProfileOverrides(profile) { + 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; }; diff --git a/lib/config.js b/lib/config.js index 175858a..7ea03dd 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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); } }