-
Notifications
You must be signed in to change notification settings - Fork 1
/
terraform.js
335 lines (283 loc) · 6.44 KB
/
terraform.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
function abs(value) {
return Math.abs(value);
}
function abspath(value) {
return path.resolve(this.path.root, value);
}
function can(exp) {
try {
eval(exp);
return true;
} catch (e) {
return false;
}
}
function ceil(value) {
return Math.ceil(value);
}
function cidrsubnet(cidr, newbits, netnum) {
let parts = cidr.split('/');
let ip = parts[0];
let mask = parseInt(parts[1], 10);
let newmask = mask + newbits;
let newcidr = ip + '/' + newmask;
return newcidr;
}
function concat(...lists) {
return [].concat(...lists);
}
function coalesce(...values) {
for (let value of values) {
if (value !== null && value !== '') {
return value;
}
}
return null;
}
function contains(list, value) {
return list.includes(value);
}
function endswith(value, suffix) {
return value.endsWith(suffix);
}
function file(filePath) {
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(this.path.root, filePath);
}
if (!fs.existsSync(filePath)) {
return 'FileNotFound';
}
return fs.readFileSync(filePath, 'utf8');
}
function filebase64sha256(filePath) {
let content = file(filePath);
return crypto.createHash('sha256').update(content).digest('base64');
}
function floor(value) {
return Math.floor(value);
}
function format(formatString, ...args) {
return formatString.replace(/%([#vtbodxXeEfFgGsq%])/g, (match, specifier) => {
if (specifier === '%') {
return '%';
}
const value = args.shift();
switch (specifier) {
case 'v':
return String(value);
case '#v':
return JSON.stringify(value);
case 't':
return Boolean(value).toString();
case 'b':
return parseInt(value, 10).toString(2);
case 'd':
return parseInt(value, 10).toString(10);
case 'o':
return parseInt(value, 10).toString(8);
case 'x':
return parseInt(value, 10).toString(16);
case 'X':
return parseInt(value, 10).toString(16).toUpperCase();
case 'e':
return Number(value).toExponential();
case 'E':
return Number(value).toExponential().toUpperCase();
case 'f':
return Number(value).toFixed();
case 'g':
return Number(value).toPrecision();
case 'G':
return Number(value).toPrecision().toUpperCase();
case 's':
return String(value);
case 'q':
return JSON.stringify(String(value));
default:
return match;
}
});
}
function index(list, value) {
return list.indexOf(value);
}
function join(separator, list) {
return list.join(separator);
}
function jsonencode(value) {
return JSON.stringify(value);
}
function jsondecode(value) {
return JSON.parse(value);
}
function keys(map) {
return Object.keys(map);
}
function length(value) {
if (typeof value === 'string' || Array.isArray(value)) {
return value.length;
} else if (value instanceof Map) {
return value.size;
} else if (typeof value === 'object' && value !== null) {
return Object.keys(value).length;
} else {
console.log('Unsupported type for size function');
return 0;
}
}
function list(values) {
return [values];
}
function log(value) {
return Math.log(value);
}
function lookup(map, key, defaultValue = null) {
return map.hasOwnProperty(key) ? map[key] : defaultValue;
}
function lower(value) {
return value.toLowerCase();
}
function max(...values) {
return Math.max(...values);
}
function merge(...maps) {
return Object.assign({}, ...maps);
}
function min(...values) {
return Math.min(...values);
}
function object(values) {
return values;
}
function parseint(value, base = 10) {
return parseInt(value, base);
}
function pow(base, exponent) {
return Math.pow(base, exponent);
}
function range(start, end) {
return Array.from({ length: end - start }, (_, i) => i + start);
}
function regex(pattern, value) {
return new RegExp(pattern).test(value);
}
function replace(value, search, replacement) {
return value.split(search).join(replacement);
}
function reverse(list) {
return list.slice().reverse();
}
function setproduct(...lists) {
return lists.reduce((acc, list) => acc.flatMap((x) => list.map((y) => x.concat([y]))), [[]]);
}
function sha1(value) {
return crypto.createHash('sha1').update(value).digest('hex');
}
function signum(value) {
return Math.sign(value);
}
function slice(list, start, end) {
return list.slice(start, end);
}
function sort(list) {
return list.slice().sort();
}
function split(separator, value) {
return value.split(separator);
}
function sqrt(value) {
return Math.sqrt(value);
}
function startswith(value, prefix) {
return value.startsWith(prefix);
}
function strlen(value) {
return value.length;
}
function substr(value, start, length) {
return value.substr(start, length);
}
function timestamp() {
return new Date().toISOString();
}
function tolist(list) {
return list;
}
function tomap(map) {
return map;
}
function trimspace(value) {
return value.trim();
}
function tryTerraform(...expressions) {
for (const expression of expressions) {
try {
return expression();
} catch (e) {
continue;
}
}
return null;
}
function upper(value) {
return value.toUpperCase();
}
function uuid() {
return crypto.randomBytes(16).toString('hex');
}
const Terraform = {
abs,
abspath,
can,
ceil,
cidrsubnet,
concat,
coalesce,
contains,
endswith,
file,
filebase64sha256,
floor,
format,
index,
join,
jsonencode,
jsondecode,
keys,
length,
list,
log,
lookup,
lower,
max,
merge,
min,
object,
parseint,
pow,
range,
regex,
replace,
reverse,
setproduct,
sha1,
signum,
slice,
sort,
split,
sqrt,
startswith,
strlen,
substr,
timestamp,
tomap,
tolist,
trimspace,
tryTerraform,
upper,
uuid,
};
module.exports = Terraform;