Skip to content

Commit db47313

Browse files
authored
Merge pull request #15 from Tensho/terraform-validate
Add "terraform-validate" Hook
2 parents 2c34fd8 + 2d51e3a commit db47313

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

.pre-commit-hooks.yaml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
- id: terraform-fmt
44
name: Terraform fmt
5-
description: Rewrites all Terraform configuration files to a canonical format.
5+
description: Rewrites all Terraform configuration files to a canonical format
66
entry: hooks/terraform-fmt.sh
77
language: script
88
files: \.tf$
99
exclude: \.+.terraform\/.*$
10+
require_serial: true
11+
12+
- id: terraform-validate
13+
name: Terraform validate
14+
description: Validates all Terraform configuration files
15+
entry: hooks/terraform-validate.sh
16+
language: script
17+
files: \.tf$
18+
exclude: \.+.terraform\/.*$
19+
require_serial: true
1020

1121
- id: shellcheck
1222
name: Shellcheck Bash Linter
@@ -16,7 +26,7 @@
1626

1727
- id: gofmt
1828
name: gofmt
19-
description: Gofmt formats Go programs.
29+
description: Gofmt formats Go programs
2030
entry: hooks/gofmt.sh
2131
language: script
2232
files: \.go$
@@ -32,15 +42,15 @@
3242

3343
- id: golint
3444
name: golint
35-
description: Golint is a linter for Go source code.
45+
description: Golint is a linter for Go source code
3646
entry: hooks/golint.sh
3747
language: script
3848
files: \.go$
3949
exclude: vendor\/.*$
4050

4151
- id: yapf
4252
name: yapf
43-
description: yapf (Yet Another Python Formatter) is a python formatter from Google.
53+
description: yapf (Yet Another Python Formatter) is a python formatter from Google
4454
entry: hooks/yapf.sh
4555
language: script
4656
files: \.py$

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This repo defines Git pre-commit hooks intended for use with [pre-commit](http:/
66
supported hooks are:
77

88
* **terraform-fmt**: Automatically run `terraform fmt` on all Terraform code (`*.tf` files).
9+
* **terraform-validate**: Automatically run `terraform validate` on all Terraform code (`*.tf` files).
910
* **shellcheck**: Run [`shellcheck`](https://www.shellcheck.net/) to lint files that contain a bash [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix))
1011
* **gofmt**: Automatically run `gofmt` on all Golang code (`*.go` files).
1112
* **golint**: Automatically run `golint` on all Golang code (`*.go` files)
@@ -26,6 +27,7 @@ repos:
2627
rev: <VERSION> # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases
2728
hooks:
2829
- id: terraform-fmt
30+
- id: terraform-validate
2931
- id: shellcheck
3032
- id: gofmt
3133
- id: golint

hooks/terraform-fmt.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ set -e
77
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
88
export PATH=$PATH:/usr/local/bin
99

10-
for file in "$@"; do
11-
terraform fmt `dirname $file`
12-
done
10+
terraform fmt -recursive

hooks/terraform-validate.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/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+
for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do
11+
terraform init -backend=false $dir
12+
terraform validate $dir
13+
done

0 commit comments

Comments
 (0)