From ad5d40b6686386e930560851f12b670df7110828 Mon Sep 17 00:00:00 2001 From: Christian Schroer <1319445+cschroer@users.noreply.github.com> Date: Thu, 29 Aug 2024 09:03:05 +0200 Subject: [PATCH] fix(terraform_docs): Always use GNU sed Ensure compatibility of `terraform_docs` hook on MacOS and Linux by using GNU sed. --- hooks/terraform_docs.sh | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index 0a3dbc298..31ad0fe22 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -40,8 +40,25 @@ function main { function replace_old_markers { local -r file=$1 - sed -i "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" - sed -i "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" + # Detect sed version, if on MacOS use gsed to support -i parameter + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + SED_COMMAND="sed" + elif [[ "$OSTYPE" == "darwin"* ]]; then + set +e # next command may fail - allow failure + PRG="$(command -v "gsed" 2>/dev/null)" + set -e # exit script on any error again + if [ -z "$PRG" ]; then + echo "ERROR: Detected MacOS but no gsed installed - install it via brew (brew install gsed)" + exit 1 + fi + SED_COMMAND="gsed" + else + echo "ERROR: Unsupported OS system - hook supports MacOS and Linux only" + exit 1 + fi + + ${SED_COMMAND} -i "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file" + ${SED_COMMAND} -i "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file" } #######################################################################