From eed0dde774c1ec7488b1dde46b94e2cd2b185cc2 Mon Sep 17 00:00:00 2001 From: Adam Bovill Date: Tue, 16 May 2023 01:54:02 +0000 Subject: [PATCH] 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`. --- .pre-commit-hooks.yaml | 11 +++++++++ hooks/vars-in-variables-files/check | 23 +++++++++++++++++++ hooks/vars-in-variables-files/fixtures/bad.tf | 4 ++++ .../fixtures/variables.function.tf | 4 ++++ hooks/vars-in-variables-files/test | 19 +++++++++++++++ script/test | 5 ++++ 6 files changed, 66 insertions(+) create mode 100644 .pre-commit-hooks.yaml create mode 100755 hooks/vars-in-variables-files/check create mode 100644 hooks/vars-in-variables-files/fixtures/bad.tf create mode 100644 hooks/vars-in-variables-files/fixtures/variables.function.tf create mode 100755 hooks/vars-in-variables-files/test create mode 100755 script/test diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..98937a2 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -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$ + ###################### diff --git a/hooks/vars-in-variables-files/check b/hooks/vars-in-variables-files/check new file mode 100755 index 0000000..73cfb47 --- /dev/null +++ b/hooks/vars-in-variables-files/check @@ -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 diff --git a/hooks/vars-in-variables-files/fixtures/bad.tf b/hooks/vars-in-variables-files/fixtures/bad.tf new file mode 100644 index 0000000..7bed8c5 --- /dev/null +++ b/hooks/vars-in-variables-files/fixtures/bad.tf @@ -0,0 +1,4 @@ +variable "should_fail" { + default = "true" + description = "This should fail because it isn't in file that starts with variables.tf" +} diff --git a/hooks/vars-in-variables-files/fixtures/variables.function.tf b/hooks/vars-in-variables-files/fixtures/variables.function.tf new file mode 100644 index 0000000..e1da2d7 --- /dev/null +++ b/hooks/vars-in-variables-files/fixtures/variables.function.tf @@ -0,0 +1,4 @@ +variable "test" { + default = "test" + description = "Simple variable" +} diff --git a/hooks/vars-in-variables-files/test b/hooks/vars-in-variables-files/test new file mode 100755 index 0000000..70db197 --- /dev/null +++ b/hooks/vars-in-variables-files/test @@ -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" diff --git a/script/test b/script/test new file mode 100755 index 0000000..003068a --- /dev/null +++ b/script/test @@ -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"