UNPKG

2.17 kBJavaScriptView Raw
1const { exec } = require('child_process')
2const chalk = require('chalk')
3
4const { printFooter, printHeader } = require('./prints')
5const { allRegions } = require('./regions')
6
7/**
8 * Prints the information for each volume in a region.
9 * @param {string} region
10 * @param {bool} userInput
11 */
12const 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 * Prints the information for a passed in volume.
37 * @param {object} volume
38 */
39const 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 * Iterates through the tags array and prints the key and value.
51 * @param {array} tags
52 */
53const 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 * Takes in the commanderjs instance and starts the tool.
64 * @param {object} program
65 */
66const 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
80module.exports = { startTool }