-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·65 lines (50 loc) · 1.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
const core = require('@actions/core');
const commandExists = require('command-exists').sync;
const {spawnSync} = require('child_process');
const main = () => {
if (!commandExists('terraform')) {
console.error('Could not find terraform binary. Terminating...');
core.setFailed();
return;
}
let paths = core.getInput('paths');
const recursive = core.getInput('recursive');
if (paths) {
console.info('Checking paths: ', paths.replace(';', ', '));
paths = paths.split(';');
} else {
console.info('No specific path provided, checking entire repository.');
paths = ['.'];
}
const terraformBaseArgs = ['fmt', '-check'];
if (recursive == 'true') {
terraformBaseArgs.push('-recursive');
}
const misformatted = [];
paths.forEach((path) => {
const res = spawnSync('terraform', terraformBaseArgs.concat(path), {
stdio: 'pipe',
encoding: 'utf-8',
});
if (res.output[2]?.length > 0) {
console.error(res.output[2]);
core.setFailed();
return;
}
if (res.output[1]?.length > 0) {
misformatted.push(res.output[1].replaceAll('\n', ';'));
}
});
core.setOutput('misformatted', misformatted);
if (misformatted.length > 0) {
console.error(
'The following files are not correctly formatted:\n'
);
misformatted.forEach((element) => {
console.error(element.replaceAll(';', '\n'))
}),
core.setFailed();
return;
}
};
main();