Skip to content

Commit 9624cc8

Browse files
author
Michael Rosenfeld
committed
feat: support .tofu file extension and update hooks/tools
Add support for the `.tofu` file extension in OpenTofu config matching and documentation. Update pre-commit hooks, regex, and README to reflect support for `.tofu` files alongside `.tf` and `.tfvars`. Fix minor shell quoting and array assignment issues. Upgrade pre-commit-hooks to v6.0.0. Signed-off-by: Michael Rosenfeld <michael@rosesecurity.com>
1 parent 451aaa5 commit 9624cc8

6 files changed

Lines changed: 14 additions & 14 deletions

File tree

.github/workflows/build-image.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,3 @@ jobs:
9898
tags: |
9999
registry.hub.docker.com/tofuutils/pre-commit-opentofu:nightly
100100
provenance: false
101-

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.5.0
3+
rev: v6.0.0
44
hooks:
55
# Git style
66
- id: check-added-large-files

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ RUN [ ${PRE_COMMIT_VERSION} = "latest" ] && pip3 install --no-cache-dir pre-comm
2222

2323
RUN curl -LO https://github.com/opentofu/opentofu/releases/download/v${TOFU_VERSION}/tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip \
2424
&& curl -LO https://github.com/opentofu/opentofu/releases/download/v${TOFU_VERSION}/tofu_${TOFU_VERSION}_SHA256SUMS \
25-
&& [ $(sha256sum "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" | cut -f 1 -d ' ') = "$(grep "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" tofu_*_SHA256SUMS | cut -f 1 -d ' ')" ] \
25+
&& [ "$(sha256sum "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" | cut -f 1 -d ' ')" = "$(grep "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" tofu_*_SHA256SUMS | cut -f 1 -d ' ')" ] \
2626
&& unzip tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip -d /usr/bin/ \
2727
&& rm "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" \
2828
&& rm "tofu_${TOFU_VERSION}_SHA256SUMS"
@@ -235,4 +235,3 @@ ENV INFRACOST_API_KEY=${INFRACOST_API_KEY:-}
235235
ENV INFRACOST_SKIP_UPDATE_CHECK=${INFRACOST_SKIP_UPDATE_CHECK:-false}
236236

237237
ENTRYPOINT [ "/entrypoint.sh" ]
238-

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ repos:
294294

295295
## Available Hooks
296296

297-
There are several [pre-commit](https://pre-commit.com/) hooks to keep OpenTofu configurations (both `*.tf` and `*.tfvars`) and Terragrunt configurations (`*.hcl`) in a good shape:
297+
There are several [pre-commit](https://pre-commit.com/) hooks to keep OpenTofu configurations (`*.tf`, `*.tofu`, and `*.tfvars`) and Terragrunt configurations (`*.hcl`) in a good shape:
298298

299299
<!-- markdownlint-disable no-inline-html -->
300300
| Hook name | Description | Dependencies<br><sup>[Install instructions here](#1-install-dependencies)</sup> |
@@ -326,6 +326,8 @@ Check the [source file](https://github.com/tofuutils/pre-commit-opentofu/blob/ma
326326

327327
OpenTofu operates on a per-dir basis, while `pre-commit` framework only supports files and files that exist. This means if you only remove the TF-related file without any other changes in the same dir, checks will be skipped. Example and details [here](https://github.com/pre-commit/pre-commit/issues/3048).
328328

329+
Hooks match `*.tofu` files where OpenTofu configuration files are supported, but some wrapped third-party tools may lag behind OpenTofu's native `*.tofu` parsing. If a hook runs `terraform-docs`, `tflint`, `tfsec`, `trivy`, `checkov`, `infracost`, or `tfupdate`, make sure the installed tool version supports the file extensions used in your repository.
330+
329331
### All hooks: Usage of environment variables in `--args`
330332

331333
> All, except deprecated hooks: `checkov`, `tofu_docs_replace`
@@ -930,7 +932,7 @@ To replicate functionality in `tofu_docs` hook:
930932
require_serial: true
931933
entry: .generate-providers.sh
932934
language: script
933-
files: \.tf(vars)?$
935+
files: \.(tf|tofu|tfvars)$
934936
pass_filenames: false
935937
936938
- repo: https://github.com/pre-commit/pre-commit-hooks

hooks/_common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function common::parse_and_export_env_vars {
123123
# `$arg` will be checked in `if` conditional, `$ARGS` will be used in the next functions.
124124
# shellcheck disable=SC2016 # '${' should not be expanded
125125
arg=${arg/'${'$env_var_name'}'/$env_var_value}
126-
ARGS[$arg_idx]=$arg
126+
ARGS[arg_idx]=$arg
127127
# shellcheck disable=SC2016 # '${' should not be expanded
128128
common::colorify "green" 'After ${'"$env_var_name"'} expansion: '"'$arg'\n"
129129
continue

hooks/terragrunt_validate_inputs.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function normalize_validate_args_for_modern_terragrunt {
3232

3333
for arg_idx in "${!ARGS[@]}"; do
3434
case "${ARGS[$arg_idx]}" in
35-
--terragrunt-strict-validate|--strict-validate)
36-
ARGS[$arg_idx]="--strict"
35+
--terragrunt-strict-validate | --strict-validate)
36+
ARGS[arg_idx]="--strict"
3737
;;
3838
esac
3939
done
@@ -45,7 +45,7 @@ function terragrunt_version_ge_0_78 {
4545
local major
4646
local minor
4747

48-
version_raw=$(terragrunt --version 2>/dev/null || true)
48+
version_raw=$(terragrunt --version 2> /dev/null || true)
4949
version=$(echo "$version_raw" | sed -E 's/.*v?([0-9]+)\.([0-9]+)\.([0-9]+).*/\1.\2.\3/')
5050

5151
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
@@ -109,8 +109,8 @@ function legacy_unit_dirs_from_files {
109109
if common::is_hook_run_on_whole_repo "$HOOK_ID" "${FILES[@]}"; then
110110
find . -type f -name terragrunt.hcl \
111111
-not -path '*/.terragrunt-cache/*' \
112-
-not -path '*/.terraform/*' \
113-
| sort -u | while read -r unit_file; do
112+
-not -path '*/.terraform/*' |
113+
sort -u | while read -r unit_file; do
114114
dirname "$unit_file"
115115
done
116116
return
@@ -137,8 +137,8 @@ function legacy_unit_dirs_from_files {
137137
if [[ ${#unit_files[@]} -eq 0 ]]; then
138138
find . -type f -name terragrunt.hcl \
139139
-not -path '*/.terragrunt-cache/*' \
140-
-not -path '*/.terraform/*' \
141-
| sort -u | while read -r unit_file; do
140+
-not -path '*/.terraform/*' |
141+
sort -u | while read -r unit_file; do
142142
dirname "$unit_file"
143143
done
144144
return

0 commit comments

Comments
 (0)