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

feat(terraform-docs): Add support for replace mode for TF 0.12+; Use native saving to file for TF 0.12+. Both requires terraform-docs v0.12.0+ which released in 2021. #705

Merged
merged 6 commits into from
Aug 29, 2024
Next Next commit
feat(terraform-docs): Use native save to file for TF 0.12+ (require…
…d `terraform-docs` v0.12.0+)
  • Loading branch information
MaxymVlasov committed Aug 29, 2024
commit 6c417c1a3e1bea5cd6b44f567927052071994e68
22 changes: 11 additions & 11 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ function terraform_docs {
((index += 1))
done

local -r tmp_file=$(mktemp)

#
# Get hook settings
#
Expand Down Expand Up @@ -267,7 +265,7 @@ function terraform_docs {

if [[ "$terraform_docs_awk_file" == "0" ]]; then
# shellcheck disable=SC2086
terraform-docs --output-file="" $tf_docs_formatter $args ./ > "$tmp_file"
terraform-docs --output-mode inject $tf_docs_formatter $args --output-file="$text_file" ./
else
# Can't append extension for mktemp, so renaming instead
local tmp_file_docs
Expand All @@ -277,20 +275,22 @@ function terraform_docs {
tmp_file_docs_tf="$tmp_file_docs.tf"

awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"

local -r tmp_file=$(mktemp)
# shellcheck disable=SC2086
terraform-docs --output-file="" $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file"
rm -f "$tmp_file_docs_tf"
fi

# Use of insertion markers to insert the terraform-docs output between the markers
# 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_expression="if (/$insertion_marker_begin/../$insertion_marker_end/) { print \$_ if /$insertion_marker_begin/; print \"I_WANT_TO_BE_REPLACED\\n\$_\" if /$insertion_marker_end/;} else { print \$_ }"
perl -i -ne "$perl_expression" "$text_file"
# Use of insertion markers to insert the terraform-docs output between the markers
# 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_expression="if (/$insertion_marker_begin/../$insertion_marker_end/) { print \$_ if /$insertion_marker_begin/; print \"I_WANT_TO_BE_REPLACED\\n\$_\" if /$insertion_marker_end/;} else { print \$_ }"
perl -i -ne "$perl_expression" "$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"
# 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"
rm -f "$tmp_file"
fi

popd > /dev/null
done
Expand Down