mirror of
https://github.com/opentofu/setup-opentofu.git
synced 2025-12-31 15:02:19 +00:00
feat: ported action definition from setup-terraform
Signed-off-by: Dmitry Kisler <admin@dkisler.com>
This commit is contained in:
parent
c37e0c575a
commit
01bef202d2
19 changed files with 18728 additions and 3 deletions
41
wrapper/lib/output-listener.js
Normal file
41
wrapper/lib/output-listener.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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;
|
||||
14
wrapper/lib/tofu-bin.js
Normal file
14
wrapper/lib/tofu-bin.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
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.TOFU_CLI_PATH, `tofu-bin${exeSuffix}`].join(path.sep);
|
||||
})();
|
||||
17
wrapper/test/output-listener.test.js
Normal file
17
wrapper/test/output-listener.test.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
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');
|
||||
});
|
||||
});
|
||||
59
wrapper/tofu.js
Executable file
59
wrapper/tofu.js
Executable file
|
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env node
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
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/tofu-bin');
|
||||
|
||||
async function checkTofu () {
|
||||
// Setting check to `true` will cause `which` to throw if tofu isn't found
|
||||
const check = true;
|
||||
return io.which(pathToCLI, check);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
// This will fail if tofu isn't found, which is what we want
|
||||
await checkTofu();
|
||||
|
||||
// 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 tofu and capture output
|
||||
const args = process.argv.slice(2);
|
||||
const options = {
|
||||
listeners,
|
||||
ignoreReturnCode: true
|
||||
};
|
||||
const exitCode = await exec(pathToCLI, args, options);
|
||||
core.debug(`OpenTofu 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 may be returned when the '-detailed-exitcode' option
|
||||
// is passed to plan. This denotes Success with non-empty
|
||||
// diff (changes present).
|
||||
return;
|
||||
}
|
||||
|
||||
// A non-zero exitCode is considered an error
|
||||
core.setFailed(`OpenTofu exited with code ${exitCode}.`);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue