Skip to content

Commit

Permalink
#152 Ensure terraform-docs 0.12 multi-line default statement is parse…
Browse files Browse the repository at this point in the history
…d correctly (#155)

* #152 Ensure terraform-docs 0.12 multi-line default statement is parsed correctly

* Fix indentation
  • Loading branch information
cytopia authored and osterman committed Jun 17, 2019
1 parent 6c0d7b6 commit 58733c4
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 deletions bin/terraform-docs.awk
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,76 @@
# https://github.com/segmentio/terraform-docs/issues/62

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

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

# [START] variable or output block started
if ($0 ~ /(variable|output) "(.*?)"/) {
# [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 ($1 == "default") {
print $0
if ($NF ~ /[\[\(\{]/) {
blockDefCnt++
blockDefStart=1
}
}
}

if ($1 == "default") {
if (braceCnt > 1) {
print " default = {}"
} else {
# [PRINT] single line "description"
if (blockDefCnt == 0) {
if ($1 == "description") {
# [CLOSE] "default" block
if (blockDefCnt > 0) {
blockDefCnt = 0
}
print $0
}
}

if ($1 == "type" ) {
type=$3
if (type ~ "object") {
print " type = \"object\""
} else {
print " type = \"" $3 "\""
# [PRINT] single line "type"
if (blockCnt > 0) {
if ($1 == "type" ) {
# [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
}
}
}

0 comments on commit 58733c4

Please sign in to comment.