Skip to content

Commit

Permalink
0.12 support for terraform-docs (#152)
Browse files Browse the repository at this point in the history
* 0.12 support for terraform-docs

* portability fix

* fix version check
  • Loading branch information
osterman authored Jun 5, 2019
1 parent b1d5e52 commit 6c0d7b6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
45 changes: 45 additions & 0 deletions bin/terraform-docs.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
# https://github.com/segmentio/terraform-docs/
# https://github.com/segmentio/terraform-docs/issues/62

{
if ( /\{/ ) {
braceCnt++
}

if ( /\}/ ) {
braceCnt--
}

if ($0 ~ /(variable|output) "(.*?)"/) {
blockCnt++
print $0
}

if ($1 == "description") {
print $0
}

if ($1 == "default") {
if (braceCnt > 1) {
print " default = {}"
} else {
print $0
}
}

if ($1 == "type" ) {
type=$3
if (type ~ "object") {
print " type = \"object\""
} else {
print " type = \"" $3 "\""
}
}

if (braceCnt == 0 && blockCnt > 0) {
blockCnt--
print $0
}
}
14 changes: 14 additions & 0 deletions bin/terraform-docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

which awk 2>&1 >/dev/null || ( echo "awk not available"; exit 1)
which terraform 2>&1 >/dev/null || ( echo "terraform not available"; exit 1)
which terraform-docs 2>&1 >/dev/null || ( echo "terraform-docs not available"; exit 1)

if [[ "`terraform version | head -1`" =~ 0\.12 ]]; then
TMP_FILE="$(mktemp /tmp/terraform-docs-XXXXXXXXXX.tf)"
awk -f ${BUILD_HARNESS_PATH}/bin/terraform-docs.awk $2/*.tf > ${TMP_FILE}
terraform-docs $1 ${TMP_FILE}
rm -f ${TMP_FILE}
else
terraform-docs $1 $2
fi
2 changes: 1 addition & 1 deletion modules/docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ docs/targets.md: docs/deps
.PHONY : docs/terraform.md
## Update `docs/terraform.md` from `terraform-docs`
docs/terraform.md: docs/deps packages/install/terraform-docs
@terraform-docs md . > $@
@$(BUILD_HARNESS_PATH)/bin/terraform-docs.sh md . > $@

0 comments on commit 6c0d7b6

Please sign in to comment.