Skip to content

Commit d303bff

Browse files
authored
feat: Make terraform_validate to run init if necessary (antonbabenko#158)
1 parent 84374f6 commit d303bff

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: git://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.2.0
3+
rev: v3.3.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,21 @@ if they are present in `README.md`.
195195
- '--envs=AWS_SECRET_ACCESS_KEY="asecretkey"'
196196
```
197197

198+
1. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc). To solve this problem you can find and delete all `.terraform` directories in your repository using this command:
199+
200+
```shell
201+
find . -type d -name ".terraform" -print0 | xargs -0 rm -r
202+
```
203+
204+
`terraform_validate` hook will try to reinitialize them before running `terraform validate` command.
205+
198206
## Notes for developers
199207

200208
1. Python hooks are supported now too. All you have to do is:
201209
1. add a line to the `console_scripts` array in `entry_points` in `setup.py`
202210
1. Put your python script in the `pre_commit_hooks` folder
203211

204-
Enjoy the clean and documented code!
212+
Enjoy the clean, valid, and documented code!
205213

206214
## Authors
207215

terraform_docs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ EOF
311311

312312
}
313313

314-
# global arrays
314+
# global arrays
315315
declare -a ARGS=()
316316
declare -a FILES=()
317317

terraform_tfsec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ parse_cmdline_() {
6666
done
6767
}
6868

69-
# global arrays
69+
# global arrays
7070
declare -a ARGS=()
7171
declare -a FILES=()
7272

terraform_validate.sh

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env bash
22
set -eo pipefail
33

4+
# `terraform validate` requires this env variable to be set
5+
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
6+
47
main() {
58
initialize_
69
parse_cmdline_ "$@"
@@ -80,34 +83,36 @@ terraform_validate_() {
8083

8184
if [[ -n "$(find "$path_uniq" -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
8285

83-
local starting_path
84-
starting_path=$(realpath "$path_uniq")
85-
local terraform_path
86-
terraform_path="$path_uniq"
87-
88-
# Find the relevant .terraform directory (indicating a 'terraform init'),
89-
# but fall through to the current directory.
90-
while [[ $terraform_path != "." ]]; do
91-
if [[ -d $terraform_path/.terraform ]]; then
92-
break
93-
else
94-
terraform_path=$(dirname "$terraform_path")
86+
pushd "$(realpath "$path_uniq")" > /dev/null
87+
88+
if [[ ! -d .terraform ]]; then
89+
set +e
90+
init_output=$(terraform init -backend=false 2>&1)
91+
init_code=$?
92+
set -e
93+
94+
if [[ $init_code != 0 ]]; then
95+
error=1
96+
echo "Init before validation failed: $path_uniq"
97+
echo "$init_output"
98+
popd > /dev/null
99+
continue
95100
fi
96-
done
101+
fi
97102

98-
local validate_path
99-
validate_path="${path_uniq#"$terraform_path"}"
103+
set +e
104+
validate_output=$(terraform validate "${ARGS[@]}" 2>&1)
105+
validate_code=$?
106+
set -e
100107

101-
# Change to the directory that has been initialized, run validation, then
102-
# change back to the starting directory.
103-
cd "$(realpath "$terraform_path")"
104-
if ! terraform validate "${ARGS[@]}" "$validate_path"; then
108+
if [[ $validate_code != 0 ]]; then
105109
error=1
110+
echo "Validation failed: $path_uniq"
111+
echo "$validate_output"
106112
echo
107-
echo "Failed path: $path_uniq"
108-
echo "================================"
109113
fi
110-
cd "$starting_path"
114+
115+
popd > /dev/null
111116
fi
112117
done
113118

@@ -116,7 +121,7 @@ terraform_validate_() {
116121
fi
117122
}
118123

119-
# global arrays
124+
# global arrays
120125
declare -a ARGS
121126
declare -a ENVS
122127
declare -a FILES

0 commit comments

Comments
 (0)