Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace terraform_docs use of GNU sed with perl #15

Merged
merged 2 commits into from
May 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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="tmp_$(date | md5).txt"
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