Skip to content

Commit 3f2a01a

Browse files
authored
Merge pull request #14 from NorddeutscherRundfunk/support_tofu_files
feat: Support .tofu files
2 parents 81b78c8 + e059c58 commit 3f2a01a

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

.pre-commit-hooks.yaml

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,37 @@
44
entry: hooks/infracost_breakdown.sh
55
language: script
66
require_serial: true
7-
files: \.(tf(vars)?|hcl)$
7+
files: \.((tf|tofu)(vars)?|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|\.tfvars)$
15+
files: \.(tf|tofu)(vars)?$
1616
exclude: \.terraform\/.*$
1717

1818
- id: tofu_docs
1919
name: OpenTofu docs
20-
description: Inserts input and output documentation into README.md (using terraform-docs).
20+
description:
21+
Inserts input and output documentation into README.md (using
22+
terraform-docs).
2123
require_serial: true
2224
entry: hooks/tofu_docs.sh
2325
language: script
24-
files: (\.tf|\.terraform\.lock\.hcl)$
26+
files: (\.(tf|tofu)|\.terraform\.lock\.hcl)$
2527
exclude: \.terraform\/.*$
2628

2729
- id: tofu_docs_without_aggregate_type_defaults
2830
name: OpenTofu docs (without aggregate type defaults)
29-
description: Inserts input and output documentation into README.md (using terraform-docs). Identical to terraform_docs.
31+
description:
32+
Inserts input and output documentation into README.md (using
33+
terraform-docs). Identical to terraform_docs.
3034
require_serial: true
3135
entry: hooks/tofu_docs.sh
3236
language: script
33-
files: (\.tf)$
37+
files: \.(tf|tofu)$
3438
exclude: \.terraform\/.*$
3539

3640
- id: tofu_docs_replace
@@ -39,7 +43,7 @@
3943
require_serial: true
4044
entry: hooks/tofu_docs_replace.py
4145
language: python
42-
files: (\.tf)$
46+
files: \.(tf|tofu)$
4347
exclude: \.terraform\/.*$
4448

4549
- id: tofu_validate
@@ -48,7 +52,7 @@
4852
require_serial: true
4953
entry: hooks/tofu_validate.sh
5054
language: script
51-
files: (\.tf|\.tfvars)$
55+
files: \.(tf|tofu)(vars)?$
5256
exclude: \.terraform\/.*$
5357

5458
- id: tofu_providers_lock
@@ -66,12 +70,13 @@
6670
require_serial: true
6771
entry: hooks/tofu_tflint.sh
6872
language: script
69-
files: (\.tf|\.tfvars)$
73+
files: \.(tf|tofu)(vars)?$
7074
exclude: \.terraform\/.*$
7175

7276
- id: terragrunt_fmt
7377
name: Terragrunt fmt
74-
description: Rewrites all Terragrunt configuration files to a canonical format.
78+
description:
79+
Rewrites all Terragrunt configuration files to a canonical format.
7580
entry: hooks/terragrunt_fmt.sh
7681
language: script
7782
files: (\.hcl)$
@@ -87,18 +92,20 @@
8792

8893
- id: tofu_tfsec
8994
name: OpenTofu validate with tfsec (deprecated, use "tofu_trivy")
90-
description: Static analysis of OpenTofu templates to spot potential security issues.
95+
description:
96+
Static analysis of OpenTofu templates to spot potential security issues.
9197
require_serial: true
9298
entry: hooks/tofu_tfsec.sh
93-
files: \.tf(vars)?$
99+
files: \.(tf|tofu)(vars)?$
94100
language: script
95101

96102
- id: tofu_trivy
97103
name: OpenTofu validate with trivy
98-
description: Static analysis of OpenTofu templates to spot potential security issues.
104+
description:
105+
Static analysis of OpenTofu templates to spot potential security issues.
99106
require_serial: true
100107
entry: hooks/tofu_trivy.sh
101-
files: \.tf(vars)?$
108+
files: \.(tf|tofu)(vars)?$
102109
language: script
103110

104111
- id: checkov
@@ -118,7 +125,7 @@
118125
entry: hooks/tofu_checkov.sh
119126
language: script
120127
always_run: false
121-
files: \.tf$
128+
files: \.(tf|tofu)$
122129
exclude: \.terraform\/.*$
123130
require_serial: true
124131

