more user friendly region selects

This commit is contained in:
Eslam A. Hefnawy 2015-12-10 18:34:04 +02:00
parent 3b24758535
commit 4786d189db
7 changed files with 56 additions and 113 deletions

View File

@ -258,16 +258,39 @@ class ServerlessPlugin {
}
cliPromptSelectRegion(message, region, addAllRegions) {
cliPromptSelectRegion(message, addAllRegions, region, stage) {
let _this = this;
// Resolve region if provided
if (region) return BbPromise.resolve(region);
// Skip if not interactive
if (!_this.S._interactive) return BbPromise.resolve();
// Skip if not interactive or stage is local
if (!_this.S._interactive || stage === 'local') return BbPromise.resolve();
let choices = awsMisc.validLambdaRegions.map(r => {
let regionChoices = awsMisc.validLambdaRegions;
// if stage is provided, limit region list
if (stage){
// Make sure stage exists in project
if (!_this.S._projectJson.stages[stage]) {
return BbPromise.reject(new SError('Stage ' + stage + ' does not exist in your project', SError.errorCodes.UNKNOWN));
}
if (addAllRegions) {
// list only regions in stage
regionChoices = [];
_this.S._projectJson.stages[stage].forEach(function(regionInStage){
regionChoices.push(regionInStage.region)
});
} else {
// list only regions NOT in stage
_this.S._projectJson.stages[stage].forEach(function(regionInStage){
let index = regionChoices.indexOf(regionInStage.region);
regionChoices.splice(index, 1);
});
}
}
let choices = regionChoices.map(r => {
return {
key: '',
value: r,
@ -275,6 +298,8 @@ class ServerlessPlugin {
};
});
if (addAllRegions) {
choices.push(
{

View File

@ -130,7 +130,7 @@ usage: serverless env get`,
})
})
.then(function(){
return _this.cliPromptSelectRegion('Select a region to get env var from: ', _this.evt.region, true)
return _this.cliPromptSelectRegion('Select a region to get env var from: ', true, _this.evt.region, _this.evt.stage)
.then(region => {
_this.evt.region = region;
BbPromise.resolve();
@ -146,16 +146,10 @@ usage: serverless env get`,
_validateAndPrepare(){
let _this = this;
console.log(_this.evt)
// non interactive validation
if (!_this.S._interactive) {
// Check API Keys
if (!_this.S._awsProfile) {
if (!_this.S._awsAdminKeyId || !_this.S._awsAdminSecretKey) {
return BbPromise.reject(new SError('Missing AWS Profile and/or API Key and/or AWS Secret Key'));
}
}
// Check Params
if (!_this.evt.stage || !_this.evt.region || !_this.evt.key) {
return BbPromise.reject(new SError('Missing stage and/or region and/or key'));

View File

@ -96,8 +96,7 @@ Usage: serverless env list`,
return _this.S.validateProject()
.bind(_this)
.then(_this._promptStage)
.then(_this._promptRegion)
.then(_this._prompt)
.then(_this._validateAndPrepare)
.then(function(evt) {
console.log("------- EnvList Event: ", evt);
@ -106,79 +105,27 @@ Usage: serverless env list`,
}
/**
* Prompt stage if it's missing
* Prompt stage and region
*/
_promptStage() {
let _this = this;
let stages = Object.keys(_this.S._projectJson.stages);
// Skip if non-interactive
if (!_this.S._interactive || _this.evt.stage) return BbPromise.resolve();
// if project has 1 stage, skip prompt
if (stages.length === 1) {
_this.evt.stage = stages[0];
return BbPromise.resolve();
}
// add local stage
stages.push('local');
// Create Choices
let choices = [];
for (let i = 0; i < stages.length; i++) {
choices.push({
key: (i + 1) + ') ',
value: stages[i],
label: stages[i],
});
}
return SCli.select('Which stage are you listing env vars for: ', choices, false)
.then(function(results) {
_this.evt.stage = results[0].value;
});
}
/**
* Prompt region if it's missing
*/
_promptRegion() {
_prompt() {
let _this = this;
// skip region prompt if selected stage is 'local'
if (_this.evt.stage === 'local') {
_this.evt.region = 'local';
return BbPromise.resolve();
}
if (!_this.S._interactive || _this.evt.region) return BbPromise.resolve();
// TODO: list only regions defined in the provided Stage
// this assumres that the provided stage is valid, we'll have to validate before getting here
let choices = awsMisc.validLambdaRegions.map(r => {
return {
key: '',
value: r,
label: r,
};
});
// adding all regions
choices.push(
{
key: '',
value: 'all',
label: 'all',
}
);
return _this.cliPromptSelect('Select a region for your stage: ', choices, false)
.then(results => {
_this.evt.region = results[0].value;
return _this.cliPromptSelectStage('Select a stage to list env vars from: ', _this.evt.stage, true)
.then(stage => {
_this.evt.stage = stage;
BbPromise.resolve();
})
.then(function(){
return _this.cliPromptSelectRegion('Select a region to list env vars from: ', true, _this.evt.region, _this.evt.stage)
.then(region => {
_this.evt.region = region;
BbPromise.resolve();
});
});
}
/**
* Validate all data from event, interactive CLI or non interactive CLI
* and prepare data
@ -190,12 +137,6 @@ Usage: serverless env list`,
// non interactive validation
if (!_this.S._interactive) {
// Check API Keys
if (!_this.S._awsProfile) {
if (!_this.S._awsAdminKeyId || !_this.S._awsAdminSecretKey) {
return BbPromise.reject(new SError('Missing AWS Profile and/or API Key and/or AWS Secret Key'));
}
}
// Check Params
if (!_this.evt.stage || !_this.evt.region) {
return BbPromise.reject(new SError('Missing stage or region'));

View File

@ -139,11 +139,11 @@ usage: serverless env set`,
return _this.cliPromptSelectStage('Select a stage to set your env var in: ', _this.evt.stage, true)
.then(stage => {
_this.evt.stage = stage;
BbPromise.resolve();
BbPromise.resolve();
})
})
.then(function(){
return _this.cliPromptSelectRegion('Select a region to set env var in: ', _this.evt.region, true)
return _this.cliPromptSelectRegion('Select a region to set env var in: ', true, _this.evt.region, _this.evt.stage)
.then(region => {
_this.evt.region = region;
BbPromise.resolve();
@ -207,7 +207,7 @@ usage: serverless env set`,
});
if (_this.evt.stage == 'local') {
putEnvQ.push(SUtils.writeFile(path.join(this.S._projectRootPath, '.env'), contents));
putEnvQ.push(SUtils.writeFile(path.join(this.S._projectRootPath, 'back', '.env'), contents));
} else {
let awsConfig = {

View File

@ -125,11 +125,11 @@ usage: serverless env unset`,
return _this.cliPromptSelectStage('Select a stage to unset env var from: ', _this.evt.stage, true)
.then(stage => {
_this.evt.stage = stage;
BbPromise.resolve();
BbPromise.resolve();
})
})
.then(function(){
return _this.cliPromptSelectRegion('Select a region to unset env var from: ', _this.evt.region, true)
return _this.cliPromptSelectRegion('Select a region to unset env var from: ', true, _this.evt.region, _this.evt.stage)
.then(region => {
_this.evt.region = region;
BbPromise.resolve();
@ -149,12 +149,6 @@ usage: serverless env unset`,
// non interactive validation
if (!_this.S._interactive) {
// Check API Keys
if (!_this.S._awsProfile) {
if (!_this.S._awsAdminKeyId || !_this.S._awsAdminSecretKey) {
return BbPromise.reject(new SError('Missing AWS Profile and/or API Key and/or AWS Secret Key'));
}
}
// Check Params
if (!_this.evt.stage || !_this.evt.region || !_this.evt.key) {
return BbPromise.reject(new SError('Missing stage and/or region and/or key'));
@ -201,7 +195,7 @@ usage: serverless env unset`,
});
if (_this.evt.stage == 'local') {
putEnvQ.push(utils.writeFile(path.join(_this.S._projectRootPath, '.env'), contents));
putEnvQ.push(utils.writeFile(path.join(_this.S._projectRootPath, 'back', '.env'), contents));
} else {
let awsConfig = {

View File

@ -122,11 +122,12 @@ usage: serverless region create`,
return _this.cliPromptSelectStage('Select an existing stage for your new region: ', _this.evt.stage, false)
.then(stage => {
_this.evt.stage = stage;
BbPromise.resolve();
})
.then(function(){
return _this.cliPromptSelectRegion('Select a new region for your existing stage: ', _this.evt.region, false)
return _this.cliPromptSelectRegion('Select a new region for your existing stage: ', false, _this.evt.region, _this.evt.stage)
.then(region => {
_this.evt.region = region;
BbPromise.resolve();
@ -146,12 +147,6 @@ usage: serverless region create`,
// non interactive validation
if (!_this.S._interactive) {
// Check API Keys
if (!_this.S._awsProfile) {
if (!_this.S._awsAdminKeyId || !_this.S._awsAdminSecretKey) {
return BbPromise.reject(new SError('Missing AWS Profile and/or API Key and/or AWS Secret Key'));
}
}
// Check Params
if (!_this.evt.stage || !_this.evt.region) {
return BbPromise.reject(new SError('Missing stage or region'));

View File

@ -145,7 +145,7 @@ usage: serverless stage create`,
BbPromise.resolve();
})
.then(function(){
return _this.cliPromptSelectRegion('Select a region for your new stage: ', _this.evt.region, false)
return _this.cliPromptSelectRegion('Select a region for your new stage: ', false, _this.evt.region, false)
.then(region => {
_this.evt.region = region;
BbPromise.resolve();
@ -163,12 +163,6 @@ usage: serverless stage create`,
// non interactive validation
if (!this.S._interactive) {
// Check API Keys
if (!this.S._awsProfile) {
if (!this.S._awsAdminKeyId || !this.S._awsAdminSecretKey) {
return BbPromise.reject(new SError('Missing AWS Profile and/or API Key and/or AWS Secret Key'));
}
}
// Check Params
if (!this.evt.stage || !this.evt.region) {
return BbPromise.reject(new SError('Missing stage or region'));