Skip to content

Commit fb62c63

Browse files
authored
Merge pull request #74 from RoseSecurity/support-tofu-file-extensions
feat: Support `.tofu` file extensions and pre-commit clean ups
2 parents ba56661 + 9624cc8 commit fb62c63

8 files changed

Lines changed: 24 additions & 24 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

.pre-commit-hooks.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
entry: hooks/infracost_breakdown.sh
55
language: script
66
require_serial: true
7-
files: \.((tf|tofu)(vars)?|hcl)$
7+
files: \.(tf|tofu|tfvars|hcl)$
88
exclude: \.terraform\/.*$
99

1010
- id: tofu_fmt
1111
name: OpenTofu fmt
1212
description: Rewrites all OpenTofu configuration files to a canonical format.
1313
entry: hooks/tofu_fmt.sh
1414
language: script
15-
files: \.(tf|tofu)(vars)?$
15+
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tfquery)\.hcl)$
1616
exclude: \.terraform\/.*$
1717

1818
- id: tofu_docs
@@ -23,7 +23,7 @@
2323
require_serial: true
2424
entry: hooks/tofu_docs.sh
2525
language: script
26-
files: (\.(tf|tofu)|\.terraform\.lock\.hcl)$
26+
files: \.(tf|tofu|terraform\.lock\.hcl)$
2727
exclude: \.terraform\/.*$
2828

2929
- id: tofu_docs_without_aggregate_type_defaults
@@ -52,7 +52,7 @@
5252
require_serial: true
5353
entry: hooks/tofu_validate.sh
5454
language: script
55-
files: \.(tf|tofu)(vars)?$
55+
files: \.(tf|tofu|tfvars|terraform\.lock\.hcl)$
5656
exclude: \.terraform\/.*$
5757

5858
- id: tofu_providers_lock
@@ -70,7 +70,7 @@
7070
require_serial: true
7171
entry: hooks/tofu_tflint.sh
7272
language: script
73-
files: \.(tf|tofu)(vars)?$
73+
files: \.(tf|tofu|tfvars)$
7474
exclude: \.terraform\/.*$
7575

7676
- id: terragrunt_fmt
@@ -104,7 +104,7 @@
104104
Static analysis of OpenTofu templates to spot potential security issues.
105105
require_serial: true
106106
entry: hooks/tofu_tfsec.sh
107-
files: \.(tf|tofu)(vars)?$
107+
files: \.(tf|tofu|tfvars)$
108108
language: script
109109

110110
- id: tofu_trivy
@@ -113,7 +113,7 @@
113113
Static analysis of OpenTofu templates to spot potential security issues.
114114
require_serial: true
115115
entry: hooks/tofu_trivy.sh
116-
files: \.(tf|tofu)(vars)?$
116+
files: \.(tf|tofu|tfvars)$
117117
language: script
118118

119119
- id: checkov
@@ -123,7 +123,7 @@
123123
language: python
124124
pass_filenames: false
125125
always_run: false
126-
files: \.tf$
126+
files: \.(tf|tofu)$
127127
exclude: \.terraform\/.*$
128128
require_serial: true
129129

@@ -145,7 +145,7 @@
145145
pass_filenames: false
146146
always_run: false
147147
require_serial: true
148-
files: \.tf$
148+
files: \.(tf|tofu)$
149149
exclude: \.terraform\/.*$
150150

151151
- id: terrascan

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

hooks/tofu_wrapper_module_for_each.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ EOF
315315
all_tf_content=$(find "${full_module_dir}" -regex '.*\.(tf|tofu)' -maxdepth 1 -type f -exec cat {} +)
316316

317317
if [[ ! $all_tf_content ]]; then
318-
common::colorify "yellow" "Skipping ${full_module_dir} because there are no *.(tf|tofu) files."
318+
common::colorify "yellow" "Skipping ${full_module_dir} because there are no .tf or .tofu files."
319319
continue
320320
fi
321321

0 commit comments

Comments
 (0)