@@ -138,7 +145,7 @@
138145
description: Runs terrascan on OpenTofu templates.
139146
language: script
140147
entry: hooks/terrascan.sh
141-
files: \.tf$
148+
files: \.(tf|tofu)$
142149
exclude: \.terraform\/.*$
143150
require_serial: true
144151

@@ -149,5 +156,5 @@
149156
entry: hooks/tfupdate.sh
150157
args:
151158
- --args=terraform
152-
files: \.tf$
159+
files: \.(tf|tofu)$
153160
require_serial: true

hooks/tofu_docs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function tofu_docs {
155155
#
156156
if $create_if_not_exist && [[ ! -f "$text_file" ]]; then
157157
dir_have_tf_files="$(
158-
find . -maxdepth 1 -type f | sed 's|.*\.||' | sort -u | grep -oE '^tf$|^tfvars$' ||
158+
find . -maxdepth 1 -type f | sed 's|.*\.||' | sort -u | grep -oE '^tofu|^tf$|^tfvars$' ||
159159
exit 0
160160
)"
161161

hooks/tofu_docs_replace.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,54 @@
77
def main(argv=None):
88
parser = argparse.ArgumentParser(
99
description="""Run terraform-docs on a set of files. Follows the standard convention of
10-
pulling the documentation from main.tf in order to replace the entire
10+
pulling the documentation from main.(tf|tofu) in order to replace the entire
1111
README.md file each time."""
1212
)
1313
parser.add_argument(
14-
'--dest', dest='dest', default='README.md',
14+
"--dest",
15+
dest="dest",
16+
default="README.md",
1517
)
1618
parser.add_argument(
17-
'--sort-inputs-by-required', dest='sort', action='store_true',
18-
help='[deprecated] use --sort-by-required instead',
19+
"--sort-inputs-by-required",
20+
dest="sort",
21+
action="store_true",
22+
help="[deprecated] use --sort-by-required instead",
1923
)
2024
parser.add_argument(
21-
'--sort-by-required', dest='sort', action='store_true',
25+
"--sort-by-required",
26+
dest="sort",
27+
action="store_true",
2228
)
2329
parser.add_argument(
24-
'--with-aggregate-type-defaults', dest='aggregate', action='store_true',
25-
help='[deprecated]',
30+
"--with-aggregate-type-defaults",
31+
dest="aggregate",
32+
action="store_true",
33+
help="[deprecated]",
2634
)
27-
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
35+
parser.add_argument("filenames", nargs="*", help="Filenames to check.")
2836
args = parser.parse_args(argv)
2937

3038
dirs = []
3139
for filename in args.filenames:
32-
if (os.path.realpath(filename) not in dirs and
33-
(filename.endswith(".tf") or filename.endswith(".tfvars"))):
40+
if os.path.realpath(filename) not in dirs and (
41+
filename.endswith(".tf")
42+
or filename.endswith(".tofu")
43+
or filename.endswith(".tfvars")
44+
):
3445
dirs.append(os.path.dirname(filename))
3546

3647
retval = 0
3748

3849
for dir in dirs:
3950
try:
4051
procArgs = []
41-
procArgs.append('terraform-docs')
52+
procArgs.append("terraform-docs")
4253
if args.sort:
43-
procArgs.append('--sort-by-required')
44-
procArgs.append('md')
54+
procArgs.append("--sort-by-required")
55+
procArgs.append("md")
4556
procArgs.append("./{dir}".format(dir=dir))
46-
procArgs.append('>')
57+
procArgs.append(">")
4758
procArgs.append("./{dir}/{dest}".format(dir=dir, dest=args.dest))
4859
subprocess.check_call(" ".join(procArgs), shell=True)
4960
except subprocess.CalledProcessError as e:
@@ -52,5 +63,5 @@ def main(argv=None):
5263
return retval
5364

5465

55-
if __name__ == '__main__':
66+
if __name__ == "__main__":
5667
sys.exit(main())

hooks/tofu_wrapper_module_for_each.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,10 @@ EOF
312312

313313
# Read content of all OpenTofu files
314314
# shellcheck disable=SC2207
315-
all_tf_content=$(find "${full_module_dir}" -name '*.tf' -maxdepth 1 -type f -exec cat {} +)
315+
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 files."
318+
common::colorify "yellow" "Skipping ${full_module_dir} because there are no *.(tf|tofu) files."
319319
continue
320320
fi
321321

0 commit comments

Comments
 (0)