Skip to content

Commit

Permalink
Replace terraform_docs use of GNU sed with perl (#15)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
brainsik authored and antonbabenko committed May 20, 2018
1 parent 6b06683 commit 2d3782c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file"

rm -f "$tmp_file"

Expand Down

0 comments on commit 2d3782c

Please sign in to comment.