Skip to content

Commit 59fd861

Browse files
authored
feat: Adding tofu hooks (#129)
* feat: Adding tofu hooks * feat: Updating README.md * fix: Updating `.pre-commit-hooks.yaml`
1 parent d9196b3 commit 59fd861

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

.pre-commit-hooks.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# This configuration file allows our pre-commit hooks to be used with pre-commit: http://pre-commit.com/
22

3+
- id: tofu-fmt
4+
name: OpenTofu fmt
5+
description: Rewrites all OpenTofu configuration files to a canonical format
6+
entry: hooks/tofu-fmt.sh
7+
language: script
8+
files: \.(tf|tofu)$
9+
exclude: \.terraform\/.*$
10+
require_serial: true
11+
12+
- id: tofu-validate
13+
name: OpenTofu validate
14+
description: Validates all OpenTofu configuration files
15+
entry: hooks/tofu-validate.sh
16+
language: script
17+
files: \.(tf|tofu)$
18+
exclude: \.terraform\/.*$
19+
require_serial: true
20+
321
- id: terraform-fmt
422
name: Terraform fmt
523
description: Rewrites all Terraform configuration files to a canonical format

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
This repo defines Git pre-commit hooks intended for use with [pre-commit](http://pre-commit.com/). The currently
66
supported hooks are:
77

8+
* **tofu-fmt**: Automatically run `tofu fmt` on all OpenTofu code (`*.tf`, `*.tofu` files).
9+
* **tofu-validate**: Automatically run `tofu validate` on all OpenTofu code (`*.tf`, `*.tofu` files).
810
* **terraform-fmt**: Automatically run `terraform fmt` on all Terraform code (`*.tf` files).
911
* **terraform-validate**: Automatically run `terraform validate` on all Terraform code (`*.tf` files).
1012
* **packer-validate**: Automatically run `packer validate` on all Packer code (`*.pkr.*` files).
1113
* **terragrunt-hclfmt**: Automatically run `terragrunt hclfmt` on all Terragrunt configurations.
12-
* **tflint**: Automatically run [`tflint`](https://github.com/terraform-linters/tflint) on all Terraform code (`*.tf` files).
14+
* **tflint**: Automatically run [`tflint`](https://github.com/terraform-linters/tflint) on all OpenTofu/Terraform code (`*.tf`, `*.tofu` files).
1315
* **shellcheck**: Run [`shellcheck`](https://www.shellcheck.net/) to lint files that contain a bash [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)).
1416
* **gofmt**: Automatically run `gofmt` on all Golang code (`*.go` files).
1517
* **goimports**: Automatically run `goimports` on all Golang code (`*.go` files).
@@ -34,8 +36,8 @@ repos:
3436
- repo: https://github.com/gruntwork-io/pre-commit
3537
rev: <VERSION> # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases
3638
hooks:
37-
- id: terraform-fmt
38-
- id: terraform-validate
39+
- id: tofu-fmt
40+
- id: tofu-validate
3941
- id: tflint
4042
- id: shellcheck
4143
- id: gofmt
@@ -60,7 +62,7 @@ That’s it! Now every time you commit a code change (`.tf` file), the hooks in
6062
If you'd like to format all of your code at once (rather than one file at a time), you can run:
6163

6264
```bash
63-
pre-commit run terraform-fmt --all-files
65+
pre-commit run tofu-fmt --all-files
6466
```
6567

6668

hooks/tofu-fmt.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions,
6+
# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a
7+
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
8+
original_path=$PATH
9+
export PATH=$PATH:/usr/local/bin
10+
11+
# Store and return last failure from fmt so this can validate every directory passed before exiting
12+
FMT_ERROR=0
13+
14+
for file in "$@"; do
15+
tofu fmt -diff -check "$file" || FMT_ERROR=$?
16+
done
17+
18+
# reset path to the original value
19+
export PATH=$original_path
20+
21+
exit ${FMT_ERROR}

hooks/tofu-validate.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions,
6+
# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a
7+
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
8+
export PATH=$PATH:/usr/local/bin
9+
10+
# Disable output not usually helpful when running in automation (such as guidance to run plan after init)
11+
export TF_IN_AUTOMATION=1
12+
13+
# Store and return last failure from validate so this can validate every directory passed before exiting
14+
VALIDATE_ERROR=0
15+
16+
for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do
17+
echo "--> Running 'tofu validate' in directory '$dir'"
18+
pushd "$dir" >/dev/null
19+
tofu init -backend=false || VALIDATE_ERROR=$?
20+
tofu validate || VALIDATE_ERROR=$?
21+
popd >/dev/null
22+
done
23+
24+
exit ${VALIDATE_ERROR}

0 commit comments

Comments
 (0)