mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
feat: Make terraform_validate to run init if necessary (#158)
This commit is contained in:
parent
84374f64a0
commit
d303bff1f9
5 changed files with 40 additions and 27 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
repos:
|
repos:
|
||||||
- repo: git://github.com/pre-commit/pre-commit-hooks
|
- repo: git://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v3.2.0
|
rev: v3.3.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
|
|
|
||||||
10
README.md
10
README.md
|
|
@ -195,13 +195,21 @@ if they are present in `README.md`.
|
||||||
- '--envs=AWS_SECRET_ACCESS_KEY="asecretkey"'
|
- '--envs=AWS_SECRET_ACCESS_KEY="asecretkey"'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
find . -type d -name ".terraform" -print0 | xargs -0 rm -r
|
||||||
|
```
|
||||||
|
|
||||||
|
`terraform_validate` hook will try to reinitialize them before running `terraform validate` command.
|
||||||
|
|
||||||
## Notes for developers
|
## Notes for developers
|
||||||
|
|
||||||
1. Python hooks are supported now too. All you have to do is:
|
1. Python hooks are supported now too. All you have to do is:
|
||||||
1. add a line to the `console_scripts` array in `entry_points` in `setup.py`
|
1. add a line to the `console_scripts` array in `entry_points` in `setup.py`
|
||||||
1. Put your python script in the `pre_commit_hooks` folder
|
1. Put your python script in the `pre_commit_hooks` folder
|
||||||
|
|
||||||
Enjoy the clean and documented code!
|
Enjoy the clean, valid, and documented code!
|
||||||
|
|
||||||
## Authors
|
## Authors
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -311,7 +311,7 @@ EOF
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# global arrays
|
# global arrays
|
||||||
declare -a ARGS=()
|
declare -a ARGS=()
|
||||||
declare -a FILES=()
|
declare -a FILES=()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ parse_cmdline_() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# global arrays
|
# global arrays
|
||||||
declare -a ARGS=()
|
declare -a ARGS=()
|
||||||
declare -a FILES=()
|
declare -a FILES=()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
|
# `terraform validate` requires this env variable to be set
|
||||||
|
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
initialize_
|
initialize_
|
||||||
parse_cmdline_ "$@"
|
parse_cmdline_ "$@"
|
||||||
|
|
@ -80,34 +83,36 @@ terraform_validate_() {
|
||||||
|
|
||||||
if [[ -n "$(find "$path_uniq" -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
|
if [[ -n "$(find "$path_uniq" -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
|
||||||
|
|
||||||
local starting_path
|
pushd "$(realpath "$path_uniq")" > /dev/null
|
||||||
starting_path=$(realpath "$path_uniq")
|
|
||||||
local terraform_path
|
|
||||||
terraform_path="$path_uniq"
|
|
||||||
|
|
||||||
# Find the relevant .terraform directory (indicating a 'terraform init'),
|
if [[ ! -d .terraform ]]; then
|
||||||
# but fall through to the current directory.
|
set +e
|
||||||
while [[ $terraform_path != "." ]]; do
|
init_output=$(terraform init -backend=false 2>&1)
|
||||||
if [[ -d $terraform_path/.terraform ]]; then
|
init_code=$?
|
||||||
break
|
set -e
|
||||||
else
|
|
||||||
terraform_path=$(dirname "$terraform_path")
|
if [[ $init_code != 0 ]]; then
|
||||||
|
error=1
|
||||||
|
echo "Init before validation failed: $path_uniq"
|
||||||
|
echo "$init_output"
|
||||||
|
popd > /dev/null
|
||||||
|
continue
|
||||||
fi
|
fi
|
||||||
done
|
|
||||||
|
|
||||||
local validate_path
|
|
||||||
validate_path="${path_uniq#"$terraform_path"}"
|
|
||||||
|
|
||||||
# Change to the directory that has been initialized, run validation, then
|
|
||||||
# change back to the starting directory.
|
|
||||||
cd "$(realpath "$terraform_path")"
|
|
||||||
if ! terraform validate "${ARGS[@]}" "$validate_path"; then
|
|
||||||
error=1
|
|
||||||
echo
|
|
||||||
echo "Failed path: $path_uniq"
|
|
||||||
echo "================================"
|
|
||||||
fi
|
fi
|
||||||
cd "$starting_path"
|
|
||||||
|
set +e
|
||||||
|
validate_output=$(terraform validate "${ARGS[@]}" 2>&1)
|
||||||
|
validate_code=$?
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [[ $validate_code != 0 ]]; then
|
||||||
|
error=1
|
||||||
|
echo "Validation failed: $path_uniq"
|
||||||
|
echo "$validate_output"
|
||||||
|
echo
|
||||||
|
fi
|
||||||
|
|
||||||
|
popd > /dev/null
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
@ -116,7 +121,7 @@ terraform_validate_() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# global arrays
|
# global arrays
|
||||||
declare -a ARGS
|
declare -a ARGS
|
||||||
declare -a ENVS
|
declare -a ENVS
|
||||||
declare -a FILES
|
declare -a FILES
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue