From ca35e016e50e8c94f1bafa99f9500841b5e9c38c Mon Sep 17 00:00:00 2001 From: "Marko (ServerlessLife)" Date: Wed, 20 Aug 2025 08:17:51 +0200 Subject: [PATCH] fix: Support .tofu files [#133] --- src/frameworks/openTofuFramework.ts | 37 +++++++++++ src/frameworks/terraformFramework.ts | 6 +- test/cdk-basic/CdkbasicStack.yaml | 0 test/cdk-basic/cdk-synth.log | 72 ++++++++++++++++++++++ test/opentofu-basic/{main.tf => main.tofu} | 0 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 test/cdk-basic/CdkbasicStack.yaml create mode 100644 test/cdk-basic/cdk-synth.log rename test/opentofu-basic/{main.tf => main.tofu} (100%) diff --git a/src/frameworks/openTofuFramework.ts b/src/frameworks/openTofuFramework.ts index 6d2fca8c..981d1cd6 100755 --- a/src/frameworks/openTofuFramework.ts +++ b/src/frameworks/openTofuFramework.ts @@ -1,6 +1,9 @@ import { exec } from 'child_process'; import { promisify } from 'util'; import { TerraformFramework } from './terraformFramework.js'; +import path from 'path'; +import { Logger } from '../logger.js'; +import fs from 'fs/promises'; export const execAsync = promisify(exec); @@ -15,6 +18,40 @@ export class OpenTofuFramework extends TerraformFramework { return 'opentofu'; } + /** + * Can this class handle the current project + * @returns + */ + public async canHandle(): Promise { + // check for any file with .tf, .tf.json, .tofu, or .tofu.json extension + const files = await fs.readdir(process.cwd()); + const r = files.some( + (f) => + f.endsWith('.tf') || + f.endsWith('.tf.json') || + f.endsWith('.tofu') || + f.endsWith('.tofu.json'), + ); + + if (!r) { + Logger.verbose( + `[${this.logName}] This is not a ${this.logName} project. There are no *.tf, *.tf.json, *.tofu, or *.tofu.json files in ${path.resolve('.')} folder.`, + ); + return false; + } else { + // check if Terraform or OpenTofu is installed + try { + await execAsync(this.checkInstalledCommand); + return true; + } catch { + Logger.verbose( + `[${this.logName}] This is not a ${this.logName} project. ${this.logName} is not installed.`, + ); + return false; + } + } + } + /** * Name of the framework in logs */ diff --git a/src/frameworks/terraformFramework.ts b/src/frameworks/terraformFramework.ts index aa22bffd..ca3d8194 100755 --- a/src/frameworks/terraformFramework.ts +++ b/src/frameworks/terraformFramework.ts @@ -67,13 +67,13 @@ export class TerraformFramework implements IFramework { * @returns */ public async canHandle(): Promise { - // is there any filey with *.tf extension + // check for any files with .tf or .tf.json extension const files = await fs.readdir(process.cwd()); - const r = files.some((f) => f.endsWith('.tf')); + const r = files.some((f) => f.endsWith('.tf') || f.endsWith('.tf.json')); if (!r) { Logger.verbose( - `[${this.logName}] This is not a ${this.logName} project. There are no *.tf files in ${path.resolve('.')} folder.`, + `[${this.logName}] This is not a ${this.logName} project. There are no *.tf or *.tf.json files in ${path.resolve('.')} folder.`, ); return false; } else { diff --git a/test/cdk-basic/CdkbasicStack.yaml b/test/cdk-basic/CdkbasicStack.yaml new file mode 100644 index 00000000..e69de29b diff --git a/test/cdk-basic/cdk-synth.log b/test/cdk-basic/cdk-synth.log new file mode 100644 index 00000000..f5368557 --- /dev/null +++ b/test/cdk-basic/cdk-synth.log @@ -0,0 +1,72 @@ +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +Bundling asset CdkbasicStack/TestTsCommonJs/Code/Stage... + + ...5d8888acd02a3a8300fd1812aaaadea60eb4d5e94fdc8b74-building/index.js 2.5kb + +⚡ Done in 5ms +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +Bundling asset CdkbasicStack/TestTsEsModule/Code/Stage... + + ...6da3d4a35f4bd8700dc1cd1e51cb1f8e221896ac5b1e3a6-building/index.mjs 952b + +⚡ Done in 3ms +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +Bundling asset CdkbasicStack/TestJsCommonJs/Code/Stage... + + ...13fdc37e5dff5529e8d38cc279c44a91ddc79ee0a6999101-building/index.js 970b + +⚡ Done in 3ms +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +Bundling asset CdkbasicStack2/TestJsEsModule/Code/Stage... + + ...8ac79e932e94cba6a9050b4975ea1709894b6ab846714ba-building/index.mjs 952b + +⚡ Done in 3ms +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +[WARNING] aws-cdk-lib.aws_lambda.FunctionOptions#logRetention is deprecated. + use `logGroup` instead + This API will be removed in the next major release. +You currently have 50 unconfigured feature flags that may require attention to keep your application up-to-date. Run 'cdk flags' to learn more. + +NOTICES (What's this? https://github.com/aws/aws-cdk/wiki/CLI-Notices) + +34892 CDK CLI will collect telemetry data on command usage starting at version 2.1100.0 (unless opted out) + + Overview: We do not collect customer content and we anonymize the + telemetry we do collect. See the attached issue for more + information on what data is collected, why, and how to + opt-out. Telemetry will NOT be collected for any CDK CLI + version prior to version 2.1100.0 - regardless of + opt-in/out. You can also preview the telemetry we will start + collecting by logging it to a local file, by adding + `--unstable=telemetry --telemetry-file=my/local/file` to any + `cdk` command. + + Affected versions: cli: ^2.0.0 + + More information at: https://github.com/aws/aws-cdk/issues/34892 + + +If you don’t want to see a notice anymore, use "cdk acknowledge ". For example, "cdk acknowledge 34892". diff --git a/test/opentofu-basic/main.tf b/test/opentofu-basic/main.tofu similarity index 100% rename from test/opentofu-basic/main.tf rename to test/opentofu-basic/main.tofu