Skip to content

Commit

Permalink
feat: add wrapper to tflint to expose stdout, stderr, and exitcode
Browse files Browse the repository at this point in the history
  • Loading branch information
dreinhardt89 committed Jun 7, 2023
1 parent 7595030 commit 45a85aa
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
19 changes: 19 additions & 0 deletions wrapper/lib/output-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class OutputListener {
constructor() {
this._buff = [];
}

get listener() {
const listen = function listen(data) {
this._buff.push(data);
};

return listen.bind(this);
}

get contents() {
return this._buff.map((chunk) => chunk.toString()).join('');
}
}

module.exports = OutputListener;
5 changes: 5 additions & 0 deletions wrapper/lib/tflint-bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const path = require('path');

module.exports = (() => {
return [process.env.TFLINT_CLI_PATH, `tflint-bin`].join(path.sep);
})();
12 changes: 12 additions & 0 deletions wrapper/test/output-listener.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const OutputListener = require('../lib/output-listener');

describe('output-listener', () => {
it('receives and exposes data', () => {
const listener = new OutputListener();
const listen = listener.listener;
listen(Buffer.from('foo'));
listen(Buffer.from('bar'));
listen(Buffer.from('baz'));
expect(listener.contents).toEqual('foobarbaz');
});
});
53 changes: 53 additions & 0 deletions wrapper/tflint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node

const core = require('@actions/core');
const { exec } = require('@actions/exec');
const io = require('@actions/io');

const OutputListener = require('./lib/output-listener');
const pathToCLI = require('./lib/tflint-bin');

async function checkTflint() {
// Setting check to `true` will cause `which` to throw if tflint isn't found
const check = true;
return io.which(pathToCLI, check);
}

(async () => {
// This will fail if tflint isn't found, which is what we want
await checkTflint();

// Create listeners to receive output (in memory) as well
const stdout = new OutputListener();
const stderr = new OutputListener();
const listeners = {
stdout: stdout.listener,
stderr: stderr.listener,
};

// Execute tflint and capture output
const args = process.argv.slice(2);
const options = {
listeners,
ignoreReturnCode: true,
};
const exitCode = await exec(pathToCLI, args, options);
core.debug(`tflint exited with code ${exitCode}.`);
core.debug(`stdout: ${stdout.contents}`);
core.debug(`stderr: ${stderr.contents}`);
core.debug(`exitcode: ${exitCode}`);

// Set outputs, result, exitcode, and stderr
core.setOutput('stdout', stdout.contents);
core.setOutput('stderr', stderr.contents);
core.setOutput('exitcode', exitCode.toString(10));

if (exitCode === 0 || exitCode === 2) {
// A exitCode of 0 is considered a success
// An exitCode of 2 denotes no errors occurred, but issues found
return;
}

// A non-zero exitCode is considered an error
core.setFailed(`Terraform exited with code ${exitCode}.`);
})();

0 comments on commit 45a85aa

Please sign in to comment.