-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·94 lines (81 loc) · 2.08 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
#!/usr/bin/env node
'use strict'
const cfn = require('./')
const _ = require('lodash')
const chalk = require('chalk')
const meow = require('meow')
const Promise = require('bluebird')
// const path = require('path')
// const cwd = process.cwd()
Promise.longStackTraces()
const cli = meow(`
Usage
cfn deploy {stack name} {template} [--{param key}={param value}...]
cfn delete {stack name}
cfn outputs {stack name}
cfn output {stack name} {field name}
Examples
cfn deploy my-stack template.js
cfn deploy your_stack template.yml --ImageId=ami-828283 --VpcId=vpc-828283
cfn delete your_stack
cfn outputs my-stack
cfn output my-stack my-field
`)
const cmds = {
deploy: {
args: 3,
exec: () => {
const name = cli.input[1]
// const template = path.join(cwd, cli.input[2])
const template = cli.input[2]
let cfParams = cli.flags
if (cfParams && cfParams.length > 0) {
console.log(`${chalk.cyan('Cloud Formation Parameters')}\n==========================`)
console.log(_.toPairs(cfParams).map(a => `${a[0]}: ${a[1]}`).join('\n'))
console.log('==========================`)\n')
}
return cfn({name, template, cfParams})
}
},
delete: {
args: 2,
exec: () => {
const name = cli.input[1]
return cfn.delete(name)
}
},
outputs: {
args: 2,
exec: () => {
const name = cli.input[1]
return cfn.outputs(name)
.then(JSON.stringify)
.then(console.log)
}
},
output: {
args: 3,
exec: () => {
const name = cli.input[1]
const field = cli.input[2]
return cfn.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))
cli.showHelp(1)
})