From ab5f7ac6e87d2c9c54e83fbcc1cccb3f598168cd Mon Sep 17 00:00:00 2001 From: Steve Swor <1486524+sworisbreathing@users.noreply.github.com> Date: Fri, 16 Aug 2024 13:13:15 +1000 Subject: [PATCH 1/5] fix(terraform_docs): force rendered documentation to be written to stdout (#697) --- hooks/terraform_docs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index 9fe0de440..e88e3abc8 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -228,7 +228,7 @@ function terraform_docs { if [[ "$terraform_docs_awk_file" == "0" ]]; then # shellcheck disable=SC2086 - terraform-docs $tf_docs_formatter $args ./ > "$tmp_file" + terraform-docs --output-file="" $tf_docs_formatter $args ./ > "$tmp_file" else # Can't append extension for mktemp, so renaming instead local tmp_file_docs @@ -239,7 +239,7 @@ function terraform_docs { awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf" # shellcheck disable=SC2086 - terraform-docs $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file" + terraform-docs --output-file="" $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file" rm -f "$tmp_file_docs_tf" fi From c6ef954392af9477e3a9f43cab1aace67575068d Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 16 Aug 2024 12:59:41 +0300 Subject: [PATCH 2/5] Prioritize terraform-docs config --- hooks/terraform_docs.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index e88e3abc8..2ede4c4b5 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -159,13 +159,22 @@ function terraform_docs { if [[ "$args" != *"--config"* ]]; then local tf_docs_formatter="md" - # Suppress terraform_docs color else local config_file=${args#*--config} config_file=${config_file#*=} config_file=${config_file% *} + # Prioritize `.terraform-docs.yml` `output.file` over + # `--hook-config=--path-to-file=` if it set + local output_file + output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep ' file:' | + awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'") + if [ "$output_file" ]; then + text_file=$output_file + fi + + # Suppress terraform_docs color local config_file_no_color config_file_no_color="$config_file$(date +%s).yml" From 5240d5374596f7fb6645dda7e1966cf0ee10e857 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 16 Aug 2024 13:19:01 +0300 Subject: [PATCH 3/5] Catch edge cases --- hooks/terraform_docs.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index 2ede4c4b5..5538178c0 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -168,10 +168,12 @@ function terraform_docs { # Prioritize `.terraform-docs.yml` `output.file` over # `--hook-config=--path-to-file=` if it set local output_file - output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep ' file:' | - awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'") + # Get latest non-commented `output.file` from `.terraform-docs.yml` + output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep ' file:' | grep -v '#' || true) + if [ "$output_file" ]; then - text_file=$output_file + # Extract filename from `output.file` line + text_file=$(echo "$output_file" | awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'") fi # Suppress terraform_docs color From 5f136ea63e469e8d7147cc32da5ff4cc84dec8f0 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 16 Aug 2024 13:22:06 +0300 Subject: [PATCH 4/5] f --- hooks/terraform_docs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index 5538178c0..6cc69199c 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -169,7 +169,7 @@ function terraform_docs { # `--hook-config=--path-to-file=` if it set local output_file # Get latest non-commented `output.file` from `.terraform-docs.yml` - output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep ' file:' | grep -v '#' || true) + output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep ' file:' | grep -v '#' | tail -n 1 || true) if [ "$output_file" ]; then # Extract filename from `output.file` line From bff1191d93cde8ed5b1a783da6c334e17fb87815 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 16 Aug 2024 17:56:44 +0300 Subject: [PATCH 5/5] Apply review suggestions --- hooks/terraform_docs.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index 6cc69199c..657853983 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -121,6 +121,7 @@ function terraform_docs { # Get hook settings # local text_file="README.md" + local use_path_to_file=false local add_to_existing=false local create_if_not_exist=false local use_standard_markers=false @@ -136,6 +137,7 @@ function terraform_docs { case $key in --path-to-file) text_file=$value + use_path_to_file=true ;; --add-to-existing-file) add_to_existing=$value @@ -169,11 +171,16 @@ function terraform_docs { # `--hook-config=--path-to-file=` if it set local output_file # Get latest non-commented `output.file` from `.terraform-docs.yml` - output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep ' file:' | grep -v '#' | tail -n 1 || true) + output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+file:' | tail -n 1) || true - if [ "$output_file" ]; then + if [[ $output_file ]]; then # Extract filename from `output.file` line text_file=$(echo "$output_file" | awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'") + + if [[ $use_path_to_file ]]; then + common::colorify "yellow" "NOTE: You set both '--hook-config=--path-to-file=' and 'output.file' in '$config_file'" + common::colorify "yellow" " 'output.file' from '$config_file' will be used." + fi fi # Suppress terraform_docs color