Skip to content

Commit

Permalink
fix(variables): pre-commit check to enforce variables in variables file
Browse files Browse the repository at this point in the history
This adds a check to enforce that variables are all defined in files that have the following format
`variables[.grouping].tf`.
  • Loading branch information
greenkiwi committed May 17, 2023
1 parent 2b9a184 commit eed0dde
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .pre-commit-hooks.yaml
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$
######################
23 changes: 23 additions & 0 deletions hooks/vars-in-variables-files/check
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
4 changes: 4 additions & 0 deletions hooks/vars-in-variables-files/fixtures/bad.tf
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"
}
4 changes: 4 additions & 0 deletions hooks/vars-in-variables-files/fixtures/variables.function.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "test" {
default = "test"
description = "Simple variable"
}
19 changes: 19 additions & 0 deletions hooks/vars-in-variables-files/test
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"
5 changes: 5 additions & 0 deletions script/test
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"

0 comments on commit eed0dde

Please sign in to comment.