joyent/node-triton#97 triton profile set - to set the *last* profile as current

This commit is contained in:
Trent Mick 2016-03-10 11:42:23 -08:00
parent 1dc156d87c
commit e8d1fb578b
5 changed files with 49 additions and 17 deletions

View File

@ -2,7 +2,7 @@
## 4.7.1 (not yet released) ## 4.7.1 (not yet released)
(nothing yet) - #97 `triton profile set -` to set the *last* profile as current.
## 4.7.0 ## 4.7.0

View File

@ -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. // TODO: use this const to create the "Configuration" docs table.
var CONFIG_VAR_NAMES = [ var CONFIG_VAR_NAMES = [
'profile', 'profile',
// Intentionally exclude 'oldProfile' so that it isn't manually set.
// 'oldProfile',
'cacheDir' 'cacheDir'
]; ];
@ -139,19 +141,20 @@ function loadConfig(opts) {
} }
function setConfigVar(opts, cb) { function setConfigVars(opts, cb) {
assert.object(opts, 'opts'); assert.object(opts, 'opts');
assert.string(opts.configDir, 'opts.configDir'); assert.string(opts.configDir, 'opts.configDir');
assert.string(opts.name, 'opts.name'); assert.object(opts.vars, 'opts.vars');
assert.string(opts.value, 'opts.value'); Object.keys(opts.vars).forEach(function (name) {
assert.ok(opts.name.indexOf('.') === -1, assert.ok(name.indexOf('.') === -1,
'dotted config name not yet supported'); 'dotted config name not yet supported');
assert.ok(CONFIG_VAR_NAMES.indexOf(opts.name) !== -1, assert.ok(CONFIG_VAR_NAMES.indexOf(name) !== -1,
'unknown config var name: ' + opts.name); 'unknown config var name: ' + name);
});
var configPath = configPathFromDir(opts.configDir); var configPath = configPathFromDir(opts.configDir);
var config; var config;
vasync.pipeline({funcs: [ vasync.pipeline({funcs: [
function loadExisting(_, next) { function loadExisting(_, next) {
fs.exists(configPath, function (exists) { 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) { 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); fs.writeFile(configPath, JSON.stringify(config, null, 4), next);
} }
]}, cb); ]}, cb);
@ -414,7 +432,7 @@ function saveProfileSync(opts) {
module.exports = { module.exports = {
loadConfig: loadConfig, loadConfig: loadConfig,
setConfigVar: setConfigVar, setConfigVars: setConfigVars,
validateProfile: validateProfile, validateProfile: validateProfile,
loadProfile: loadProfile, loadProfile: loadProfile,

View File

@ -265,10 +265,11 @@ function _createProfile(opts, cb) {
return; return;
} }
mod_config.setConfigVar({ mod_config.setConfigVars({
configDir: cli.configDir, configDir: cli.configDir,
name: 'profile', vars: {
value: data.name profile: data.name
}
}, function (err) { }, function (err) {
if (err) { if (err) {
next(err); next(err);

View File

@ -35,6 +35,9 @@ do_set_current.help = [
' {{name}} set-current NAME', ' {{name}} set-current NAME',
'', '',
'{{options}}', '{{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', 'The "current" profile is the one used by default, unless overridden by',
'`triton -p PROFILE-NAME ...` or the TRITON_PROFILE environment variable.' '`triton -p PROFILE-NAME ...` or the TRITON_PROFILE environment variable.'
].join('\n'); ].join('\n');

View File

@ -16,6 +16,15 @@ function setCurrentProfile(opts, cb) {
assert.func(cb, 'cb'); assert.func(cb, 'cb');
var cli = opts.cli; 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 { try {
var profile = mod_config.loadProfile({ var profile = mod_config.loadProfile({
configDir: cli.configDir, configDir: cli.configDir,
@ -39,10 +48,11 @@ function setCurrentProfile(opts, cb) {
return cb(); return cb();
} }
mod_config.setConfigVar({ mod_config.setConfigVars({
configDir: cli.configDir, configDir: cli.configDir,
name: 'profile', vars: {
value: profile.name profile: profile.name
}
}, function (err) { }, function (err) {
if (err) { if (err) {
return cb(err); return cb(err);