Skip to content

Commit

Permalink
Support OpenTofu
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryGSC committed Jun 25, 2024
1 parent bf1b0b0 commit 3743f15
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ async function run() {
}
// we need to parse the terraform plan into a json string
async function jsonFromPlan(workingDir, planFileName) {
const exitCode = await exec.exec('which tofu', undefined, { silent: true, ignoreReturnCode: true });
const hasTofu = exitCode === 0;
const command = hasTofu ? 'tofu' : 'terraform';
// run terraform show -json to parse the plan into a json string
let output = '';
const options = {
Expand All @@ -91,7 +94,7 @@ async function jsonFromPlan(workingDir, planFileName) {
cwd: workingDir // execute the command from working directory 'dir'
};
core.debug(`execOptions: ${JSON.stringify(options)}`);
await exec.exec('terraform', ['show', '-json', planFileName], options);
await exec.exec(command, ['show', '-json', planFileName], options);
// pull out any extra fluff from terraform wrapper from the hashicorp/setup-terraform action
const json = output.match(/{.*}/);
if (json === null) {
Expand Down
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ async function run(): Promise<void> {

// we need to parse the terraform plan into a json string
async function jsonFromPlan(workingDir: string, planFileName: string): Promise<string> {
const exitCode = await exec.exec('which tofu', undefined, {silent: true, ignoreReturnCode: true})
const hasTofu = exitCode === 0
const command = hasTofu ? 'tofu' : 'terraform'

// run terraform show -json to parse the plan into a json string
let output = ''
const options: ExecOptions = {
Expand All @@ -66,7 +70,7 @@ async function jsonFromPlan(workingDir: string, planFileName: string): Promise<s
}

core.debug(`execOptions: ${JSON.stringify(options)}`)
await exec.exec('terraform', ['show', '-json', planFileName], options)
await exec.exec(command, ['show', '-json', planFileName], options)

// pull out any extra fluff from terraform wrapper from the hashicorp/setup-terraform action
const json = output.match(/{.*}/)
Expand Down

0 comments on commit 3743f15

Please sign in to comment.