Skip to content

Commit f2254d3

Browse files
committed
Check for inline provider for terraform validate (fix)
1 parent 40c0423 commit f2254d3

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

hooks/terraform-validate.sh

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env bash
22

3-
set -e
4-
53
# This is a clone of https://github.com/gruntwork-io/pre-commit/blob/master/hooks/terraform-validate.sh
64
# It adds support for modules that use the Azure Resource Manager provider 2.0+.
75
# This provider requires an explicit `features` block which in most cases is supplied by a super module.
@@ -15,19 +13,27 @@ function main() {
1513
fi
1614

1715
for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do
18-
pushd "$dir" >/dev/null
19-
if [ -n "$azurerm_provider_version" ]; then
20-
cat << EOF > provider.tf
16+
local -r inline_provider_match="$(grep 'provider "azurerm" {' "$dir/main.tf")"
17+
if [[ -z "$inline_provider_match" ]]; then
18+
continue
19+
fi
20+
21+
pushd "$dir" >/dev/null || exit 1
22+
if [ -n "$azurerm_provider_version" ]; then
23+
cat << EOF > provider.tf
2124
provider "azurerm" {
2225
version = "$azurerm_provider_version"
2326
features {}
2427
}
2528
EOF
26-
fi
27-
terraform init -backend=false
28-
terraform validate
29-
rm provider.tf
30-
popd >/dev/null
29+
fi
30+
terraform init -backend=false
31+
terraform validate
32+
local -r result="$?"
33+
rm provider.tf
34+
# TODO: Make sure the provider.tf gets removed.
35+
popd >/dev/null || exit 1
36+
exit "$result"
3137
done
3238
}
3339

0 commit comments

Comments
 (0)