Skip to content

Commit 8cddce3

Browse files
authored
Added support for terraform_docs for Terraform 0.12 (#45)
1 parent dbf9108 commit 8cddce3

File tree

2 files changed

+115
-8
lines changed

2 files changed

+115
-8
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
### Step 1
88

9-
On MacOSX install the pre-commit package
9+
On MacOSX install the `pre-commit` and `awk` (required for Terraform 0.12) package
1010

1111
```bash
12-
brew install pre-commit
12+
brew install pre-commit awk
1313
```
1414

1515
For other operating systems check the [official documentation](http://pre-commit.com/#install)
@@ -21,10 +21,10 @@ Step into the repository you want to have the pre-commit hooks installed and run
2121
```bash
2222
cat <<EOF > .pre-commit-config.yaml
2323
- repo: git://github.com/antonbabenko/pre-commit-terraform
24-
rev: v1.12.0
24+
rev: v1.13.0
2525
hooks:
2626
- id: terraform_fmt
27-
# - id: terraform_docs # terraform-docs is not working with Terraform 0.12 yet (read note in README)
27+
- id: terraform_docs
2828
EOF
2929
```
3030

@@ -73,7 +73,7 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo
7373
7474
1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing.
7575

76-
1. `terraform-docs` is not working with Terraform 0.12 yet (see [issue #62](https://github.com/segmentio/terraform-docs/issues/62)), so remember to disable `terraform_docs`, `terraform_docs_replace` and `terraform_docs_without_aggregate_type_defaults` hooks in your `.pre-commit-config.yaml`
76+
1. `terraform-docs` works with Terraform 0.12 but support is hackish (it requires `awk` to be installed) and may contain bugs. You can follow the native support of Terraform 0.12 in `terraform-docs` in [issue #62](https://github.com/segmentio/terraform-docs/issues/62).
7777

7878
## Notes for developers
7979

terraform_docs.sh

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,25 @@ main() {
2525
esac
2626
done
2727

28-
terraform_docs "$args" "$files"
28+
local hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12)
29+
30+
if [[ "$hack_terraform_docs" == "1" ]]; then
31+
which awk 2>&1 >/dev/null || ( echo "awk is required for terraform-docs hack to work with Terraform 0.12"; exit 1)
32+
33+
TMP_AWK_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.awk)"
34+
terraform_docs_awk $TMP_AWK_FILE
35+
terraform_docs "$TMP_AWK_FILE" "$args" "$files"
36+
rm -f "$TMP_AWK_FILE"
37+
else
38+
terraform_docs "0" "$args" "$files"
39+
fi
40+
2941
}
3042

3143
terraform_docs() {
32-
readonly args="$1"
33-
readonly files="$2"
44+
readonly terraform_docs_awk_file="$1"
45+
readonly args="$2"
46+
readonly files="$3"
3447

3548
declare -a paths
3649
declare -a tfvars_files
@@ -62,7 +75,14 @@ terraform_docs() {
6275
continue
6376
fi
6477

78+
if [[ "$terraform_docs_awk_file" == "0" ]]; then
6579
terraform-docs $args md ./ > "$tmp_file"
80+
else
81+
TMP_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.tf)"
82+
awk -f "$terraform_docs_awk_file" ./*.tf > "$TMP_FILE"
83+
terraform-docs $args md "$TMP_FILE" > "$tmp_file"
84+
rm -f "$TMP_FILE"
85+
fi
6686

6787
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
6888
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"
@@ -76,6 +96,93 @@ terraform_docs() {
7696
done
7797
}
7898

99+
terraform_docs_awk() {
100+
readonly output_file=$1
101+
102+
cat <<"EOF" > $output_file
103+
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
104+
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
105+
# https://github.com/segmentio/terraform-docs/
106+
# https://github.com/segmentio/terraform-docs/issues/62
107+
108+
{
109+
if ( /\{/ ) {
110+
braceCnt++
111+
}
112+
113+
if ( /\}/ ) {
114+
braceCnt--
115+
}
116+
117+
# [START] variable or output block started
118+
if ($0 ~ /(variable|output) "(.*?)"/) {
119+
# [CLOSE] "default" block
120+
if (blockDefCnt > 0) {
121+
blockDefCnt = 0
122+
}
123+
blockCnt++
124+
print $0
125+
}
126+
127+
# [START] multiline default statement started
128+
if (blockCnt > 0) {
129+
if ($1 == "default") {
130+
print $0
131+
if ($NF ~ /[\[\(\{]/) {
132+
blockDefCnt++
133+
blockDefStart=1
134+
}
135+
}
136+
}
137+
138+
# [PRINT] single line "description"
139+
if (blockDefCnt == 0) {
140+
if ($1 == "description") {
141+
# [CLOSE] "default" block
142+
if (blockDefCnt > 0) {
143+
blockDefCnt = 0
144+
}
145+
print $0
146+
}
147+
}
148+
149+
# [PRINT] single line "type"
150+
if (blockCnt > 0) {
151+
if ($1 == "type" ) {
152+
# [CLOSE] "default" block
153+
if (blockDefCnt > 0) {
154+
blockDefCnt = 0
155+
}
156+
type=$3
157+
if (type ~ "object") {
158+
print " type = \"object\""
159+
} else {
160+
print " type = \"" $3 "\""
161+
}
162+
}
163+
}
164+
165+
# [CLOSE] variable/output block
166+
if (blockCnt > 0) {
167+
if (braceCnt == 0 && blockCnt > 0) {
168+
blockCnt--
169+
print $0
170+
}
171+
}
172+
173+
# [PRINT] Multiline "default" statement
174+
if (blockCnt > 0 && blockDefCnt > 0) {
175+
if (blockDefStart == 1) {
176+
blockDefStart = 0
177+
} else {
178+
print $0
179+
}
180+
}
181+
}
182+
EOF
183+
184+
}
185+
79186
getopt() {
80187
# pure-getopt, a drop-in replacement for GNU getopt in pure Bash.
81188
# version 1.4.3

0 commit comments

Comments
 (0)