Skip to content

Commit

Permalink
fix(terraform_docs): Ensure compatibility by using 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.

In PR antonbabenko#701, the `terraform_docs` hook uses `sed` for in-file changes. MacOS requires an extension for the `-i` parameter, unlike GNU sed. The solution is to use GNU sed on both MacOS and Linux to ensure consistent behaviour across operating systems.
  • Loading branch information
cschroer committed Aug 29, 2024
1 parent 99fceb8 commit b7c78ef
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 b7c78ef

Please sign in to comment.