Skip to content

Commit 40f924c

Browse files
authored
feat: Adding terragrunt hcl fmt script (#134)
* feat: Adding `terragrunt hcl fmt` script * fix: Fixing `.pre-commit-hooks.yaml` * fix: Updating hook logic
1 parent e93068a commit 40f924c

File tree

3 files changed

+66
-18
lines changed

3 files changed

+66
-18
lines changed

.pre-commit-hooks.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@
6464
\.terragrunt-cache\/.*|
6565
)$
6666
67+
- id: terragrunt-hcl-fmt
68+
name: Terragrunt hcl fmt
69+
description: Rewrites all Terragrunt configuration files to a canonical format
70+
entry: hooks/terragrunt-hcl-fmt.sh
71+
language: script
72+
files: \.hcl$
73+
exclude: >
74+
(?x)^(
75+
\.terraform\/.*|
76+
\.terragrunt-cache\/.*|
77+
)$
78+
6779
- id: shellcheck
6880
name: Shellcheck Bash Linter
6981
description: Performs linting on bash scripts
@@ -99,7 +111,6 @@
99111
description: golangci-lint is a Go linters aggregator
100112
entry: hooks/golangci-lint.sh
101113
language: script
102-
language: script
103114
files: \.go$
104115
require_serial: true
105116

README.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ supported hooks are:
1010
* **terraform-fmt**: Automatically run `terraform fmt` on all Terraform code (`*.tf` files).
1111
* **terraform-validate**: Automatically run `terraform validate` on all Terraform code (`*.tf` files).
1212
* **packer-validate**: Automatically run `packer validate` on all Packer code (`*.pkr.*` files).
13-
* **terragrunt-hclfmt**: Automatically run `terragrunt hclfmt` on all Terragrunt configurations.
13+
* **terragrunt-hclfmt**: Automatically run `terragrunt hclfmt` on all Terragrunt configurations (deprecated, use `terragrunt-hcl-fmt` instead if you are using Terragrunt 0.77.22 or later).
14+
* **terragrunt-hcl-fmt**: Automatically run `terragrunt hcl fmt` on all Terragrunt configurations (requires Terragrunt 0.77.22 or later).
1415
* **tflint**: Automatically run [`tflint`](https://github.com/terraform-linters/tflint) on all OpenTofu/Terraform code (`*.tf`, `*.tofu` files).
1516
* **shellcheck**: Run [`shellcheck`](https://www.shellcheck.net/) to lint files that contain a bash [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)).
1617
* **gofmt**: Automatically run `gofmt` on all Golang code (`*.go` files).
@@ -23,10 +24,6 @@ supported hooks are:
2324
markdown doc files.
2425
* **sentinel-fmt**: Automatically run `sentinel fmt` on all Sentinel code (`*.sentinel.*` files).
2526

26-
27-
28-
29-
3027
## General Usage
3128

3229
In each of your repos, add a file called `.pre-commit-config.yaml` with the following contents:
@@ -44,19 +41,15 @@ repos:
4441
- id: golint
4542
```
4643
47-
Next, have every developer: 
44+
Next, have every developer:
4845
4946
1. Install [pre-commit](http://pre-commit.com/). E.g. `brew install pre-commit`.
5047
1. Run `pre-commit install` in the repo.
5148

5249
That’s it! Now every time you commit a code change (`.tf` file), the hooks in the `hooks:` config will execute.
5350

54-
55-
56-
5751
## Running Against All Files At Once
5852

59-
6053
### Example: Formatting all files
6154

6255
If you'd like to format all of your code at once (rather than one file at a time), you can run:
@@ -65,8 +58,6 @@ If you'd like to format all of your code at once (rather than one file at a time
6558
pre-commit run tofu-fmt --all-files
6659
```
6760

68-
69-
7061
### Example: Enforcing in CI
7162

7263
If you'd like to enforce all your hooks, you can configure your CI build to fail if the code doesn't pass checks by
@@ -81,9 +72,6 @@ pre-commit run --all-files
8172
If all the hooks pass, the last command will exit with an exit code of 0. If any of the hooks make changes (e.g.,
8273
because files are not formatted), the last command will exit with a code of 1, causing the build to fail.
8374

84-
85-
86-
8775
## Helm Lint Caveats
8876

8977
### Detecting charts
@@ -129,7 +117,7 @@ containerImage: nginx
129117

130118
Now when the pre-commit hook runs, it will call `helm lint` with both `linter_values.yaml` and `values.yaml`:
131119

132-
```
120+
```bash
133121
helm lint -f values.yaml -f linter_values.yaml .
134122
```
135123

@@ -153,7 +141,7 @@ repos:
153141

154142
With the introduction of `--chdir` into tflint, the `--config` argument is now bound to whatever subdirectory you are
155143
running the check against. For mono-repos this isn't ideal as you may have a central configuration file you'd like to
156-
use. If this matches your use-case, you can specify the placeholder `__GIT_ROOT__` value in the `--config` argument
144+
use. If this matches your use-case, you can specify the placeholder `__GIT_ROOT__` value in the `--config` argument
157145
that will evaluate to the root of the repository you are in.
158146

159147
```yaml

hooks/terragrunt-hcl-fmt.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
check_terragrunt_version() {
11+
local minimum_supported_version="0.77.22"
12+
local current_version
13+
14+
if ! command -v terragrunt >/dev/null 2>&1; then
15+
echo "Warning: terragrunt command not found. Proceeding anyway..." >&2
16+
17+
return 0
18+
fi
19+
20+
if ! current_version=$(terragrunt --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1); then
21+
echo "Warning: Could not determine terragrunt version. Proceeding anyway..." >&2
22+
23+
return 0
24+
fi
25+
26+
if [ "$(printf '%s\n' "$minimum_supported_version" "$current_version" | sort -V | head -1)" = "$minimum_supported_version" ]; then
27+
return 0
28+
fi
29+
30+
echo "Error: Terragrunt version $current_version is less than the minimum supported version $minimum_supported_version" >&2
31+
echo "Please upgrade Terragrunt to version $minimum_supported_version or later" >&2
32+
33+
exit 1
34+
}
35+
36+
format_files() {
37+
for file in "$@"; do
38+
pushd "$(dirname "$file")" >/dev/null
39+
terragrunt hcl fmt --file "$(basename "$file")"
40+
popd >/dev/null
41+
done
42+
}
43+
44+
main() {
45+
check_terragrunt_version
46+
format_files "$@"
47+
}
48+
49+
main "$@"

0 commit comments

Comments
 (0)