From 2d3782cefadb0941102fe413ab08a75905fcd081 Mon Sep 17 00:00:00 2001 From: jeremy avnet <162998+brainsik@users.noreply.github.com> Date: Sun, 20 May 2018 03:10:28 -0700 Subject: [PATCH] Replace terraform_docs use of GNU sed with perl (#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. --- terraform_docs.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/terraform_docs.sh b/terraform_docs.sh index 918d5583f..781973aa7 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -15,7 +15,7 @@ for file_with_path in "$@"; do tfvars_files+=("$file_with_path") fi - let "index+=1" + ((index+=1)) done readonly tmp_file=$(mktemp) @@ -33,11 +33,11 @@ for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do terraform-docs md ./ > "$tmp_file" - # Replace content between markers with the placeholder - http://fahdshariff.blogspot.no/2012/12/sed-mutli-line-replacement-between-two.html - 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" + # Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834 + 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" - # Replace placeholder with the content of the file - https://stackoverflow.com/a/31057013/550451 - sed -i -e "/I_WANT_TO_BE_REPLACED/r $tmp_file" -e "//d" "$text_file" + # Replace placeholder with the content of the file + perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", ; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file" rm -f "$tmp_file"