Skip to content

Commit cd090b6

Browse files
authored
feat(terraform_docs): Add support for custom markers to better support other formats than Markdown (#752)
1 parent 92fbec7 commit cd090b6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,16 @@ Unlike most other hooks, this hook triggers once if there are any changed files
608608
- --hook-config=--add-to-existing-file=true # Boolean. true or false
609609
- --hook-config=--create-file-if-not-exist=true # Boolean. true or false
610610
- --hook-config=--use-standard-markers=true # Boolean. Defaults to true (v1.93+), false (<v1.93). Set to true for compatibility with terraform-docs
611+
# The following two options "--custom-marker-begin" and "--custom-marker-end" are ignored if "--use-standard-markers" is set to false
612+
- --hook-config=--custom-marker-begin=<!-- BEGIN_TF_DOCS --> # String.
613+
# Set to use custom marker which helps you with using other formats like asciidoc.
614+
# For Asciidoc this could be "--hook-config=--custom-marker-begin=// BEGIN_TF_DOCS"
615+
- --hook-config=--custom-marker-end=<!-- END_TF_DOCS --> # String.
616+
# Set to use custom marker which helps you with using other formats like asciidoc.
617+
# For Asciidoc this could be "--hook-config=--custom-marker-end=// END_TF_DOCS"
618+
- --hook-config=--custom-doc-header="# " # String. Defaults to "# "
619+
# Set to use custom marker which helps you with using other formats like asciidoc.
620+
# For Asciidoc this could be "--hook-config=--custom-marker-end=\= "
611621
```
612622

613623
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:

hooks/terraform_docs.sh

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ readonly SCRIPT_DIR
99

1010
insertion_marker_begin="<!-- BEGIN_TF_DOCS -->"
1111
insertion_marker_end="<!-- END_TF_DOCS -->"
12+
doc_header="# "
1213

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

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

4950
#######################################################################
@@ -115,6 +116,18 @@ function terraform_docs {
115116
common::colorify "yellow" "WARNING: --use-standard-markers is deprecated and will be removed in the future."
116117
common::colorify "yellow" " All needed changes already done by the hook, feel free to remove --use-standard-markers setting from your pre-commit config"
117118
;;
119+
--custom-marker-begin)
120+
insertion_marker_begin=$value
121+
common::colorify "green" "INFO: --custom-marker-begin is used and the marker is set to \"$value\"."
122+
;;
123+
--custom-marker-end)
124+
insertion_marker_end=$value
125+
common::colorify "green" "INFO: --custom-marker-end is used and the marker is set to \"$value\"."
126+
;;
127+
--custom-doc-header)
128+
doc_header=$value
129+
common::colorify "green" "INFO: --custom-doc-header is used and the doc header is set to \"$value\"."
130+
;;
118131
esac
119132
done
120133

@@ -198,7 +211,7 @@ function terraform_docs {
198211

199212
# Use of insertion markers, where there is no existing README file
200213
{
201-
echo -e "# ${PWD##*/}\n"
214+
echo -e "${doc_header}${PWD##*/}\n"
202215
echo "$insertion_marker_begin"
203216
echo "$insertion_marker_end"
204217
} >> "$output_file"

0 commit comments

Comments
 (0)