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

fix(terraform_docs): Restore --hook-config=--add-to-existing-file default behavior. Regression from 1.94.0. #716

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Changes from 4 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
44 changes: 30 additions & 14 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,40 @@ function terraform_docs {

replace_old_markers "$output_file"

#
# If `--add-to-existing-file=true` set, check is in file exist "hook markers",
# and if not - append "hook markers" to the end of file.
#
if $add_to_existing; then
HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file" || exit 0)

if [ ! "$HAVE_MARKER" ]; then
# Use of insertion markers, where addToExisting=true, with no markers in the existing file
echo "$insertion_marker_begin" >> "$output_file"
echo "$insertion_marker_end" >> "$output_file"
fi
fi

if [[ "$terraform_docs_awk_file" == "0" ]]; then
#? TF 0.12+ and terraform-docs 0.12.0+
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

#
# If `--add-to-existing-file=false` (default behavior), check if "hook markers" exist in file,
# and, if not, skip execution to avoid addition of terraform-docs section, as
# terraform-docs in 'inject' mode adds markers by default if they are not present
#
if [[ $add_to_existing == false ]]; then
HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file") || true
[[ ! $HAVE_MARKER ]] && continue
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
fi
# shellcheck disable=SC2086
terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter $args ./ > /dev/null

else
#? TF 0.12+ and terraform-docs < 0.8
#? Yes, we don't cover case of TF 0.12+ and terraform-docs 0.8-0.11
#? but I probably just drop this section in next release of the hook,
#? as there's no sense to support hacks for tool versions which were released more than 3 years ago

#
# If `--add-to-existing-file=true` set, check if "hook markers" exist in file,
# and, if not, append "hook markers" to the end of the file.
#
if [[ $add_to_existing == true ]]; then
HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$output_file") || true

if [[ ! $HAVE_MARKER ]]; then
# Use of insertion markers, when "add_to_existing=true" with no markers in the existing file
echo "$insertion_marker_begin" >> "$output_file"
echo "$insertion_marker_end" >> "$output_file"
fi
fi
# Can't append extension for mktemp, so renaming instead
local tmp_file_docs
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
Expand Down
Loading