Skip to content

Commit

Permalink
add init command for easy onboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
austencollins authored and pmuens committed Dec 1, 2016
1 parent 662edbd commit af35ee3
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/plugins/Plugins.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"plugins": [
"./init/index.js",
"./create/create.js",
"./install/install.js",
"./package/index.js",
Expand Down
119 changes: 119 additions & 0 deletions lib/plugins/init/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict';

const BbPromise = require('bluebird');
const path = require('path');
const os = require('os');

class Init {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;

this.commands = {
init: {
usage: 'Initializes a provider with the Serverless Framework',
lifecycleEvents: [
'init',
],
options: {
provider: {
usage: 'Name of the provider',
required: true,
shortcut: 'p',
},
key: {
usage: 'Access key for the provider',
shortcut: 'k',
},
secret: {
usage: 'Secret key for the provider',
shortcut: 's',
}
},
},
};

this.hooks = {
'init:init': () => BbPromise.bind(this)
.then(this.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.`);
}

// 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.`);
}

// Inform
this.serverless.cli.log('Initializing AWS...');
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 ||
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.`);
}

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

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

// Check if credentials files contains anything
let 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.');
}

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

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

return BbPromise.resolve();
}
}

module.exports = Init;
19 changes: 19 additions & 0 deletions lib/plugins/init/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const expect = require('chai').expect;
const Init = require('../index.js');
const Serverless = require('../../../Serverless');

describe('Init', () => {
let info;
let serverless;

beforeEach(() => {
serverless = new Serverless();
init = new Init(serverless);
});

describe('#constructor()', () => {
it('should have commands', () => expect(info.commands).to.be.not.empty);
});
});

0 comments on commit af35ee3

Please sign in to comment.