2015-09-18 09:15:55 -05:00

275 lines
8.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
'use strict';
var JawsError = require('../lib/jaws-error'),
Jaws = require('../lib/index.js'),
program = require('commander'),
utils = require('../lib/utils'),
Promise = require('bluebird'),
minimist = require('minimist'),
execute = utils.execute;
Promise.onPossiblyUnhandledRejection(function(error) {
throw error;
});
var JAWS = new Jaws();
program
.option('-v, --verbose')
.version(JAWS._meta.version);
/**
* New
* - Create a new project|stage|region|action
*/
program
.command('new <type>')
.allowUnknownOption()
.description('Make a new "project", "stage", "region", or "module <module_name> <action>"')
.action(function() {
// Parse Args
var args = minimist(process.argv.slice(3));
var type = args._[0] ? args._[0].toLowerCase() : null;
if (type == 'project') {
// New Project
var CmdNewProject = require('../lib/commands/new_project');
execute(CmdNewProject.run(
args.name,
args.stage ? args.stage.toLowerCase() : null,
args.s3Bucket,
args.region,
args.email,
args.profile,
args.noExeCf
));
} else if (type == 'region' || type == 'stage') {
// New Region/Stage
var CmdNewStageRegion = require('../lib/commands/new_stage_region');
execute(CmdNewStageRegion.run(
JAWS,
type,
args.stage,
args.region,
args.noExeCf
));
} else {
// Unknown Type
console.error('Unsupported type ' + type + '. Must be project|region|stage|module');
process.exit(1);
}
});
/**
* module
* - install awsm
* - create awsm
*/
program
.command('module <cmd> [params]')
.description('aws-module commands\n\nValid <cmd>' +
'\'s:\n\ninstall: install aws-module.' +
'\n\t Ex: jaws module install <url>' +
'\n\tupdate: update aws-module' +
'\n\t Ex: jaws module update <url>' +
'\n\ncreate: create aws-module action. Module will be created if DNE. create <module resource> <action>' +
'\n\t Ex: jaws module create users list'
)
.option('-s, --save', 'Install&Update Only: Save awsm\'s CloudFormation to your project')
.option('-d, --dont-install-dependencies', 'Install&Update Only: DONT install awsm\'s depdencies automatically')
.option('-l, --lambda', 'Create Only: create lambda. Default is create lambda and endpoint.')
.option('-e, --endpoint', 'Create Only: create API Gateway endpoint. Default is create lambda and endpoint.')
.option('-r, --runtime', 'Create Only: lambda runtime. Default nodejs')
.action(function(cmd, params, options) {
var theParams = process.argv.slice(3);
if (!theParams) {
throw new JawsError('Missing params', JawsError.errorCodes.UNKNOWN);
}
if (cmd == 'install' || cmd == 'update') {
if (theParams.length !== 2) {
throw new JawsError('Please specify awsm url', JawsError.errorCodes.UNKNOWN);
}
var url = theParams[1],
theCmd = require('../lib/commands/module_install');
execute(theCmd[cmd](JAWS, url, options.save, options.dontInstallDependencies));
} else if (cmd == 'create') {
if (theParams.length !== 3) {
throw new JawsError('Please specify awsm resource and action name');
}
var theCmd = require('../lib/commands/module_create');
var module = {
name: theParams[1],
action: theParams[2],
runtime: options.runtime || 'nodejs'
};
if (options.lambda) {
module.type = 'lambda';
} else if (options.endpoint) {
module.type = 'endpoint';
} else {
module.type = 'both';
}
execute(theCmd.run(JAWS, module));
} else {
console.error('Unsupported cmd ' + cmd + '. Must be install|update|create');
process.exit(1);
}
});
/**
* Tag
* - Tag a lambda or endpoint for deployment
*/
program
.command('tag [type]')
.description('Tag lambda function or api gateway resource (endpoint) for deployment ' +
'the next time deploy command is run. Type "lambda" is the default.')
.option('-u, --untag', 'un-tag lambda|endpoint')
.option('-a, --tag-all', 'tag all lambda|endpoint functions in project')
.option('-l, --list-all', 'list all tagged lambda|endpoint functions in project')
.option('-n, --untag-all', 'un-tag all lambda|endpoint functions in project')
.action(function(type, options) {
type = type || 'lambda';
type = type.toLowerCase();
if (-1 == ['endpoint', 'lambda'].indexOf(type)) {
console.error('Unsupported type ' + type + '. Must be endpoint|lambda');
process.exit(1);
}
var CmdTag = require('../lib/commands/tag');
if (options.listAll) {
execute(CmdTag.listAll(JAWS, type).then(function(relPaths) {
console.log(relPaths);
}));
} else if (options.tagAll || options.untagAll) {
var untag = (options.untagAll) ? true : false;
execute(CmdTag.tagAll(JAWS, type, untag));
} else {
// If not tagging all, you have to be tagging whats in your CWD (null 1st param)
execute(CmdTag.tag(type, null, options.untag));
}
});
/**
* Deploy
* - Deploy Lambda or Endpoint
*/
program
.command('deploy <type> [stage] [region]')
.description('Deploy a lambda function (type lambda), a REST API (endpoint), or provision AWS resources (resources) for the specified stage.' +
' By default will tag and deploy type at cwd')
.option('-t, --tags', 'Deploy all lambdas tagged as deployable in their jaws.json. Default is to just deploy cwd')
.option('-d, --dont-exe-cf', 'Don\'t execute the lambda cloudformation, just generate it. Zips will be uploaded to s3')
.action(function(type, stage, region, options) {
type = type.toLowerCase();
switch (type) {
case 'endpoint':
var allTagged = (options.tags) ? true : false;
var theCmd = require('../lib/commands/deploy_endpoint');
execute(theCmd.run(JAWS, stage, region, allTagged));
break;
case 'lambda':
var allTagged = (options.tags) ? true : false;
var theCmd = require('../lib/commands/deploy_lambda');
execute(theCmd.run(JAWS, stage, region, allTagged, options.dontExeCf));
break;
default:
console.error('Unsupported type ' + type + '. Must be endpoint|lambda|resources');
process.exit(1);
break;
}
});
/**
* Env
* - Manage ENV variables for a stage
*/
program
.command('env <cmd> <stage> <region> [key] [val]')
.description('Manage env vars for stage & region. Valid cmds: list,get,set,unset. <region> can be "all". <stage> can be "local"')
.usage('get test us-east-1 USERNAME or set test all USERNAME blah.')
.action(function(cmd, stage, region, key, val) {
var CmdEnv = require('../lib/commands/env');
cmd = cmd.toLowerCase();
switch (cmd) {
case 'list':
execute(CmdEnv.listEnv(JAWS, stage, region, true));
break;
case 'get':
if (!key) {
console.error('Must specify key to set');
process.exit(1);
}
execute(CmdEnv.getEnvKey(JAWS, stage, region, key));
break;
case 'set':
if (!key || typeof val == 'undefined') {
console.error('Must specify key and value');
process.exit(1);
}
execute(CmdEnv.setEnvKey(JAWS, stage, region, key, val));
break;
case 'unset':
if (!key) {
console.error('Must specify key to unset');
process.exit(1);
}
if (val) {
console.error('Do not specify val for unset');
process.exit(1);
}
execute(CmdEnv.setEnvKey(JAWS, stage, region, key));
break;
default:
console.error('Unsupported cmd "' + cmd + '". Must be list|get|set|unset');
process.exit(1);
break;
}
});
/**
* Dash
*/
program
.command('dash')
.description('View a project summary and select resources to deploy.')
.action(function() {
var CmdDash = require('../lib/commands/dash');
execute(CmdDash.run(JAWS));
});
if (process.argv.length == 2) {
program.outputHelp();
} else {
program.parse(process.argv);
if (program.verbose) {
process.env.JAWS_VERBOSE = true;
}
}