1 | const { exec } = require('child_process')
|
2 | const chalk = require('chalk')
|
3 |
|
4 | const { printFooter, printHeader } = require('./prints')
|
5 | const { allRegions } = require('./regions')
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const getInstances = (region, regionEntered) => {
|
13 | let cmd = `aws ec2 describe-volumes --no-paginate --output json`
|
14 |
|
15 | if (region) cmd += ` --region ${region}`
|
16 |
|
17 | exec(cmd, (error, stdout, stderr) => {
|
18 | if (error) {
|
19 | console.log(stderr)
|
20 | } else {
|
21 | const data = JSON.parse(stdout).Volumes
|
22 |
|
23 | if (data.length != 0) {
|
24 | for (let index = 0; index < data.length; index++) {
|
25 | printVolumeData(data[index])
|
26 | }
|
27 | printFooter()
|
28 | } else if (regionEntered) {
|
29 | console.log(`\n ${chalk.red(`no instances found in ${region}`)}\n`)
|
30 | }
|
31 | }
|
32 | })
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const printVolumeData = volume => {
|
40 | console.log(`\n Volume ID: ${volume.VolumeId}`)
|
41 | console.log(` Availability Zone: ${volume.AvailabilityZone}`)
|
42 | printTags(volume.Tags)
|
43 | console.log(` State: ${volume.State}`)
|
44 |
|
45 | const encrypted = volume.Encrypted ? chalk.green('True') : chalk.red('False!')
|
46 | console.log(` ${chalk.blue('Encrypted')}: ${encrypted}`)
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | const printTags = tags => {
|
54 | if (tags.length && tags.length > 0) {
|
55 | for (let index = 0; index < tags.length; index++) {
|
56 | const tag = tags[index]
|
57 | console.log(` ${tag.Key}: ${tag.Value}`)
|
58 | }
|
59 | }
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const startTool = program => {
|
67 | const regionEntered = program.region && program.args.length > 0
|
68 | const region = regionEntered ? program.args[0] : allRegions
|
69 |
|
70 | printHeader()
|
71 | if (regionEntered) {
|
72 | getInstances(region, true)
|
73 | } else {
|
74 | for (let index = 0; index < allRegions.length; index++) {
|
75 | getInstances(allRegions[index].RegionName, false)
|
76 | }
|
77 | }
|
78 | }
|
79 |
|
80 | module.exports = { startTool }
|