Skip to content

Commit d03f44f

Browse files
feat(terraform_docs): Start seamless migration to terraform-docs markers (antonbabenko#701)
--------- Co-authored-by: George L. Yermulnik <[email protected]>
1 parent de6a8d9 commit d03f44f

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,9 @@ Unlike most other hooks, this hook triggers once if there are any changed files
568568
1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/terraform-docs/terraform-docs) framed by markers:
569569

570570
```txt
571-
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
571+
<!-- BEGIN_TF_DOCS -->
572572
573-
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
573+
<!-- END_TF_DOCS -->
574574
```
575575

576576
if they are present in `README.md`.
@@ -581,8 +581,8 @@ Unlike most other hooks, this hook triggers once if there are any changed files
581581
* create a documentation file
582582
* extend existing documentation file by appending markers to the end of the file (see item 1 above)
583583
* use different filename for the documentation (default is `README.md`)
584-
* use the same insertion markers as `terraform-docs` by default. It will be default in `v2.0`.
585-
To migrate to `terraform-docs` insertion markers, run in repo root:
584+
* use the same insertion markers as `terraform-docs`. It's default starting from `v1.93`.
585+
To migrate everything to `terraform-docs` insertion markers, run in repo root:
586586

587587
```bash
588588
grep -rl 'BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK' . | xargs sed -i 's/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/BEGIN_TF_DOCS/g'
@@ -595,7 +595,7 @@ Unlike most other hooks, this hook triggers once if there are any changed files
595595
- --hook-config=--path-to-file=README.md # Valid UNIX path. I.e. ../TFDOC.md or docs/README.md etc.
596596
- --hook-config=--add-to-existing-file=true # Boolean. true or false
597597
- --hook-config=--create-file-if-not-exist=true # Boolean. true or false
598-
- --hook-config=--use-standard-markers=true # Boolean. Defaults in v1.x to false. Set to true for compatibility with terraform-docs
598+
- --hook-config=--use-standard-markers=true # Boolean. Defaults to true (v1.93+), false (<v1.93). Set to true for compatibility with terraform-docs
599599
```
600600

601601
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: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ readonly SCRIPT_DIR
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 -->"
10+
insertion_marker_begin="<!-- BEGIN_TF_DOCS -->"
11+
insertion_marker_end="<!-- END_TF_DOCS -->"
1412

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 -->"
13+
# Old markers used by the hook before the introduction of the terraform-docs markers
14+
readonly old_insertion_marker_begin="<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
15+
readonly old_insertion_marker_end="<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
1816

1917
function main {
2018
common::initialize "$SCRIPT_DIR"
@@ -29,6 +27,23 @@ function main {
2927
terraform_docs_ "${HOOK_CONFIG[*]}" "${ARGS[*]}" "${FILES[@]}"
3028
}
3129

30+
#######################################################################
31+
# Function to replace old markers with new markers affected files
32+
# Globals:
33+
# insertion_marker_begin - Standard insertion marker at beginning
34+
# insertion_marker_end - Standard insertion marker at the end
35+
# old_insertion_marker_begin - Old insertion marker at beginning
36+
# old_insertion_marker_end - Old insertion marker at the end
37+
# Arguments:
38+
# file (string) filename to check
39+
#######################################################################
40+
function replace_old_markers {
41+
local -r file=$1
42+
43+
sed -i "s/^${old_insertion_marker_begin}$/${insertion_marker_begin}/" "$file"
44+
sed -i "s/^${old_insertion_marker_end}$/${insertion_marker_end}/" "$file"
45+
}
46+
3247
#######################################################################
3348
# Function which prepares hacks for old versions of `terraform` and
3449
# `terraform-docs` that them call `terraform_docs`
@@ -124,7 +139,7 @@ function terraform_docs {
124139
local use_path_to_file=false
125140
local add_to_existing=false
126141
local create_if_not_exist=false
127-
local use_standard_markers=false
142+
local use_standard_markers=true
128143

129144
read -r -a configs <<< "$hook_config"
130145

@@ -147,14 +162,16 @@ function terraform_docs {
147162
;;
148163
--use-standard-markers)
149164
use_standard_markers=$value
165+
common::colorify "yellow" "WARNING: --use-standard-markers is deprecated and will be removed in the future."
166+
common::colorify "yellow" " All needed changes already done by the hook, feel free to remove --use-standard-markers setting from your pre-commit config"
150167
;;
151168
esac
152169
done
153170

154-
if [ "$use_standard_markers" = true ]; then
155-
# update the insertion markers to those used by terraform-docs
156-
insertion_marker_begin="$standard_insertion_marker_begin"
157-
insertion_marker_end="$standard_insertion_marker_end"
171+
if [[ $use_standard_markers == false ]]; then
172+
# update the insertion markers to those used by pre-commit-terraform before v1.93
173+
insertion_marker_begin="$old_insertion_marker_begin"
174+
insertion_marker_end="$old_insertion_marker_end"
158175
fi
159176

160177
# Override formatter if no config file set
@@ -232,6 +249,8 @@ function terraform_docs {
232249
# If file still not exist - skip dir
233250
[[ ! -f "$text_file" ]] && popd > /dev/null && continue
234251

252+
replace_old_markers "$text_file"
253+
235254
#
236255
# If `--add-to-existing-file=true` set, check is in file exist "hook markers",
237256
# and if not - append "hook markers" to the end of file.

0 commit comments

Comments
 (0)