diff --git a/test/terraform/terraform-docs.awk b/test/terraform/terraform-docs.awk index a627c09..5896464 100644 --- a/test/terraform/terraform-docs.awk +++ b/test/terraform/terraform-docs.awk @@ -4,43 +4,82 @@ # https://github.com/segmentio/terraform-docs/issues/62 { - if ( /\{/ ) { - braceCnt++ + if ( $0 ~ /\{/ ) { + braceCnt++ } - if ( /\}/ ) { - braceCnt-- + if ( $0 ~ /\}/ ) { + braceCnt-- } - if ($0 ~ /(variable|output) "(.*?)"/) { + # [START] variable or output block started + if ($0 ~ /^[[:space:]]*(variable|output)[[:space:]][[:space:]]*"(.*?)"/) { + # Normalize the braceCnt (should be 1 now) + braceCnt = 1 + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } blockCnt++ print $0 } - if ($1 == "description") { - print $0 + # [START] multiline default statement started + if (blockCnt > 0) { + if ($0 ~ /^[[:space:]][[:space:]]*(default)[[:space:]][[:space:]]*=/) { + if ($3 ~ "null") { + print " default = \"null\"" + } else { + print $0 + blockDefCnt++ + blockDefStart=1 + } + } } - if ($1 == "default") { - if (braceCnt > 1) { - print " default = {}" - } else { - print $0 + # [PRINT] single line "description" + if (blockCnt > 0) { + if (blockDefCnt == 0) { + if ($0 ~ /^[[:space:]][[:space:]]*description[[:space:]][[:space:]]*=/) { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + print $0 + } } } - if ($1 == "type" ) { - type=$3 - if (type ~ "object") { - print " type = \"object\"" - } else { - gsub(/"/, "", $3) - print " type = \"" $3 "\"" + # [PRINT] single line "type" + if (blockCnt > 0) { + if ($0 ~ /^[[:space:]][[:space:]]*type[[:space:]][[:space:]]*=/ ) { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + type=$3 + if (type ~ "object") { + print " type = \"object\"" + } else { + print " type = \"" $3 "\"" + } } } - if (braceCnt == 0 && blockCnt > 0) { - blockCnt-- - print $0 + # [CLOSE] variable/output block + if (blockCnt > 0) { + if (braceCnt == 0 && blockCnt > 0) { + blockCnt-- + print $0 + } + } + + # [PRINT] Multiline "default" statement + if (blockCnt > 0 && blockDefCnt > 0) { + if (blockDefStart == 1) { + blockDefStart = 0 + } else { + print $0 + } } }