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)
(nothing yet)
- #97 `triton profile set -` to set the *last* profile as current.
## 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.
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,
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(opts.name) !== -1,
'unknown config var name: ' + opts.name);
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,

View File

@ -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);

View File

@ -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');

View File

@ -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);