Skip to content

Commit 4a0e1fe

Browse files
jonmcewenarxro008yermulnikMaxymVlasov
authored
feat(terraform_docs): Add terraform-docs default markers support and describe how to migrate to them (antonbabenko#609)
--------- Co-authored-by: Jon M <[email protected]> Co-authored-by: George L. Yermulnik <[email protected]> Co-authored-by: Maksym Vlasov <[email protected]>
1 parent 03705ce commit 4a0e1fe

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,21 @@ Unlike most other hooks, this hook triggers once if there are any changed files
486486
* create a documentation file
487487
* extend existing documentation file by appending markers to the end of the file (see item 1 above)
488488
* use different filename for the documentation (default is `README.md`)
489+
* use the same insertion markers as `terraform-docs` by default. It will be default in `v2.0`.
490+
To migrate to `terraform-docs` insertion markers, run in repo root:
491+
492+
```bash
493+
grep -rl 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs sed -i 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/g'
494+
grep -rl 'END OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs sed -i 's/END OF PRE-COMMIT-TERRAFORM DOCS HOOK/END_TF_DOCS/g'
495+
```
489496

490497
```yaml
491498
- id: terraform_docs
492499
args:
493500
- --hook-config=--path-to-file=README.md # Valid UNIX path. I.e. ../TFDOC.md or docs/README.md etc.
494501
- --hook-config=--add-to-existing-file=true # Boolean. true or false
495502
- --hook-config=--create-file-if-not-exist=true # Boolean. true or false
503+
- --hook-config=--use-standard-markers=true # Boolean. Defaults in v1.x to false. Set to true for compatibility with terraform-docs
496504
```
497505

498506
4. You can provide [any configuration available in `terraform-docs`](https://terraform-docs.io/user-guide/configuration/) as an argument to `terraform_doc` hook, for example:

hooks/terraform_docs.sh

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
77
# shellcheck source=_common.sh
88
. "$SCRIPT_DIR/_common.sh"
99

10+
# set up default insertion markers. These will be changed to the markers used by
11+
# terraform-docs if the hook config contains `--use-standard-markers=true`
12+
insertion_marker_begin="<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
13+
insertion_marker_end="<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
14+
15+
# these are the standard insertion markers used by terraform-docs
16+
readonly standard_insertion_marker_begin="<!-- BEGIN_TF_DOCS -->"
17+
readonly standard_insertion_marker_end="<!-- END_TF_DOCS -->"
18+
1019
function main {
1120
common::initialize "$SCRIPT_DIR"
1221
common::parse_cmdline "$@"
@@ -111,6 +120,7 @@ function terraform_docs {
111120
local text_file="README.md"
112121
local add_to_existing=false
113122
local create_if_not_exist=false
123+
local use_standard_markers=false
114124

115125
read -r -a configs <<< "$hook_config"
116126

@@ -130,9 +140,18 @@ function terraform_docs {
130140
--create-file-if-not-exist)
131141
create_if_not_exist=$value
132142
;;
143+
--use-standard-markers)
144+
use_standard_markers=$value
145+
;;
133146
esac
134147
done
135148

149+
if [ "$use_standard_markers" = true ]; then
150+
# update the insertion markers to those used by terraform-docs
151+
insertion_marker_begin="$standard_insertion_marker_begin"
152+
insertion_marker_end="$standard_insertion_marker_end"
153+
fi
154+
136155
# Override formatter if no config file set
137156
if [[ "$args" != *"--config"* ]]; then
138157
local tf_docs_formatter="md"
@@ -178,10 +197,12 @@ function terraform_docs {
178197
dir="$(dirname "$text_file")"
179198

180199
mkdir -p "$dir"
200+
201+
# Use of insertion markers, where there is no existing README file
181202
{
182203
echo -e "# ${PWD##*/}\n"
183-
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
184-
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
204+
echo "$insertion_marker_begin"
205+
echo "$insertion_marker_end"
185206
} >> "$text_file"
186207
fi
187208

@@ -193,11 +214,12 @@ function terraform_docs {
193214
# and if not - append "hook markers" to the end of file.
194215
#
195216
if $add_to_existing; then
196-
HAVE_MARKER=$(grep -o '<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->' "$text_file" || exit 0)
217+
HAVE_MARKER=$(grep -o "$insertion_marker_begin" "$text_file" || exit 0)
197218

198219
if [ ! "$HAVE_MARKER" ]; then
199-
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
200-
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
220+
# Use of insertion markers, where addToExisting=true, with no markers in the existing file
221+
echo "$insertion_marker_begin" >> "$text_file"
222+
echo "$insertion_marker_end" >> "$text_file"
201223
fi
202224
fi
203225

@@ -218,8 +240,10 @@ function terraform_docs {
218240
rm -f "$tmp_file_docs_tf"
219241
fi
220242

243+
# Use of insertion markers to insert the terraform-docs output between the markers
221244
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
222-
perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file"
245+
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 \$_ }"
246+
perl -i -ne "$perl_expression" "$text_file"
223247

224248
# Replace placeholder with the content of the file
225249
perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file"

0 commit comments

Comments
 (0)