Skip to content

Commit b7c78ef

Browse files
committed
fix(terraform_docs): Ensure compatibility by using GNU sed
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.
1 parent 99fceb8 commit b7c78ef

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

hooks/terraform_docs.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,25 @@ function main {
4040
function replace_old_markers {
4141
local -r file=$1
4242

43-
sed -i "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file"
44-
sed -i "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file"
43+
# Detect sed version, if on MacOS use gsed to support -i parameter
44+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
45+
SED_COMMAND="sed"
46+
elif [[ "$OSTYPE" == "darwin"* ]]; then
47+
set +e # next command may fail - allow failure
48+
PRG="$(command -v "gsed" 2>/dev/null)"
49+
set -e # exit script on any error again
50+
if [ -z "$PRG" ]; then
51+
echo "ERROR: Detected MacOS but no gsed installed - install it via brew (brew install gsed)"
52+
exit 1
53+
fi
54+
SED_COMMAND="gsed"
55+
else
56+
echo "ERROR: Unsupported OS system - hook supports MacOS and Linux only"
57+
exit 1
58+
fi
59+
60+
${SED_COMMAND} -i "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file"
61+
${SED_COMMAND} -i "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file"
4562
}
4663

4764
#######################################################################

0 commit comments

Comments
 (0)