Skip to content

Commit

Permalink
fix(terraform_docs): Always use GNU sed
Browse files Browse the repository at this point in the history
Ensure compatibility of `terraform_docs` hook on MacOS and Linux by using GNU sed.
  • Loading branch information
cschroer committed Aug 29, 2024
1 parent 99fceb8 commit ad5d40b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

#######################################################################
Expand Down

0 comments on commit ad5d40b

Please sign in to comment.