forked from terraform-linters/setup-tflint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add wrapper to tflint to expose stdout, stderr, and exitcode
- Loading branch information
1 parent
7595030
commit 45a85aa
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}.`); | ||
})(); |