forked from kostia-official/tf-state-mover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·112 lines (93 loc) · 2.85 KB
/
index.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
#!/usr/bin/env node
const chalk = require('chalk');
const execa = require('execa');
const getStream = require('get-stream');
const inquirer = require('inquirer');
const semver = require('semver')
const stripAnsi = require('strip-ansi');
const _ = require('lodash');
const tfVersionRegex = /Terraform v(.*)/;
// terraform v0.11
const addRegexV11 = / \+ ((?!create).*)/;
const deleteRegexV11 = / - ((?!destroy).*)/;
// terraform v0.12
const addRegexV12 = / # (.*) will be created/;
const deleteRegexV12 = / # (.*) will be destroyed/;
(async function run() {
try {
const plan = await parsePlan();
await prompt(plan);
console.info(chalk.blue('Finishing moving resources'));
} catch (err) {
console.error(chalk.red(err));
}
})();
async function parsePlan() {
const tfVersionOut = await shell('terraform version', false);
const tfVersion = tfVersionOut.match(tfVersionRegex)[1];
console.info(chalk.blue(`Runnning on Terraform v${tfVersion}`));
let tfPlanOut = await shell('terraform plan');
tfPlanOut = stripAnsi(tfPlanOut);
let addRegex = addRegexV12
let deleteRegex = deleteRegexV12
if (semver.lt(tfVersion, '0.12.0')) {
addRegex = addRegexV11
deleteRegex = deleteRegexV11
}
const toAdd = parseResource(tfPlanOut, addRegex);
const toDelete = parseResource(tfPlanOut, deleteRegex);
toAdd.push(new inquirer.Separator())
toDelete.push(new inquirer.Separator())
return { toAdd, toDelete };
}
function parseResource(tfPlan, regex) {
return tfPlan
.split('\n')
.filter(s => regex.test(s))
.map(s => s.match(regex)[1]);
}
async function prompt(plan) {
if (isEmpty(plan, 'toDelete')) return;
const planAfterMove = await pickMove(plan);
await prompt(planAfterMove);
}
async function isProceed() {
const { isProceed } = await inquirer.prompt([{
type: 'confirm',
name: 'isProceed',
message: 'Move another resource?',
default: true
}]);
return isProceed;
}
async function pickMove({ toDelete, toAdd }) {
const { from, to } = await inquirer.prompt([{
type: 'list',
name: 'from',
message: 'Move from',
choices: toDelete,
pageSize: Math.min(toDelete.length, 10)
}, {
type: 'list',
name: 'to',
message: 'Move to ',
choices: toAdd,
pageSize: Math.min(toAdd.length, 10)
}]);
const toDeleteAfterMove = _.remove(toDelete, item => item !== from);
const toAddAfterMove = _.remove(toAdd, item => item !== to);
await shell(`terraform state mv ${from} ${to}`);
return { toDelete: toDeleteAfterMove, toAdd: toAddAfterMove };
}
function shell(command, printOutput = true) {
const [first, ...other] = command.split(' ');
const { stdout, stderr } = execa(first, other);
if (printOutput) {
stdout.pipe(process.stdout);
stderr.pipe(process.stderr);
}
return getStream(stdout);
}
function isEmpty(object, key) {
return _.get(object, key).length === 1;
}