-
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(pre-commit): add check for outputs in non outputs files
This adds a check to ensure that all outputs are in files named `outputs[.grouping].tf`.
- Loading branch information
Showing
7 changed files
with
68 additions
and
5 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
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
###################### | ||
# 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$ | ||
###################### | ||
|
||
- id: outputs-in-outputs-files | ||
name: "Ensure that all outputs are defined in files that have the following name format: `outputs[.grouping].tf`" | ||
description: "Errors if a outputs is defined in a file that does not start with `outputs.`" | ||
entry: hooks/outputs-in-outputs-files/check | ||
language: script | ||
files: \.tf$ | ||
exclude: ^outputs\..*\.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,24 @@ | ||
#!/bin/bash -e | ||
|
||
check_files() { | ||
has_error=0 | ||
for file in "$@"; do | ||
|
||
# ignore files that start with outputs. | ||
if [[ "$file" == */outputs.*tf ]]; then | ||
echo "outputs file ok: $file" | ||
# grep the file to see if it has a line that start with 'outputs "' | ||
elif grep -q "^output \"" "$file"; then | ||
echo "ERROR: $file MUST not contain outputs" | ||
has_error=1 | ||
fi | ||
done | ||
return $has_error | ||
} | ||
|
||
if ! check_files "$@"; then | ||
echo "outputs defined in files that do not match our naming convention: 'outputs[.grouping].tf'" | ||
echo "See: https://open-turo.github.io/standards-terraform/modules/outputs/" | ||
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 @@ | ||
output "should_fail" { | ||
description = "This should fail because it isn't in file that starts with variables.tf" | ||
value = "should fail" | ||
} |
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 @@ | ||
output "test" { | ||
description = "Simple output" | ||
value = "good value" | ||
} |
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/outputs.function.tf" | ||
"$script_directory/check" "$script_directory/fixtures/outputs.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