From c986c5e3440be4bf5a46c7933bb629227a3cd292 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Fri, 30 Aug 2024 16:34:05 +0300 Subject: [PATCH] fix(`terraform_docs`): Fix non-GNU sed issues, introduced in v1.93.0, as previous fix doesn't work correctly (#708) --- README.md | 5 +++-- hooks/terraform_docs.sh | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0b262520b..66d6ea39c 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 --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/' ``` ```yaml diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index ade82c598..4c402549e 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 --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" } #######################################################################