Skip to content

Commit d2082c7

Browse files
author
Andrew Babichev
committed
Add terraform-validate hook
1 parent 2c34fd8 commit d2082c7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

.pre-commit-hooks.yaml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +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\/.*$
1010

11+
- id: terraform-validate
12+
name: Terraform validate
13+
description: Validates all Terraform configuration files
14+
entry: hooks/terraform-validate.sh
15+
language: script
16+
files: \.tf$
17+
exclude: \.+.terraform\/.*$
18+
require_serial: true
19+
1120
- id: shellcheck
1221
name: Shellcheck Bash Linter
1322
description: Performs linting on bash scripts
@@ -16,7 +25,7 @@
1625

1726
- id: gofmt
1827
name: gofmt
19-
description: Gofmt formats Go programs.
28+
description: Gofmt formats Go programs
2029
entry: hooks/gofmt.sh
2130
language: script
2231
files: \.go$
@@ -32,15 +41,15 @@
3241

3342
- id: golint
3443
name: golint
35-
description: Golint is a linter for Go source code.
44+
description: Golint is a linter for Go source code
3645
entry: hooks/golint.sh
3746
language: script
3847
files: \.go$
3948
exclude: vendor\/.*$
4049

4150
- id: yapf
4251
name: yapf
43-
description: yapf (Yet Another Python Formatter) is a python formatter from Google.
52+
description: yapf (Yet Another Python Formatter) is a python formatter from Google
4453
entry: hooks/yapf.sh
4554
language: script
4655
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-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 "$@" | tr -s " " "\n" | sort -u | xargs -n1 dirname | uniq); do
11+
terraform init -backend=false $dir
12+
terraform validate $dir
13+
done

0 commit comments

Comments
 (0)