From 6c0d7b6387325b3db24bf54b2fc7ed0dfb21571e Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 4 Jun 2019 18:42:24 -0700 Subject: [PATCH] 0.12 support for terraform-docs (#152) * 0.12 support for terraform-docs * portability fix * fix version check --- bin/terraform-docs.awk | 45 ++++++++++++++++++++++++++++++++++++++++++ bin/terraform-docs.sh | 14 +++++++++++++ modules/docs/Makefile | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 bin/terraform-docs.awk create mode 100755 bin/terraform-docs.sh diff --git a/bin/terraform-docs.awk b/bin/terraform-docs.awk new file mode 100644 index 00000000..43e735ed --- /dev/null +++ b/bin/terraform-docs.awk @@ -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 + } +} diff --git a/bin/terraform-docs.sh b/bin/terraform-docs.sh new file mode 100755 index 00000000..388e7a16 --- /dev/null +++ b/bin/terraform-docs.sh @@ -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 diff --git a/modules/docs/Makefile b/modules/docs/Makefile index d2477bac..6e63a4c4 100644 --- a/modules/docs/Makefile +++ b/modules/docs/Makefile @@ -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 . > $@