This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·127 lines (109 loc) · 3.86 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
'use strict'
const os = require('os')
const _ = require('lodash')
const meow = require('meow')
const chalk = require('chalk')
const simpleCfn = require('./')
const fs = require('fs')
const Promise = require('bluebird')
Promise.longStackTraces()
// Allow SDK to search for configs ~/.aws/credentials
process.env.AWS_SDK_LOAD_CONFIG = 1
const cli = meow(`
Usage
simple-cfn deploy {stack name} {template} [--capability=CAPABILITY] [--{param key}={param value}...]
simple-cfn deploy {stack name} {template} [--capability=CAPABILITY] [--file=/path/to/file] [--stack-role-name role-name ]
simple-cfn check {template}
simple-cfn outputs {stack name}
simple-cfn output {stack name} {field name}
Examples
simple-cfn deploy stack-name template.js
simple-cfn deploy stack-name template.yml --ImageId=ami-828283 --VpcId=vpc-828283 --capability=CAPABILITY_NAMED_IAM --capability=CAPABILITY_AUTO_EXPAND
simple-cfn deploy stack-name template.yml --file=/home/parameters.yml
simple-cfn check /home/parameters.yml
simple-cfn outputs stack-name
simple-cfn output stack-name field-name
`)
const cmds = {
deploy: {
args: 3,
exec: () => {
const name = cli.input[1]
const template = cli.input[2]
let cfParams = _.omit(cli.flags, ['capability', 'file', 'stackRoleName'])
if (cfParams && Object.keys(cfParams).length > 0) {
console.log(`${chalk.cyan('Cloud Formation Parameters')}${os.EOL}==========================`)
console.log(_.toPairs(cfParams).map(a => `${a[0]}: ${a[1]}`).join(os.EOL))
console.log(`==========================${os.EOL}`)
}
if (cli.flags.file) {
const filePath = cli.flags.file
console.log(`${chalk.cyan('Parsing Parameters from file')}${os.EOL}==========================`)
console.log(filePath)
console.log(`==========================${os.EOL}`)
if (fs.existsSync(filePath)) {
const parsedParams = fs.readFileSync(filePath, 'utf8')
cfParams = parsedParams.toString()
} else {
return Promise.reject(new Error(chalk.red(`${filePath} does not exist. Please double check the path specified via --file.`)))
}
}
let stackRoleName
if (cli.flags.stackRoleName) {
stackRoleName = cli.flags.stackRoleName
console.log(`${chalk.cyan('Stack using role')}${os.EOL}==========================`)
console.log(stackRoleName)
console.log(`==========================${os.EOL}`)
}
let capabilities
if (cli.flags.capability) {
capabilities = Array.isArray(cli.flags.capability) ? cli.flags.capability : [cli.flags.capability]
console.log(`${chalk.cyan('Cloud Formation Capabilities')}${os.EOL}==========================`)
console.log(capabilities.join(os.EOL))
console.log(`==========================${os.EOL}`)
}
return simpleCfn({ name, template, cfParams, capabilities, stackRoleName })
}
},
check: {
args: 1,
exec: () => {
const template = cli.input[1]
return simpleCfn.validate(template, {}).then(console.log)
}
},
outputs: {
args: 2,
exec: () => {
const name = cli.input[1]
return simpleCfn.outputs(name)
.then(JSON.stringify)
.then(console.log)
}
},
output: {
args: 3,
exec: () => {
const name = cli.input[1]
const field = cli.input[2]
return simpleCfn.output(name, field)
.then(console.log)
}
}
}
const exec = () => {
const len = cli.input.length
if (len < 1) {
return Promise.reject(new Error(chalk.red('Invalid Usage')))
}
const cmdName = cli.input[0]
const cmd = cmds[cmdName]
if (!cmd || (len < cmd.args)) {
return Promise.reject(new Error(chalk.red('Invalid Usage')))
}
return cmd.exec()
}
exec().catch(err => {
console.error(chalk.red(err.message || err))
})