From e8d1fb578bef407e6cdbad43b0b0c03e1ea9d1ff Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 10 Mar 2016 11:42:23 -0800 Subject: [PATCH] joyent/node-triton#97 `triton profile set -` to set the *last* profile as current --- CHANGES.md | 2 +- lib/config.js | 38 +++++++++++++++++++++++--------- lib/do_profile/do_create.js | 7 +++--- lib/do_profile/do_set_current.js | 3 +++ lib/do_profile/profilecommon.js | 16 +++++++++++--- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d1b7961..013a71f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ ## 4.7.1 (not yet released) -(nothing yet) +- #97 `triton profile set -` to set the *last* profile as current. ## 4.7.0 diff --git a/lib/config.js b/lib/config.js index 9517f3c..94fbc88 100644 --- a/lib/config.js +++ b/lib/config.js @@ -49,6 +49,8 @@ var OVERRIDE_NAMES = []; // config object keys to do a one-level deep override // TODO: use this const to create the "Configuration" docs table. var CONFIG_VAR_NAMES = [ 'profile', + // Intentionally exclude 'oldProfile' so that it isn't manually set. + // 'oldProfile', 'cacheDir' ]; @@ -139,19 +141,20 @@ function loadConfig(opts) { } -function setConfigVar(opts, cb) { +function setConfigVars(opts, cb) { assert.object(opts, 'opts'); assert.string(opts.configDir, 'opts.configDir'); - assert.string(opts.name, 'opts.name'); - assert.string(opts.value, 'opts.value'); - assert.ok(opts.name.indexOf('.') === -1, - 'dotted config name not yet supported'); - assert.ok(CONFIG_VAR_NAMES.indexOf(opts.name) !== -1, - 'unknown config var name: ' + opts.name); + assert.object(opts.vars, 'opts.vars'); + Object.keys(opts.vars).forEach(function (name) { + assert.ok(name.indexOf('.') === -1, + 'dotted config name not yet supported'); + assert.ok(CONFIG_VAR_NAMES.indexOf(name) !== -1, + 'unknown config var name: ' + name); + }); var configPath = configPathFromDir(opts.configDir); - var config; + vasync.pipeline({funcs: [ function loadExisting(_, next) { fs.exists(configPath, function (exists) { @@ -183,8 +186,23 @@ function setConfigVar(opts, cb) { }); }, + /* + * To support `triton profile set -` to set profile to the *last* + * one used, we special case the setting of the "profile" config var + * to *also* then set "oldProfile" to the old value. (We are copying + * the "OLDPWD" naming used by the shell for `cd -`.) + */ + function specialCaseOldProfile(_, next) { + if (opts.vars.hasOwnProperty('profile') && config.profile) { + opts.vars['oldProfile'] = config.profile; + } + next(); + }, + function updateAndSave(_, next) { - config[opts.name] = opts.value; + Object.keys(opts.vars).forEach(function (name) { + config[name] = opts.vars[name]; + }); fs.writeFile(configPath, JSON.stringify(config, null, 4), next); } ]}, cb); @@ -414,7 +432,7 @@ function saveProfileSync(opts) { module.exports = { loadConfig: loadConfig, - setConfigVar: setConfigVar, + setConfigVars: setConfigVars, validateProfile: validateProfile, loadProfile: loadProfile, diff --git a/lib/do_profile/do_create.js b/lib/do_profile/do_create.js index d0b0a92..39c72fe 100644 --- a/lib/do_profile/do_create.js +++ b/lib/do_profile/do_create.js @@ -265,10 +265,11 @@ function _createProfile(opts, cb) { return; } - mod_config.setConfigVar({ + mod_config.setConfigVars({ configDir: cli.configDir, - name: 'profile', - value: data.name + vars: { + profile: data.name + } }, function (err) { if (err) { next(err); diff --git a/lib/do_profile/do_set_current.js b/lib/do_profile/do_set_current.js index 6030b6b..632e569 100644 --- a/lib/do_profile/do_set_current.js +++ b/lib/do_profile/do_set_current.js @@ -35,6 +35,9 @@ do_set_current.help = [ ' {{name}} set-current NAME', '', '{{options}}', + 'NAME is the name of an existing profile, or "-" to switch to the', + 'previously set profile.', + '', 'The "current" profile is the one used by default, unless overridden by', '`triton -p PROFILE-NAME ...` or the TRITON_PROFILE environment variable.' ].join('\n'); diff --git a/lib/do_profile/profilecommon.js b/lib/do_profile/profilecommon.js index 5edfba0..c9d1d54 100644 --- a/lib/do_profile/profilecommon.js +++ b/lib/do_profile/profilecommon.js @@ -16,6 +16,15 @@ function setCurrentProfile(opts, cb) { assert.func(cb, 'cb'); var cli = opts.cli; + if (opts.name === '-') { + if (cli.tritonapi.config.hasOwnProperty('oldProfile')) { + opts.name = cli.tritonapi.config.oldProfile; + } else { + cb(new errors.ConfigError('"oldProfile" is not set in config')); + return; + } + } + try { var profile = mod_config.loadProfile({ configDir: cli.configDir, @@ -39,10 +48,11 @@ function setCurrentProfile(opts, cb) { return cb(); } - mod_config.setConfigVar({ + mod_config.setConfigVars({ configDir: cli.configDir, - name: 'profile', - value: profile.name + vars: { + profile: profile.name + } }, function (err) { if (err) { return cb(err);