-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(variables): pre-commit check to enforce variables in variables file
This adds a check to enforce that variables are all defined in files that have the following format `variables[.grouping].tf`.
- Loading branch information
Showing
6 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
###################### | ||
# Yaml related hooks | ||
# These only have shell dependencies | ||
- id: vars-in-variables-files | ||
name: "Ensure that all variables are defined in files that have the following name format: `variables[.grouping].tf`" | ||
description: "Errors if a variable is defined in a file that does not start with `variables.`" | ||
entry: hooks/vars-in-variables-files/check | ||
language: script | ||
files: \.tf$ | ||
exclude: ^variables\..*\.tf$ | ||
###################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash -e | ||
|
||
check_files() { | ||
has_error=0 | ||
for file in "$@"; do | ||
|
||
# grep the file to see if it has a line that start with 'variable "' | ||
if [[ "$file" == */variables.*tf ]]; then | ||
echo "variables file ok: $file" | ||
elif grep -q "^variable \"" "$file"; then | ||
echo "ERROR: $file MUST not contain variables" | ||
has_error=1 | ||
fi | ||
done | ||
return $has_error | ||
} | ||
|
||
if ! check_files "$@"; then | ||
echo "Variables defined in files that do not match our naming convention: 'variables[.grouping].tf'" | ||
echo "See: https://open-turo.github.io/standards-terraform/modules/input-variables/" | ||
fi | ||
|
||
exit $has_error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "should_fail" { | ||
default = "true" | ||
description = "This should fail because it isn't in file that starts with variables.tf" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "test" { | ||
default = "test" | ||
description = "Simple variable" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash -e | ||
|
||
# get the directory of the script | ||
script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | ||
|
||
echo "testing: $script_directory" | ||
|
||
echo "testing: check $script_directory/fixtures/variables.function.tf" | ||
"$script_directory/check" "$script_directory/fixtures/variables.function.tf" | ||
|
||
# check to see if the next file failed | ||
echo "testing: check $script_directory/fixtures/bad.tf" | ||
echo " expecting error" | ||
if "$script_directory/check" "$script_directory/fixtures/bad.tf"; then | ||
echo "ERROR: should have failed" | ||
exit 1 | ||
fi | ||
|
||
echo "testing: PASS" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash -e | ||
|
||
REPO_DIR="$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)")" | ||
|
||
"$REPO_DIR/hooks/variables/test" |