Skip to content

Commit 2d3782c

Browse files
brainsikantonbabenko
authored andcommitted
Replace terraform_docs use of GNU sed with perl (antonbabenko#15)
* Fix ShellCheck warning 2219 https://github.com/koalaman/shellcheck/wiki/SC2219 * Replace GNU sed commands with perl This replaces the sed commands which required GNU sed be installed with perl versions. This should make this script more universally usable (e.g., on macOS) without installing additional tools.
1 parent 6b06683 commit 2d3782c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

terraform_docs.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ for file_with_path in "$@"; do
1515
tfvars_files+=("$file_with_path")
1616
fi
1717

18-
let "index+=1"
18+
((index+=1))
1919
done
2020

2121
readonly tmp_file=$(mktemp)
@@ -33,11 +33,11 @@ for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
3333

3434
terraform-docs md ./ > "$tmp_file"
3535

36-
# Replace content between markers with the placeholder - http://fahdshariff.blogspot.no/2012/12/sed-mutli-line-replacement-between-two.html
37-
sed -i -n '/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/{p;:a;N;/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/!ba;s/.*\n/I_WANT_TO_BE_REPLACED\n/};p' "$text_file"
36+
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
37+
perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file"
3838

39-
# Replace placeholder with the content of the file - https://stackoverflow.com/a/31057013/550451
40-
sed -i -e "/I_WANT_TO_BE_REPLACED/r $tmp_file" -e "//d" "$text_file"
39+
# Replace placeholder with the content of the file
40+
perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file"
4141

4242
rm -f "$tmp_file"
4343

0 commit comments

Comments
 (0)