-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathindex.js
executable file
·95 lines (83 loc) · 2.44 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
#!/usr/bin/env node
const program = require('commander');
const awsGetStack = require('./lib/stack');
const awsGetTemplate = require('./lib/template');
const configuration = require('./lib/configuration');
const state = require('./lib/state');
const cleanHCL = require('./lib/cleanHCL');
const getStdin = require('get-stdin');
const formatOptions = opts => {
return {
stack: opts.parent.stack,
resourceName: opts.parent.resourceName || 'main',
};
};
const getStack = opts => {
return getStdin().then(result => stdinOrGetStack(result, opts));
};
const stdinOrGetStack = (result, opts) => {
if (result !== '' && opts.stack === '-') {
return JSON.parse(result);
}
return awsGetStack(opts);
};
const handleError = err => {
console.log(err);
process.exit(1);
};
program.option('-s, --stack <stack>', 'The CloudFormation stack to import');
program.option(
'-r, --resource-name <resourceName>',
'The name to assign the terraform resource'
);
program
.command('config')
.description('Generates Terraform configuration in JSON')
.action(options => {
const opts = formatOptions(options);
return getStack(opts)
.then(result => {
return configuration.generate(opts, result.Stacks[0]);
})
.then(result => console.log(JSON.stringify(result)))
.catch(err => handleError(err));
return getStack(opts)
.then(result => {
return configuration.generate(opts, result.Stacks[0]);
})
.then(result => console.log(JSON.stringify(result)))
.catch(err => handleError(err));
});
program
.command('state')
.description('Generates Terraform state file in JSON')
.action(options => {
const opts = formatOptions(options);
return getStack(opts)
.then(result => {
return state.generate(opts, result.Stacks[0]);
})
.then(result => console.log(JSON.stringify(result)))
.catch(err => handleError(err));
});
program
.command('template')
.description('Prints the CloudFormation Stack Template')
.action(options => {
const opts = formatOptions(options);
return awsGetTemplate(opts)
.then(result => console.log(JSON.stringify(result)))
.catch(err => handleError(err));
});
program
.command('clean-hcl')
.description('Cleans generated HCL according to my preferences')
.action(options => {
const opts = formatOptions(options);
return getStdin()
.then(str => cleanHCL.clean(opts, str))
.then(result => console.log(result))
.catch(err => handleError(err));
});
program.parse(process.argv);
// TODO mark required arguments