@@ -260,24 +260,40 @@ function terraform_docs {
260260
261261 replace_old_markers " $output_file "
262262
263- #
264- # If `--add-to-existing-file=true` set, check is in file exist "hook markers",
265- # and if not - append "hook markers" to the end of file.
266- #
267- if $add_to_existing ; then
268- HAVE_MARKER=$( grep -o " $insertion_marker_begin " " $output_file " || exit 0)
269-
270- if [ ! " $HAVE_MARKER " ]; then
271- # Use of insertion markers, where addToExisting=true, with no markers in the existing file
272- echo " $insertion_marker_begin " >> " $output_file "
273- echo " $insertion_marker_end " >> " $output_file "
274- fi
275- fi
276-
277263 if [[ " $terraform_docs_awk_file " == " 0" ]]; then
264+ # ? TF 0.12+ and terraform-docs 0.12.0+
265+
266+ #
267+ # If `--add-to-existing-file=false` (default behavior), check if "hook markers" exist in file,
268+ # and, if not, skip execution to avoid addition of terraform-docs section, as
269+ # terraform-docs in 'inject' mode adds markers by default if they are not present
270+ #
271+ if [[ $add_to_existing == false ]]; then
272+ have_marker=$( grep -o " $insertion_marker_begin " " $output_file " ) || unset have_marker
273+ [[ ! $have_marker ]] && continue
274+ fi
278275 # shellcheck disable=SC2086
279276 terraform-docs --output-mode=" $output_mode " --output-file=" $output_file " $tf_docs_formatter $args ./ > /dev/null
277+
280278 else
279+ # ? TF 0.12+ and terraform-docs < 0.8
280+ # ? Yes, we don't cover case of TF 0.12+ and terraform-docs 0.8-0.11
281+ # ? but I probably just drop this section in next release of the hook,
282+ # ? as there's no sense to support hacks for tool versions which were released more than 3 years ago
283+
284+ #
285+ # If `--add-to-existing-file=true` set, check if "hook markers" exist in file,
286+ # and, if not, append "hook markers" to the end of the file.
287+ #
288+ if [[ $add_to_existing == true ]]; then
289+ have_marker=$( grep -o " $insertion_marker_begin " " $output_file " ) || unset have_marker
290+
291+ if [[ ! $have_marker ]]; then
292+ # Use of insertion markers, when "add_to_existing=true" with no markers in the existing file
293+ echo " $insertion_marker_begin " >> " $output_file "
294+ echo " $insertion_marker_end " >> " $output_file "
295+ fi
296+ fi
281297 # Can't append extension for mktemp, so renaming instead
282298 local tmp_file_docs
283299 tmp_file_docs=$( mktemp " ${TMPDIR:-/ tmp} /terraform-docs-XXXXXXXXXX" )
0 commit comments