From 156e11efb63af041bc3692d096c6b71a59ec03e0 Mon Sep 17 00:00:00 2001 From: Andy Chen Date: Mon, 24 Jun 2024 11:19:22 -0700 Subject: [PATCH] feat(hooks): check component is in maint.tf within a directory ending with -component --- hooks/component-in-component-files/check | 26 ++++++++++++------- .../{foo-component => bad-folder}/main.tf | 0 .../fixtures/good-component/main.tf | 7 +++++ hooks/component-in-component-files/test | 13 +++++++--- 4 files changed, 32 insertions(+), 14 deletions(-) rename hooks/component-in-component-files/fixtures/{foo-component => bad-folder}/main.tf (100%) create mode 100644 hooks/component-in-component-files/fixtures/good-component/main.tf diff --git a/hooks/component-in-component-files/check b/hooks/component-in-component-files/check index 0c49916..81abe44 100755 --- a/hooks/component-in-component-files/check +++ b/hooks/component-in-component-files/check @@ -1,4 +1,5 @@ #!/bin/bash -e +# shellcheck disable=SC2094 # Function to check if module names follow the 'component_' convention check_component_module_id() { @@ -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 @@ -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 @@ -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 @@ -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: '.tf'" + echo "components defined in the files that do not match our naming convention: '.tf' or 'main.tf' within a directory ending with '-component'." echo "See: https://open-turo.github.io/standards-terraform/modules/component/" fi diff --git a/hooks/component-in-component-files/fixtures/foo-component/main.tf b/hooks/component-in-component-files/fixtures/bad-folder/main.tf similarity index 100% rename from hooks/component-in-component-files/fixtures/foo-component/main.tf rename to hooks/component-in-component-files/fixtures/bad-folder/main.tf diff --git a/hooks/component-in-component-files/fixtures/good-component/main.tf b/hooks/component-in-component-files/fixtures/good-component/main.tf new file mode 100644 index 0000000..c4aee84 --- /dev/null +++ b/hooks/component-in-component-files/fixtures/good-component/main.tf @@ -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 +} diff --git a/hooks/component-in-component-files/test b/hooks/component-in-component-files/test index 4a507f3..cc5ad2e 100755 --- a/hooks/component-in-component-files/test +++ b/hooks/component-in-component-files/test @@ -8,10 +8,16 @@ 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 @@ -19,7 +25,6 @@ if "$script_directory/check" "$script_directory/fixtures/bad-foo-bar.tf"; then 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