Skip to content

Commit

Permalink
feat(terraform_docs): Add support for custom markers to better supp…
Browse files Browse the repository at this point in the history
…ort other formats than Markdown (#752)
  • Loading branch information
tarfu authored Jan 16, 2025
1 parent 92fbec7 commit cd090b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,16 @@ Unlike most other hooks, this hook triggers once if there are any changed files
- --hook-config=--add-to-existing-file=true # Boolean. true or false
- --hook-config=--create-file-if-not-exist=true # Boolean. true or false
- --hook-config=--use-standard-markers=true # Boolean. Defaults to true (v1.93+), false (<v1.93). Set to true for compatibility with terraform-docs
# The following two options "--custom-marker-begin" and "--custom-marker-end" are ignored if "--use-standard-markers" is set to false
- --hook-config=--custom-marker-begin=<!-- BEGIN_TF_DOCS --> # String.
# Set to use custom marker which helps you with using other formats like asciidoc.
# For Asciidoc this could be "--hook-config=--custom-marker-begin=// BEGIN_TF_DOCS"
- --hook-config=--custom-marker-end=<!-- END_TF_DOCS --> # String.
# Set to use custom marker which helps you with using other formats like asciidoc.
# For Asciidoc this could be "--hook-config=--custom-marker-end=// END_TF_DOCS"
- --hook-config=--custom-doc-header="# " # String. Defaults to "# "
# Set to use custom marker which helps you with using other formats like asciidoc.
# For Asciidoc this could be "--hook-config=--custom-marker-end=\= "
```

4. If you want to use a terraform-docs config file, you must supply the path to the file, relative to the git repo root path:
Expand Down
19 changes: 16 additions & 3 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readonly SCRIPT_DIR

insertion_marker_begin="<!-- BEGIN_TF_DOCS -->"
insertion_marker_end="<!-- END_TF_DOCS -->"
doc_header="# "

# Old markers used by the hook before the introduction of the terraform-docs markers
readonly old_insertion_marker_begin="<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
Expand Down Expand Up @@ -42,8 +43,8 @@ function replace_old_markers {

# Determine the appropriate sed command based on the operating system (GNU sed or BSD sed)
sed --version &> /dev/null && SED_CMD=(sed -i) || SED_CMD=(sed -i '')
"${SED_CMD[@]}" -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file"
"${SED_CMD[@]}" -e "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file"
"${SED_CMD[@]}" -e "s/^${old_insertion_marker_begin}$/${insertion_marker_begin//\//\\/}/" "$file"
"${SED_CMD[@]}" -e "s/^${old_insertion_marker_end}$/${insertion_marker_end//\//\\/}/" "$file"
}

#######################################################################
Expand Down Expand Up @@ -115,6 +116,18 @@ function terraform_docs {
common::colorify "yellow" "WARNING: --use-standard-markers is deprecated and will be removed in the future."
common::colorify "yellow" " All needed changes already done by the hook, feel free to remove --use-standard-markers setting from your pre-commit config"
;;
--custom-marker-begin)
insertion_marker_begin=$value
common::colorify "green" "INFO: --custom-marker-begin is used and the marker is set to \"$value\"."
;;
--custom-marker-end)
insertion_marker_end=$value
common::colorify "green" "INFO: --custom-marker-end is used and the marker is set to \"$value\"."
;;
--custom-doc-header)
doc_header=$value
common::colorify "green" "INFO: --custom-doc-header is used and the doc header is set to \"$value\"."
;;
esac
done

Expand Down Expand Up @@ -198,7 +211,7 @@ function terraform_docs {

# Use of insertion markers, where there is no existing README file
{
echo -e "# ${PWD##*/}\n"
echo -e "${doc_header}${PWD##*/}\n"
echo "$insertion_marker_begin"
echo "$insertion_marker_end"
} >> "$output_file"
Expand Down

0 comments on commit cd090b6

Please sign in to comment.