Skip to content

Commit

Permalink
fix(pre-commit): add check for outputs in non outputs files
Browse files Browse the repository at this point in the history
This adds a check to ensure that all outputs are in files named
`outputs[.grouping].tf`.
  • Loading branch information
greenkiwi committed May 17, 2023
1 parent eed0dde commit b84d45a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ concurrency:
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- run: script/test

# Build job
build:
runs-on: ubuntu-latest
Expand Down
12 changes: 8 additions & 4 deletions .pre-commit-hooks.yaml
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$
24 changes: 24 additions & 0 deletions hooks/outputs-in-outputs-files/check
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
4 changes: 4 additions & 0 deletions hooks/outputs-in-outputs-files/fixtures/bad.tf
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"
}
4 changes: 4 additions & 0 deletions hooks/outputs-in-outputs-files/fixtures/outputs.function.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "test" {
description = "Simple output"
value = "good value"
}
19 changes: 19 additions & 0 deletions hooks/outputs-in-outputs-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/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"
3 changes: 2 additions & 1 deletion script/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

REPO_DIR="$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)")"

"$REPO_DIR/hooks/variables/test"
"$REPO_DIR/hooks/outputs-in-outputs-files/test"
"$REPO_DIR/hooks/vars-in-variables-files/test"

0 comments on commit b84d45a

Please sign in to comment.