mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
Improve async function configuration
This commit is contained in:
parent
4ad356c2b4
commit
0ad85fe09d
@ -57,51 +57,53 @@ class AwsConfigCredentials {
|
||||
fse.ensureFileSync(this.credentialsFilePath);
|
||||
|
||||
this.hooks = {
|
||||
'config:credentials:config': () => BbPromise.bind(this).then(this.configureCredentials),
|
||||
'config:credentials:config': () => this.configureCredentials(),
|
||||
};
|
||||
}
|
||||
|
||||
configureCredentials() {
|
||||
// sanitize
|
||||
this.options.provider = this.options.provider.toLowerCase();
|
||||
this.options.profile = this.options.profile ? this.options.profile : 'default';
|
||||
return BbPromise.try(() => {
|
||||
// sanitize
|
||||
this.options.provider = this.options.provider.toLowerCase();
|
||||
this.options.profile = this.options.profile ? this.options.profile : 'default';
|
||||
|
||||
// resolve if provider option is not 'aws'
|
||||
if (this.options.provider !== 'aws') {
|
||||
return BbPromise.resolve();
|
||||
}
|
||||
// resolve if provider option is not 'aws'
|
||||
if (this.options.provider !== 'aws') return null;
|
||||
|
||||
// validate
|
||||
if (!this.options.key || !this.options.secret) {
|
||||
throw new this.serverless.classes.Error('Please include --key and --secret options for AWS.');
|
||||
}
|
||||
|
||||
this.serverless.cli.log('Setting up AWS...');
|
||||
|
||||
this.credentials = this.getCredentials();
|
||||
|
||||
// Get the profile start line and end line numbers inside the credentials array
|
||||
const profileBoundaries = this.getProfileBoundaries();
|
||||
|
||||
// Check if the profile exists
|
||||
const isNewProfile = profileBoundaries.start === -1;
|
||||
if (isNewProfile) {
|
||||
this.addProfile();
|
||||
} else {
|
||||
// Only update the profile if the overwrite flag was set
|
||||
if (!this.options.overwrite) {
|
||||
const message = [
|
||||
`Failed! ~/.aws/credentials already has a "${this.options.profile}" profile.`,
|
||||
' Use the overwrite flag ("-o" or "--overwrite") to force the update',
|
||||
].join('');
|
||||
this.serverless.cli.log(message);
|
||||
return BbPromise.resolve();
|
||||
// validate
|
||||
if (!this.options.key || !this.options.secret) {
|
||||
throw new this.serverless.classes.Error(
|
||||
'Please include --key and --secret options for AWS.'
|
||||
);
|
||||
}
|
||||
|
||||
this.updateProfile(profileBoundaries);
|
||||
}
|
||||
this.serverless.cli.log('Setting up AWS...');
|
||||
|
||||
return this.saveCredentialsFile();
|
||||
this.credentials = this.getCredentials();
|
||||
|
||||
// Get the profile start line and end line numbers inside the credentials array
|
||||
const profileBoundaries = this.getProfileBoundaries();
|
||||
|
||||
// Check if the profile exists
|
||||
const isNewProfile = profileBoundaries.start === -1;
|
||||
if (isNewProfile) {
|
||||
this.addProfile();
|
||||
} else {
|
||||
// Only update the profile if the overwrite flag was set
|
||||
if (!this.options.overwrite) {
|
||||
const message = [
|
||||
`Failed! ~/.aws/credentials already has a "${this.options.profile}" profile.`,
|
||||
' Use the overwrite flag ("-o" or "--overwrite") to force the update',
|
||||
].join('');
|
||||
this.serverless.cli.log(message);
|
||||
return null;
|
||||
}
|
||||
|
||||
this.updateProfile(profileBoundaries);
|
||||
}
|
||||
|
||||
return this.saveCredentialsFile();
|
||||
});
|
||||
}
|
||||
|
||||
getCredentials() {
|
||||
|
||||
@ -121,8 +121,13 @@ describe('AwsConfigCredentials', () => {
|
||||
it('should throw an error if the "key" and "secret" options are not given', () => {
|
||||
awsConfigCredentials.options.key = false;
|
||||
awsConfigCredentials.options.secret = false;
|
||||
|
||||
expect(() => awsConfigCredentials.configureCredentials()).to.throw(Error);
|
||||
return awsConfigCredentials.configureCredentials().then(
|
||||
() => {
|
||||
throw new Error('Unexpected');
|
||||
},
|
||||
error =>
|
||||
expect(error.message).to.include('Please include --key and --secret options for AWS')
|
||||
);
|
||||
});
|
||||
|
||||
it('should not update the profile if the overwrite flag is not set', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user