Skip to content

Commit

Permalink
feat(hooks): check component is in maint.tf within a directory ending…
Browse files Browse the repository at this point in the history
… with -component
  • Loading branch information
amazingandyyy committed Jun 24, 2024
1 parent 353c4b2 commit 156e11e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
26 changes: 16 additions & 10 deletions hooks/component-in-component-files/check
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash -e
# shellcheck disable=SC2094

# Function to check if module names follow the 'component_<name>' convention
check_component_module_id() {
Expand All @@ -14,11 +15,19 @@ check_component_module_id() {
check_component_file_name() {
local module_name="$1"
local component_name="$2"
local filename="$3"
local expected_file="${component_name//_/-}.tf"
local file="$3"
local filename
local expected_file
local parent_directory
local parent_directory_name

if [[ "$filename" != "$expected_file" && "$filename" != "main.tf" ]]; then
echo "ERROR: Module '$module_name' with name '$component_name' should be in '$expected_file' or 'main.tf', but found in '$filename'."
filename=$(basename "$file")
expected_file="${component_name//_/-}.tf"
parent_directory=$(dirname "$file")
parent_directory_name=$(basename "$parent_directory")

if [[ "$filename" != "$expected_file" && ("$filename" != "main.tf" || ! "$parent_directory_name" =~ -component$) ]]; then
echo "ERROR: Module '$module_name' with name '$component_name' should be in '$expected_file' or a 'main.tf' within a directory ending with '-component', but found in '$filename'."
return 1
fi
return 0
Expand All @@ -27,11 +36,8 @@ check_component_file_name() {
check_component_files() {
has_error=0
for file in "$@"; do
# Extract the filename from the path
filename=$(basename "$file")

# Check only '.tf' files to avoid processing other file types
if [[ "$filename" == *.tf ]]; then
if [[ "$file" == *.tf ]]; then
# Read through the file line by line to find module blocks and extract the 'name' field
module_name=""
while IFS= read -r line; do
Expand All @@ -43,7 +49,7 @@ check_component_files() {
# When module_name is set, look for the name attribute within the block
if [[ -n "$module_name" && "$line" =~ ^\ *name\ *= ]]; then
component_name=$(echo "$line" | awk -F\" '{print $2}')
check_component_file_name "$module_name" "$component_name" "$filename" || has_error=1
check_component_file_name "$module_name" "$component_name" "$file" || has_error=1
# Reset module_name to ensure correct block processing
module_name=""
fi
Expand All @@ -55,7 +61,7 @@ check_component_files() {

# Execute the check across all .tf files passed as arguments
if ! check_component_files "$@"; then
echo "components defined in thef iles that do not match our naming convention: '<component-name>.tf'"
echo "components defined in the files that do not match our naming convention: '<component-name>.tf' or 'main.tf' within a directory ending with '-component'."
echo "See: https://open-turo.github.io/standards-terraform/modules/component/"
fi

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "component_good_foo_bar" {
source = "app.terraform.io/turo/component-metadata/null"
version = "3.1.2"

name = "good-foo-bar"
system_metadata = var.metadata_module.parent_system_metadata
}
13 changes: 9 additions & 4 deletions hooks/component-in-component-files/test
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ echo "testing: $script_directory"
echo "testing: check $script_directory/fixtures/good-foo-bar.tf"
"$script_directory/check" "$script_directory/fixtures/good-foo-bar.tf"

echo "testing: check $script_directory/fixtures/foo-component/main.tf"
"$script_directory/check" "$script_directory/fixtures/foo-component/main.tf"
echo "testing: check $script_directory/fixtures/good-component/main.tf"
"$script_directory/check" "$script_directory/fixtures/good-component/main.tf"

echo "testing: check $script_directory/fixtures/bad-folder/main.tf"
echo " expecting error"
if "$script_directory/check" "$script_directory/fixtures/bad-folder/main.tf"; then
echo "ERROR: should have failed"
exit 1
fi

# check to see if the next file failed
echo "testing: check $script_directory/fixtures/bad-foo-bar.tf"
echo " expecting error"
if "$script_directory/check" "$script_directory/fixtures/bad-foo-bar.tf"; then
echo "ERROR: should have failed"
exit 1
fi

# check to see if the next file failed
echo "testing: check $script_directory/fixtures/bad-component.tf"
echo " expecting error"
if "$script_directory/check" "$script_directory/fixtures/bad-component.tf"; then
Expand Down

0 comments on commit 156e11e

Please sign in to comment.