Initial commit

This commit is contained in:
Matthew Sanabria 2020-04-23 12:57:44 -04:00
commit cd5e05ffbf
23 changed files with 26743 additions and 0 deletions

View file

@ -0,0 +1,36 @@
/**
* Acts as a listener for @actions/exec, by capturing STDOUT and STDERR
* streams, and exposing them via a contents attribute.
*
* @example
* // Instantiate a new listener
* const listener = new OutputListener();
* // Register listener against STDOUT stream
* await exec.exec('ls', ['-ltr'], {
* listeners: {
* stdout: listener.listener
* }
* });
* // Log out STDOUT contents
* console.log(listener.contents);
*/
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;

View file

@ -0,0 +1,9 @@
const os = require('os');
const path = require('path');
module.exports = (() => {
// If we're on Windows, then the executable ends with .exe
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
return [process.env.TERRAFORM_CLI_PATH, `terraform-bin${exeSuffix}`].join(path.sep);
})();

7295
wrapper/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

27
wrapper/package.json Normal file
View file

@ -0,0 +1,27 @@
{
"name": "terraform.js",
"version": "0.0.0",
"description": "JavaScript wrapper for terraform binary",
"main": "terraform.js",
"scripts": {
"test": "semistandard --env jest && jest --coverage",
"lint": "semistandard --env jest --fix",
"build": "ncc build terraform.js --out dist"
},
"author": "",
"dependencies": {
"@actions/core": "^1.2.3",
"@actions/exec": "^1.0.3",
"@actions/io": "^1.0.2"
},
"devDependencies": {
"@zeit/ncc": "0.22.1",
"jest": "^25.4.0",
"semistandard": "^14.2.0"
},
"semistandard": {
"ignore": [
"dist/**"
]
}
}

48
wrapper/terraform.js Executable file
View file

@ -0,0 +1,48 @@
#!/usr/bin/env node
const io = require('@actions/io');
const core = require('@actions/core');
const { exec } = require('@actions/exec');
const OutputListener = require('./lib/output-listener');
const pathToCLI = require('./lib/terraform-bin');
async function checkTerraform () {
// Setting check to `true` will cause `which` to throw if terraform isn't found
const check = true;
return io.which(pathToCLI, check);
}
(async () => {
// This will fail if Terraform isn't found, which is what we want
await checkTerraform();
// 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 terraform and capture output
const args = process.argv.slice(2);
const options = {
listeners,
ignoreReturnCode: true
};
const exitCode = await exec(pathToCLI, args, options);
core.debug(`Terraform 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));
// A non-zero exitCode is considered an error
if (exitCode !== 0) {
core.setFailed(`Terraform exited with code ${exitCode}.`);
}
})();

View file

@ -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');
});
});