Skip to content

Commit

Permalink
Update terraform-docs.awk (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
aknysh authored Jun 28, 2019
1 parent 79dfc20 commit 6a4b2b3
Showing 1 changed file with 61 additions and 22 deletions.
83 changes: 61 additions & 22 deletions test/terraform/terraform-docs.awk
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 6a4b2b3

Please sign in to comment.