-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (109 loc) · 3.45 KB
/
Copy pathindex.js
File metadata and controls
129 lines (109 loc) · 3.45 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const Mustache = require('mustache');
const readlineSync = require('readline-sync');
const fs = require('fs');
const { getFlag } = require('./utils');
if (!fs.existsSync('./output')) {
fs.mkdirSync('./output');
}
if (!fs.existsSync('./templates')) {
throw Error('Required templates dir');
}
const files = fs.readdirSync('./templates');
const templates = files.map((f) => f.replace('.json', ''));
const templateFromFlag = getFlag('template', true);
let templateName;
if (templateFromFlag) {
if (!templates.includes(templateFromFlag))
throw Error(
`Invalid template: ${templateFromFlag}, required: ${templates}`,
);
templateName = templateFromFlag;
} else {
const templateIndex = readlineSync.keyInSelect(
templates,
'Choose a template: ',
);
templateName = templates[templateIndex];
}
let config;
let region = '';
let project = '';
let account_id = '';
let stages = [];
let stacks = [];
const template = fs.readFileSync(`templates/${templateName}.json`, 'utf8');
const configFile = getFlag('config', true);
if (configFile) {
account_id = config.account_id;
region = config.region;
project = config[templateName].project;
stages = config[templateName].stages;
stacks = config[templateName].stacks;
} else {
account_id = getFlag('account-id', true);
if (!account_id) account_id = readlineSync.question('AWS Account ID: ');
region = getFlag('region', true);
if (!region) region = readlineSync.question('Region: ');
project = getFlag('project', true);
if (!project) project = readlineSync.question('Project: ');
let stage = getFlag('stage', true);
if (!stage) stage = readlineSync.question('Stage (comma separate): ');
stages = stage.split(',');
let stack = getFlag('stack', true);
if (!stack) stack = readlineSync.question('Stack (comma separate): ');
stacks = stack.split(',');
}
let merge = getFlag('merge');
let mergedIAM;
for (const stage of stages) {
for (const stack of stacks) {
const output = Mustache.render(template, {
region,
account_id,
project,
stage,
stack_name: stack,
stack_short_name: stack.substring(0, 6),
});
if (merge) {
const parsedOutput = JSON.parse(output);
if (!mergedIAM) {
mergedIAM = parsedOutput;
} else {
mergedIAM.Statement.push(...parsedOutput.Statement);
}
} else {
const fileName = `./output/${templateName}_${project}_${stage}_${stack}.json`;
fs.writeFileSync(fileName, output);
console.log(`-> Created ${fileName}`);
}
}
}
if (merge) {
const fileName = `./output/${templateName}_${project}_${stages.join('-')}_${stacks.join('-')}.json`;
const mergedStatements = {};
for (const index in mergedIAM.Statement) {
const statement = mergedIAM.Statement[index];
if (!mergedStatements[statement.Sid]) {
if (typeof statement.Resource === 'string') {
statement.Resource = [statement.Resource];
}
mergedStatements[statement.Sid] = statement;
} else {
if (typeof statement.Resource === 'string') {
mergedStatements[statement.Sid].Resource.push(statement.Resource);
} else if (statement.Resource instanceof Array) {
mergedStatements[statement.Sid].Resource.push(...statement.Resource);
} else {
console.warn('Unknown resource type!', statement.Resource);
}
}
}
mergedIAM.Statement = Object.values(mergedStatements).map((statement) => {
statement.Resource = [...new Set(statement.Resource)];
return statement;
});
const output = JSON.stringify(mergedIAM, null, '\t');
fs.writeFileSync(fileName, output);
console.log(`-> Created ${fileName}`);
}