From a12f76fe9fc30a12a9baf1ea00bfc37684d5cb4d Mon Sep 17 00:00:00 2001
From: MaxymVlasov <MaxymVlasov@users.noreply.github.com>
Date: Mon, 9 Sep 2024 22:09:50 +0300
Subject: [PATCH 1/5] fix(`terraform_docs`): 1.94.0 regression. Restore
 `--hook-config=--add-to-existing-file` default behavior

---
 hooks/terraform_docs.sh | 44 ++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 14 deletions(-)

diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh
index 4c402549e..f3135f684 100755
--- a/hooks/terraform_docs.sh
+++ b/hooks/terraform_docs.sh
@@ -260,24 +260,40 @@ function terraform_docs {
 
     replace_old_markers "$output_file"
 
-    #
-    # If `--add-to-existing-file=true` set, check is in file exist "hook markers",
-    # and if not - append "hook markers" to the end of file.
-    #
-    if $add_to_existing; then
-      HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
-
-      if [ ! "$HAVE_MARKER" ]; then
-        # Use of insertion markers, where addToExisting=true, with no markers in the existing file
-        echo "$insertion_marker_begin" >> "$output_file"
-        echo "$insertion_marker_end" >> "$output_file"
-      fi
-    fi
-
     if [[ "$terraform_docs_awk_file" == "0" ]]; then
+      #? TF 0.12+ and terraform-docs 0.12.0+
+
+      #
+      # If `--add-to-existing-file=true` set, check is in file exist "hook markers",
+      # and if not - append "hook markers" to the end of file.
+      #
+      if $add_to_existing; then
+        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
+
+        if [ ! "$HAVE_MARKER" ]; then
+          # terraform-docs in 'inject; mode adds markers by default if they not present.
+          # So, we need need just skip execution to skip addition of terraform-docs section
+          continue
+        fi
+      fi
       # shellcheck disable=SC2086
       terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter $args ./ > /dev/null
+
     else
+      #? TF 0.12+ and terraform-docs < 0.8
+      #? Yes, we don't cover case of TF 0.12+ and terraform-docs 0.8-0.11
+      #? but I portably just drop this section in next release of the hook,
+      #? as there no sense to support hacks for tool versions which was released more than 3 years ago
+
+      if $add_to_existing; then
+        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
+
+        if [ ! "$HAVE_MARKER" ]; then
+          # Use of insertion markers, where addToExisting=true, with no markers in the existing file
+          echo "$insertion_marker_begin" >> "$output_file"
+          echo "$insertion_marker_end" >> "$output_file"
+        fi
+      fi
       # Can't append extension for mktemp, so renaming instead
       local tmp_file_docs
       tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")

From c0b1f38bae3b34f3f223bc73a8b384e2e7a766d7 Mon Sep 17 00:00:00 2001
From: MaxymVlasov <MaxymVlasov@users.noreply.github.com>
Date: Mon, 9 Sep 2024 22:16:57 +0300
Subject: [PATCH 2/5] f

---
 hooks/terraform_docs.sh | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh
index f3135f684..4edab423b 100755
--- a/hooks/terraform_docs.sh
+++ b/hooks/terraform_docs.sh
@@ -264,17 +264,13 @@ function terraform_docs {
       #? TF 0.12+ and terraform-docs 0.12.0+
 
       #
-      # If `--add-to-existing-file=true` set, check is in file exist "hook markers",
-      # and if not - append "hook markers" to the end of file.
+      # If `--add-to-existing-file=false` (default behavior), check is in file exist "hook markers",
+      # and if not skip execution to avoid addition of terraform-docs section -
+      # terraform-docs in 'inject' mode adds markers by default if they not present
       #
-      if $add_to_existing; then
+      if ! $add_to_existing; then
         HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
-
-        if [ ! "$HAVE_MARKER" ]; then
-          # terraform-docs in 'inject; mode adds markers by default if they not present.
-          # So, we need need just skip execution to skip addition of terraform-docs section
-          continue
-        fi
+        [[ ! $HAVE_MARKER ]] && continue
       fi
       # shellcheck disable=SC2086
       terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter $args ./ > /dev/null
@@ -285,6 +281,10 @@ function terraform_docs {
       #? but I portably just drop this section in next release of the hook,
       #? as there no sense to support hacks for tool versions which was released more than 3 years ago
 
+      #
+      # If `--add-to-existing-file=true` set, check is in file exist "hook markers",
+      # and if not - append "hook markers" to the end of file.
+      #
       if $add_to_existing; then
         HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
 

From d5a98e5e5789c8180e98c27895f42f0826a96033 Mon Sep 17 00:00:00 2001
From: Maksym Vlasov <MaxymVlasov@users.noreply.github.com>
Date: Mon, 9 Sep 2024 23:06:01 +0300
Subject: [PATCH 3/5] Apply suggestions from code review

Co-authored-by: George L. Yermulnik <yz@yz.kiev.ua>
---
 hooks/terraform_docs.sh | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh
index 4edab423b..758046908 100755
--- a/hooks/terraform_docs.sh
+++ b/hooks/terraform_docs.sh
@@ -264,11 +264,11 @@ function terraform_docs {
       #? TF 0.12+ and terraform-docs 0.12.0+
 
       #
-      # If `--add-to-existing-file=false` (default behavior), check is in file exist "hook markers",
-      # and if not skip execution to avoid addition of terraform-docs section -
-      # terraform-docs in 'inject' mode adds markers by default if they not present
+      # If `--add-to-existing-file=false` (default behavior), check if "hook markers" exist in file,
+      # and, if not, skip execution to avoid addition of terraform-docs section, as
+      # terraform-docs in 'inject' mode adds markers by default if they are not present
       #
-      if ! $add_to_existing; then
+      if [[ $add_to_existing == false ]]; then
         HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
         [[ ! $HAVE_MARKER ]] && continue
       fi
@@ -278,18 +278,18 @@ function terraform_docs {
     else
       #? TF 0.12+ and terraform-docs < 0.8
       #? Yes, we don't cover case of TF 0.12+ and terraform-docs 0.8-0.11
-      #? but I portably just drop this section in next release of the hook,
-      #? as there no sense to support hacks for tool versions which was released more than 3 years ago
+      #? but I probably just drop this section in next release of the hook,
+      #? as there's no sense to support hacks for tool versions which were released more than 3 years ago
 
       #
-      # If `--add-to-existing-file=true` set, check is in file exist "hook markers",
-      # and if not - append "hook markers" to the end of file.
+      # If `--add-to-existing-file=true` set, check if "hook markers" exist in file,
+      # and, if not, append "hook markers" to the end of the file.
       #
-      if $add_to_existing; then
+      if [[ $add_to_existing == true ]]; then
         HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
 
-        if [ ! "$HAVE_MARKER" ]; then
-          # Use of insertion markers, where addToExisting=true, with no markers in the existing file
+        if [[ ! $HAVE_MARKER ]]; then
+          # Use of insertion markers, when "add_to_existing=true" with no markers in the existing file
           echo "$insertion_marker_begin" >> "$output_file"
           echo "$insertion_marker_end" >> "$output_file"
         fi

From 1f883693ec0568ba026da63173d4a0356d760c5c Mon Sep 17 00:00:00 2001
From: Maksym Vlasov <MaxymVlasov@users.noreply.github.com>
Date: Mon, 9 Sep 2024 23:10:38 +0300
Subject: [PATCH 4/5] Apply suggestions from code review

Co-authored-by: George L. Yermulnik <yz@yz.kiev.ua>
---
 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 758046908..43f95aa13 100755
--- a/hooks/terraform_docs.sh
+++ b/hooks/terraform_docs.sh
@@ -269,7 +269,7 @@ function terraform_docs {
       # terraform-docs in 'inject' mode adds markers by default if they are not present
       #
       if [[ $add_to_existing == false ]]; then
-        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
+        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file") || true
         [[ ! $HAVE_MARKER ]] && continue
       fi
       # shellcheck disable=SC2086
@@ -286,7 +286,7 @@ function terraform_docs {
       # and, if not, append "hook markers" to the end of the file.
       #
       if [[ $add_to_existing == true ]]; then
-        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)
+        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file") || true
 
         if [[ ! $HAVE_MARKER ]]; then
           # Use of insertion markers, when "add_to_existing=true" with no markers in the existing file

From 7c5c3a5ae123501ceea77439ea50dc6ecc23e7a5 Mon Sep 17 00:00:00 2001
From: MaxymVlasov <MaxymVlasov@users.noreply.github.com>
Date: Tue, 10 Sep 2024 15:58:35 +0300
Subject: [PATCH 5/5] Apply review suggestions

---
 hooks/terraform_docs.sh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh
index 43f95aa13..435190a32 100755
--- a/hooks/terraform_docs.sh
+++ b/hooks/terraform_docs.sh
@@ -269,8 +269,8 @@ function terraform_docs {
       # terraform-docs in 'inject' mode adds markers by default if they are not present
       #
       if [[ $add_to_existing == false ]]; then
-        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file") || true
-        [[ ! $HAVE_MARKER ]] && continue
+        have_marker=$(grep -o "$insertion_marker_begin" "$output_file") || unset have_marker
+        [[ ! $have_marker ]] && continue
       fi
       # shellcheck disable=SC2086
       terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter $args ./ > /dev/null
@@ -286,9 +286,9 @@ function terraform_docs {
       # and, if not, append "hook markers" to the end of the file.
       #
       if [[ $add_to_existing == true ]]; then
-        HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file") || true
+        have_marker=$(grep -o "$insertion_marker_begin" "$output_file") || unset have_marker
 
-        if [[ ! $HAVE_MARKER ]]; then
+        if [[ ! $have_marker ]]; then
           # Use of insertion markers, when "add_to_existing=true" with no markers in the existing file
           echo "$insertion_marker_begin" >> "$output_file"
           echo "$insertion_marker_end" >> "$output_file"