Skip to content

Commit

Permalink
fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
austencollins authored and pmuens committed Dec 1, 2016
1 parent 3814837 commit 42ee9eb
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions lib/plugins/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Init {
secret: {
usage: 'Secret key for the provider',
shortcut: 's',
}
},
},
},
};
Expand All @@ -40,77 +40,79 @@ class Init {
}

init() {

// Sanitize
this.options.provider = this.options.provider.toLowerCase();

// Validate
if (['aws'].indexOf(this.options.provider) < 0) {
throw new this.serverless.classes.Error(`Only 'aws' is supported at this time.`);
throw new this.serverless.classes.Error('Only \'aws\' is supported at this time.');
}

// Init AWS
return this.initAws();
};
}

/**
* Init AWS
* - Saves AWS API Keys to a profile on the file system
*/

initAws() {

// Validate
if (!this.options.key || !this.options.secret) {
throw new this.serverless.classes.Error(`Please include --key and --secret options for AWS.`);
throw new this.serverless.classes.Error('Please include --key and --secret options for AWS.');
}

// Inform
this.serverless.cli.log('Initializing AWS...');
this.serverless.cli.log(`Storing an AWS profile on your local file system in '~/.aws/credentials'...`);
this.serverless.cli.log('Storing an AWS profile on your local file system in' +
' \'~/.aws/credentials\'...');

// Locate home directory on user's machine
let env = process.env;
let home = env.HOME ||
const env = process.env;
const home = env.HOME ||
env.USERPROFILE ||
(env.HOMEPATH ? ((env.HOMEDRIVE || 'C:/') + env.HOMEPATH) : null);

if (!home) {
throw new this.serverless.classes.Error(`Can't find home directory on your local file system.`);
throw new this.serverless.classes.Error('Can\'t find home directory ' +
'on your local file system.');
}

// Check if ~/.aws/credentials exists
let configDir = path.join(home, '.aws'),
credsPath = path.join(configDir, 'credentials');
const configDir = path.join(home, '.aws');
const credsPath = path.join(configDir, 'credentials');

if (this.serverless.utils.fileExistsSync(credsPath)) {

// Check if credentials files contains anything
let credsFile = this.serverless.utils.readFileSync(credsPath);
const credsFile = this.serverless.utils.readFileSync(credsPath);

// If credentials file is malformed, throw error and give guidance
if ((credsFile.length && credsFile.indexOf('aws_access_key_id=') < 0) ||
(credsFile.length && credsFile.indexOf('aws_secret_access_key=') < 0)) {
// TODO: Add guidance in error message when the serverless error parser stops poorly reformatting everything.
throw new this.serverless.classes.Error('~/.aws/credentials exists, but it\'s formatted incorrectly. Open it and make sure it\'s correctly formatted.');
// TODO: Add guidance in error message when the serverless error parser gets neater
throw new this.serverless.classes.Error('~/.aws/credentials exists, but it\'s ' +
'formatted incorrectly. Open it and make sure it\'s correctly formatted.');
}

// If credentials file exists and is fine, say so, then exit
if (credsFile.length) {
this.serverless.cli.log('Exiting... ~/.aws/credentials exists and is formatted correctly. Make sure you\'re using the correct profile in your serverless.yml');
this.serverless.cli.log('Exiting... ~/.aws/credentials exists and is formatted ' +
'correctly. Make sure you\'re using the correct profile in your serverless.yml');
return BbPromise.resolve();
}
}

// Write credentials file with 'default' profile
this.serverless.utils.writeFileSync(
credsPath,
'[default]' + os.EOL +
'aws_access_key_id=' + this.options.key + os.EOL +
'aws_secret_access_key=' + this.options.secret + os.EOL);
`[default]
aws_access_key_id=${this.options.key}
aws_secret_access_key=${this.options.secret}` + os.EOL);

// Inform
this.serverless.cli.log('Success! ~/.aws/credentials was created and your access keys were stored under the \'default\' profile.');
this.serverless.cli.log('Success! ~/.aws/credentials was created and your access keys ' +
'were stored under the \'default\' profile.');

return BbPromise.resolve();
}
Expand Down

0 comments on commit 42ee9eb

Please sign in to comment.