Skip to content

Commit de44f92

Browse files
committed
Support help for each command
1 parent 810f563 commit de44f92

File tree

3 files changed

+133
-52
lines changed

3 files changed

+133
-52
lines changed

cli.js

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@ const provider = require('simplify-sdk/provider');
1212
const readlineSync = require('readline-sync');
1313
const { options } = require('yargs');
1414
const { exec } = require('child_process');
15-
const { authenticate, registerUser, confirmRegistration, getCurrentSession, userSignOut } = require('./cognito')
16-
const PLAN_DEFINITIONS = {
17-
"BASIC": {
18-
"Index": 0,
19-
"Version": "Community",
20-
"Description": "FREE membership with community access to development resources",
21-
"Subscription": 0
22-
},
23-
"PREMIUM": {
24-
"Index": 1,
25-
"Version": "Enterprise",
26-
"Description": "10$ per month with unlimited access to productionr ready resources",
27-
"Subscription": 10
28-
}
15+
const { authenticate, registerUser, confirmRegistration, getCurrentSession, userSignOut } = require('./cognito');
16+
const yargs = require('yargs');
17+
const { PLAN_DEFINITIONS, ALLOWED_COMANDS, AVAILABLE_COMMANDS } = require('./const')
18+
const getOptionDesc = function(cmdOpt, optName) {
19+
const options = (AVAILABLE_COMMANDS.find(cmd => cmd.name == cmdOpt) || { options: [] }).options
20+
return (options.find(opt => opt.name == optName) || { desc : '' }).desc
2921
}
3022
var currentSubscription = "Basic"
3123
var functionMeta = { lashHash256: null }
@@ -634,33 +626,51 @@ const showAvailableStacks = (options, promptDescription) => {
634626

635627
showBoxBanner()
636628

637-
var argv = require('yargs').usage('simplify-cli init | regiter | login | logout | upgrade | create | deploy | destroy | list [options]')
638-
.string('help').describe('help', 'Display Help for a specific command')
639-
.string('name').describe('name', 'Specify a name for the created project')
640-
.string('template').describe('template', 'Init nodejs or python template')
641-
.string('data').describe('data', 'Saved parameters in JSON file').default('data', 'parameters.json')
642-
.string('config').alias('c', 'config').describe('config', 'function configuration').default('config', 'config.json')
643-
.string('policy').alias('p', 'policy').describe('policy', 'function policy to attach').default('policy', 'policy.json')
644-
.string('role').alias('r', 'role').describe('role', 'function policy to attach').default('role', 'role.json')
645-
.string('source').alias('s', 'source').describe('source', 'function source to deploy').default('source', 'src')
646-
.string('env').alias('e', 'env').describe('env', 'environment name')
647-
.string('region').describe('region', 'region name to deploy')
648-
.string('exclude').describe('exclude', 'files or folders to exclude for a zip')
649-
.string('env-file').describe('env-file', 'environment variable file').default('env-file', '.env')
650-
.boolean('update').describe('update', 'force update function code').default('update', false)
651-
.boolean('publish').describe('publish', 'force publish with a version').default('publish', false)
652-
.boolean('layer').describe('layer', 'deploy source folder as layer').default('layer', false)
653-
.string('location').describe('location', 'stack folder to deploy').default('location', '')
654-
.string('stack').describe('stack', 'stack name to deploy')
655-
.string('function').describe('function', 'function name to deploy')
656-
.string('composer').describe('composer', 'multistacks composer to deploy')
657-
.demandOption(['c', 'p', 's']).demandCommand(1).argv;
629+
var argv = require('yargs').usage('simplify-cli command [options]')
630+
.string('help').describe('help', 'display help for a specific command')
631+
.string('name').describe('name', getOptionDesc('create', 'name'))
632+
.string('template').describe('template', getOptionDesc('create', 'template'))
633+
.string('stack').describe('stack', getOptionDesc('deploy', 'stack'))
634+
.string('function').describe('function', getOptionDesc('deploy', 'function'))
635+
.string('location').describe('location', getOptionDesc('deploy', 'location')).default('location', '')
636+
.string('parameters').describe('parameters', getOptionDesc('deploy', 'parameters')).default('parameters', 'parameters.json')
637+
.string('config').describe('config', 'function configuration').default('config', 'config.json')
638+
.string('policy').describe('policy', 'function policy to attach').default('policy', 'policy.json')
639+
.string('role').describe('role', 'function policy to attach').default('role', 'role.json')
640+
.string('source').describe('source', 'function source to deploy').default('source', 'src')
641+
.string('env').describe('env', 'environment name')
642+
.string('region').describe('region', getOptionDesc('deploy', 'region'))
643+
.string('dotenv').describe('dotenv', getOptionDesc('deploy', 'dotenv')).default('dotenv', '.env')
644+
.boolean('update').describe('update', getOptionDesc('deploy', 'update')).default('update', false)
645+
.boolean('publish').describe('publish', getOptionDesc('deploy', 'publish')).default('publish', false)
646+
.boolean('layer').describe('layer', getOptionDesc('deploy', 'layer')).default('layer', false)
647+
.demandCommand(0).argv;
658648

659649
var cmdOPS = (argv._[0] || 'list').toUpperCase()
660650
var optCMD = (argv._.length > 1 ? argv._[1] : undefined)
661651
var cmdArg = argv['stack'] || argv['function'] || optCMD
662652
var cmdType = cmdArg ? fs.existsSync(path.resolve(argv.location, cmdArg, "template.yaml")) ? "CF-Stack" : "Function" : undefined
663-
653+
if (argv._.length == 0) {
654+
yargs.showHelp()
655+
console.log(`\n`, ` * ${CBRIGHT}Supported command list${CRESET}:`, '\n')
656+
AVAILABLE_COMMANDS.map((cmd, idx) => {
657+
console.log(`\t- ${CPROMPT}${cmd.name.toLowerCase()}${CRESET} : ${cmd.desc}`)
658+
})
659+
console.log(`\n`)
660+
process.exit(0)
661+
} else {
662+
if (typeof argv['help'] !== 'undefined') {
663+
console.log(`\n`, ` * ${CBRIGHT}Supported options${CRESET}:`, '\n')
664+
const cmdResult = AVAILABLE_COMMANDS.find(cmd => cmdOPS.toLowerCase() == cmd.name)
665+
if (cmdResult) {
666+
cmdResult.options.map((cmd, idx) => {
667+
console.log(`\t${CPROMPT}--${cmd.name.toLowerCase()}${CRESET} : ${cmd.desc}`)
668+
})
669+
}
670+
console.log(`\n`)
671+
process.exit(0)
672+
}
673+
}
664674
const showSubscriptionPlan = function (userSession) {
665675
currentSubscription = (userSession.getIdToken().payload[`subscription`] || 'Basic')
666676
const currentVersion = PLAN_DEFINITIONS[currentSubscription.toUpperCase()].Version || 'Community'
@@ -679,8 +689,8 @@ const processCLI = function (cmdRun, session) {
679689
configStackName: cmdArg,
680690
configStackFolder: argv.location,
681691
envName: argv.env,
682-
envFile: argv['env-file'],
683-
dataFile: argv.data,
692+
envFile: argv['dotenv'],
693+
dataFile: argv.parameters,
684694
roleFile: argv.role,
685695
policyFile: argv.policy,
686696
sourceDir: argv.source,
@@ -693,7 +703,7 @@ const processCLI = function (cmdRun, session) {
693703
regionName: argv.region,
694704
configFile: argv.config,
695705
envName: argv.env,
696-
envFile: argv['env-file']
706+
envFile: argv['dotenv']
697707
}, `Available ${CPROMPT}stack${CRESET} and ${CPROMPT}function${CRESET} to deploy with command: simplify-cli deploy [--stack or --function] name`)
698708
}
699709

@@ -703,14 +713,14 @@ const processCLI = function (cmdRun, session) {
703713
regionName: argv.region,
704714
configFile: argv.config,
705715
envName: argv.env,
706-
envFile: argv['env-file'],
716+
envFile: argv['dotenv'],
707717
functionName: cmdArg,
708718
withFunctionLayer: argv.layer
709719
}) : destroyStack)({
710720
regionName: argv.region,
711721
configFile: argv.config,
712722
envName: argv.env,
713-
envFile: argv['env-file'],
723+
envFile: argv['dotenv'],
714724
configStackFolder: argv.location,
715725
configStackName: cmdArg
716726
})
@@ -719,7 +729,7 @@ const processCLI = function (cmdRun, session) {
719729
regionName: argv.region,
720730
configFile: argv.config,
721731
envName: argv.env,
722-
envFile: argv['env-file']
732+
envFile: argv['dotenv']
723733
}, `Select a ${CPROMPT}stack${CRESET} or ${CPROMPT}function${CRESET} to destroy with command: simplify-cli destroy [--stack or --function] name`)
724734
}
725735
} else if (cmdRun === "LOGOUT") {
@@ -774,7 +784,7 @@ const processCLI = function (cmdRun, session) {
774784
regionName: argv.region,
775785
configFile: argv.config,
776786
envName: argv.env,
777-
envFile: argv['env-file']
787+
envFile: argv['dotenv']
778788
}, `Deployed ${CPROMPT}stacks${CRESET} and ${CPROMPT}functions${CRESET} managed by Simplify CLI:`)
779789
} else if (cmdRun === "INIT") {
780790
function verifyAccountAccess(options, callback, errorHandler) {
@@ -904,13 +914,10 @@ const processCLI = function (cmdRun, session) {
904914
} else if (cmdRun === "CREATE") {
905915
const templateName = argv.template || optCMD
906916
if (typeof templateName === "undefined") {
907-
if (typeof argv.help !== "undefined") {
908-
showTemplates("basic/functions", `\nCreate a function template: simplify-cli create [--template=]ShowLog | Detector\n`)
909-
showTemplates("basic/stacks", `\nOr create a deployment stack: simplify-cli create [--template=]CloudFront | CognitoUser...\n`)
910-
console.log(`\n *`, `Or fetch from YAML: simplify-cli create [--template=]https://github.com/awslabs/...template.yml \n`)
911-
} else {
912-
console.log(`\n *`, `Missing a Template: simplify-cli create [--template=]Template \n`)
913-
}
917+
console.log(`\nMissing a Template: simplify-cli create [--template=]Template \n`)
918+
showTemplates("basic/functions", `\nCreate a function template: simplify-cli create [--template=]ShowLog | Detector\n`)
919+
showTemplates("basic/stacks", `\nOr create a deployment stack: simplify-cli create [--template=]CloudFront | CognitoUser...\n`)
920+
console.log(`\n *`, `Or fetch from YAML: simplify-cli create [--template=]https://github.com/awslabs/...template.yml \n`)
914921
} else {
915922
createStackOnInit(templateName, argv.location, process.env)
916923
}
@@ -919,7 +926,7 @@ const processCLI = function (cmdRun, session) {
919926
}
920927
}
921928

922-
if (["INIT", "LOGIN", "REGISTER"].indexOf(cmdOPS) == -1) {
929+
if (ALLOWED_COMANDS.indexOf(cmdOPS) == -1) {
923930
if (!fs.existsSync(path.resolve(argv.config || 'config.json'))) {
924931
console.log(`\n`, `- ${CPROMPT}This is not a valid environment${CRESET}. You must create an environment first.`)
925932
console.log(`\n`, `*`, `Create environment: \tsimplify-cli init`, `\n`)
@@ -942,6 +949,8 @@ if (["INIT", "LOGIN", "REGISTER"].indexOf(cmdOPS) == -1) {
942949
if (["INIT"].indexOf(cmdOPS) == -1) {
943950
const configInfo = JSON.parse(fs.readFileSync(path.resolve(argv.config || 'config.json')))
944951
if (configInfo.hasOwnProperty('Profile') && configInfo.hasOwnProperty('Region') && configInfo.hasOwnProperty('Bucket')) {
952+
console.log(`\n`, ` * ${CPROMPT}Opensource Support${CRESET} : Community (FREE)`)
953+
console.log(` * ${CPROMPT}Enterprise Support${CRESET} : simplify-cli register`)
945954
processCLI(cmdOPS)
946955
} else {
947956
console.log(`\n`, `- ${CPROMPT}This is not a valid environment${CRESET}. The ${argv.config || 'config.json'} is incorrect.`)

const.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const CUNDERLINE = '\x1b[4m'
2+
const CRESET = '\x1b[0m'
3+
4+
const PLAN_DEFINITIONS = {
5+
"BASIC": {
6+
"Index": 0,
7+
"Version": "Community",
8+
"Description": "FREE membership with community access to development resources",
9+
"Subscription": 0
10+
},
11+
"PREMIUM": {
12+
"Index": 1,
13+
"Version": "Enterprise",
14+
"Description": "10$ per month with unlimited access to productionr ready resources",
15+
"Subscription": 10
16+
}
17+
}
18+
const AVAILABLE_COMMANDS = [
19+
{ name: "init", desc: "setup project with AWS programmatic access", options: [
20+
{ name: "help", desc: "display help for `init` command" }
21+
] },
22+
{ name: "list", desc: "list all deployemnts as snapshot to your cloud", options: [
23+
{ name: "help", desc: "display help for `list` command" }
24+
] },
25+
{ name: "create", desc: "create a deployment `folder` from templates", options: [
26+
{ name: "help", desc: "display help for `create` command" },
27+
{ name: "name", desc: "another alterative deployment folder name" },
28+
{ name: "template", desc: "a simplify `template` or a https://name/template.yaml" }
29+
] },
30+
{ name: "deploy", desc: "push a deployment `folder` to AWS cloud", options: [
31+
{ name: "help", desc: "display help for `deploy` command" },
32+
{ name: "stack", desc: "explicit as a stack for this deployment" },
33+
{ name: "function", desc: "explicit as a function for this deployment" },
34+
{ name: "parameters", desc: "stack parameters in JSON" },
35+
{ name: "config", desc: "explicit the Configuration for `function` deployment" },
36+
{ name: "policy", desc: "explicit the IAM policy for `function` deployment" },
37+
{ name: "role", desc: "explicit the IAM role for `function` deployment" },
38+
{ name: "src", desc: "specific the `source` folder for `function` deployment" },
39+
{ name: "layer", desc: "deploy the `source` folder as a layer" },
40+
{ name: "publish", desc: "publish new a version for `function`" },
41+
{ name: "update", desc: "force update code for `function`" },
42+
{ name: "region", desc: "selected region to deploy" },
43+
{ name: "dotenv", desc: "another alternative env" },
44+
{ name: "location", desc: "the root folder of this deployment" },
45+
] },
46+
{ name: "destroy", desc: "cleanup your cloud deployment resource", options: [
47+
{ name: "help", desc: "display help for `destroy` command" },
48+
{ name: "stack", desc: "explicit as a stack for this deployment" },
49+
{ name: "function", desc: "explicit as a function for this deployment" }
50+
] },
51+
{ name: "regiter", desc: "create an account to get support", options: [
52+
{ name: "help", desc: "display help for `register` command" }
53+
] },
54+
{ name: "login", desc: "sign in as a registered user", options: [
55+
{ name: "help", desc: "display help for `login` command" }
56+
] },
57+
{ name: "logout", desc: `sign out your current session (${CUNDERLINE}require login${CRESET})`, options: [
58+
{ name: "help", desc: "display help for `logout` command" }
59+
] },
60+
{ name: "upgrade", desc: `upgrade your subscription plan (${CUNDERLINE}require login${CRESET})`, options: [
61+
{ name: "help", desc: "display help for `upgrade` command" }
62+
] },
63+
{ name: "support", desc: `get support from our team (${CUNDERLINE}require login${CRESET})`, options: [
64+
{ name: "help", desc: "display help for `support` command" }
65+
] }
66+
]
67+
const ALLOWED_COMANDS = ["INIT", "LOGIN", "REGISTER", "CREATE", "DEPLOY", "DESTROY", "LIST"]
68+
69+
module.exports = {
70+
ALLOWED_COMANDS,
71+
AVAILABLE_COMMANDS
72+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simplify-cli",
3-
"version": "0.1.25",
3+
"version": "0.1.26",
44
"description": "Simplify CLI - Easy Deployment",
55
"main": "cli.js",
66
"bin": {

0 commit comments

Comments
 (0)