From fa5f222ba84cce0f42355ff512630cabe94a63b5 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 30 Aug 2024 13:12:07 +0300 Subject: [PATCH 1/8] sed='' --- README.md | 4 ++-- hooks/terraform_docs.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0b262520b..a872bf1c8 100644 --- a/README.md +++ b/README.md @@ -585,8 +585,8 @@ Unlike most other hooks, this hook triggers once if there are any changed files To migrate everything to `terraform-docs` insertion markers, run in repo root: ```bash - grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i'' -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' - grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i'' -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' + grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i='' -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' + grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i='' -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' ``` ```yaml diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index ade82c598..de70c2b9f 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -40,8 +40,8 @@ function main { function replace_old_markers { local -r file=$1 - sed -i'' -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" - sed -i'' -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" + sed -i='' -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" + sed -i='' -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" } ####################################################################### From 74a133c161cf980a9bb505d58da1f62031cf3751 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 30 Aug 2024 13:13:50 +0300 Subject: [PATCH 2/8] different sed per OS --- README.md | 9 +++++---- hooks/terraform_docs.sh | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a872bf1c8..cea78e6c2 100644 --- a/README.md +++ b/README.md @@ -585,8 +585,9 @@ Unlike most other hooks, this hook triggers once if there are any changed files To migrate everything to `terraform-docs` insertion markers, run in repo root: ```bash - grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i='' -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' - grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 sed -i='' -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' + SED_CMD=$(sed --version > /dev/null 2>&1 && echo "sed -i''" || echo "sed -i ''") + grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 $SED_CMD -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' + grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 $SED_CMD -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' ``` ```yaml @@ -598,7 +599,7 @@ Unlike most other hooks, this hook triggers once if there are any changed files - --hook-config=--use-standard-markers=true # Boolean. Defaults to true (v1.93+), false ( **Warning** > Avoid use `recursive.enabled: true` in config file, that can cause unexpected behavior. -5. If you need some exotic settings, it can be done too. I.e. this one generates HCL files: +2. If you need some exotic settings, it can be done too. I.e. this one generates HCL files: ```yaml - id: terraform_docs diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index de70c2b9f..d1390faa4 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -40,8 +40,10 @@ function main { function replace_old_markers { local -r file=$1 - sed -i='' -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" - sed -i='' -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" + # Determine the appropriate sed command based on the operating system (GNU sed or BSD sed) + SED_CMD=$(sed --version > /dev/null 2>&1 && echo "sed -i''" || echo "sed -i ''") + $SED_CMD -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" + $SED_CMD -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" } ####################################################################### From cc5d581ee6fe4f687db195ed88083467633a903a Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 30 Aug 2024 13:26:00 +0300 Subject: [PATCH 3/8] deal with echo issue --- hooks/terraform_docs.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index d1390faa4..e0ab3aaa8 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -41,8 +41,15 @@ function replace_old_markers { local -r file=$1 # Determine the appropriate sed command based on the operating system (GNU sed or BSD sed) - SED_CMD=$(sed --version > /dev/null 2>&1 && echo "sed -i''" || echo "sed -i ''") + if sed --version > /dev/null 2>&1; then + SED_CMD="sed -i''" + else + # shellcheck disable=SC2089 + SED_CMD="sed -i ''" + fi + # shellcheck disable=SC2090 $SED_CMD -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" + # shellcheck disable=SC2090 $SED_CMD -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" } From aee5dc9a8d6625a47396f2b4797368c57cbb5f49 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 30 Aug 2024 13:30:18 +0300 Subject: [PATCH 4/8] try array --- hooks/terraform_docs.sh | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index e0ab3aaa8..d52f0a76a 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -42,15 +42,14 @@ function replace_old_markers { # Determine the appropriate sed command based on the operating system (GNU sed or BSD sed) if sed --version > /dev/null 2>&1; then - SED_CMD="sed -i''" + SED_CMD=(sed -i'') else - # shellcheck disable=SC2089 - SED_CMD="sed -i ''" + SED_CMD=(sed -i '') fi - # shellcheck disable=SC2090 - $SED_CMD -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" - # shellcheck disable=SC2090 - $SED_CMD -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" + # shellcheck disable=SC2068 + ${SED_CMD[@]} -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" + # shellcheck disable=SC2068 + ${SED_CMD[@]} -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" } ####################################################################### From bb402c9080c92d7ea1bb72f3334d0baf512b73aa Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 30 Aug 2024 13:34:51 +0300 Subject: [PATCH 5/8] Update README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cea78e6c2..4c34e4e77 100644 --- a/README.md +++ b/README.md @@ -585,9 +585,9 @@ Unlike most other hooks, this hook triggers once if there are any changed files To migrate everything to `terraform-docs` insertion markers, run in repo root: ```bash - SED_CMD=$(sed --version > /dev/null 2>&1 && echo "sed -i''" || echo "sed -i ''") - grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 $SED_CMD -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' - grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 $SED_CMD -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' + if sed --version > /dev/null 2>&1; then SED_CMD=(sed -i''); else SED_CMD=(sed -i ''); fi + grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 ${SED_CMD[@]} -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' + grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 ${SED_CMD[@]} -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' ``` ```yaml From c44f074a28c215645008d994fee9847ebf2c4c8f Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Fri, 30 Aug 2024 13:38:54 +0300 Subject: [PATCH 6/8] fix readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4c34e4e77..3a2ffa2e2 100644 --- a/README.md +++ b/README.md @@ -599,7 +599,7 @@ Unlike most other hooks, this hook triggers once if there are any changed files - --hook-config=--use-standard-markers=true # Boolean. Defaults to true (v1.93+), false ( **Warning** > Avoid use `recursive.enabled: true` in config file, that can cause unexpected behavior. -2. If you need some exotic settings, it can be done too. I.e. this one generates HCL files: +5. If you need some exotic settings, it can be done too. I.e. this one generates HCL files: ```yaml - id: terraform_docs From a76f03bfc4978741f37f79a6b4a86dc72078a78d Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 30 Aug 2024 15:15:17 +0300 Subject: [PATCH 7/8] quotes --- README.md | 4 ++-- hooks/terraform_docs.sh | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3a2ffa2e2..a74bd8ba0 100644 --- a/README.md +++ b/README.md @@ -586,8 +586,8 @@ Unlike most other hooks, this hook triggers once if there are any changed files ```bash if sed --version > /dev/null 2>&1; then SED_CMD=(sed -i''); else SED_CMD=(sed -i ''); fi - grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 ${SED_CMD[@]} -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' - grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 ${SED_CMD[@]} -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' + grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 "${SED_CMD[@]}" -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' + grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 "${SED_CMD[@]}" -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' ``` ```yaml diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index d52f0a76a..2a81a4c99 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -46,10 +46,8 @@ function replace_old_markers { else SED_CMD=(sed -i '') fi - # shellcheck disable=SC2068 - ${SED_CMD[@]} -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" - # shellcheck disable=SC2068 - ${SED_CMD[@]} -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" + "${SED_CMD[@]}" -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" + "${SED_CMD[@]}" -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" } ####################################################################### From 70670818f5d14ba13c268dd366ecfa8374e88f42 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 30 Aug 2024 15:49:25 +0300 Subject: [PATCH 8/8] Simplify code --- README.md | 2 +- hooks/terraform_docs.sh | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a74bd8ba0..66d6ea39c 100644 --- a/README.md +++ b/README.md @@ -585,7 +585,7 @@ Unlike most other hooks, this hook triggers once if there are any changed files To migrate everything to `terraform-docs` insertion markers, run in repo root: ```bash - if sed --version > /dev/null 2>&1; then SED_CMD=(sed -i''); else SED_CMD=(sed -i ''); fi + sed --version &> /dev/null && SED_CMD=(sed -i) || SED_CMD=(sed -i '') grep -rl --null 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 "${SED_CMD[@]}" -e 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/' grep -rl --null 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs -0 "${SED_CMD[@]}" -e 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/' ``` diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index 2a81a4c99..4c402549e 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -41,11 +41,7 @@ function replace_old_markers { local -r file=$1 # Determine the appropriate sed command based on the operating system (GNU sed or BSD sed) - if sed --version > /dev/null 2>&1; then - SED_CMD=(sed -i'') - else - SED_CMD=(sed -i '') - fi + sed --version &> /dev/null && SED_CMD=(sed -i) || SED_CMD=(sed -i '') "${SED_CMD[@]}" -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" "${SED_CMD[@]}" -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" }