From 6ffc42a4b5c1f194b9feacfa33e308ea7ea569b4 Mon Sep 17 00:00:00 2001 From: cytopia Date: Sat, 2 Nov 2019 12:15:33 +0100 Subject: [PATCH] Fixes #65: terraform-docs should not fail if complex types contain 'description' keyword (#73) --- terraform_docs.sh | 165 +++++++++++++++++++++++++++++++++------------- 1 file changed, 120 insertions(+), 45 deletions(-) diff --git a/terraform_docs.sh b/terraform_docs.sh index 0935b69e6..958272eee 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -120,81 +120,156 @@ terraform_docs_awk() { braceCnt-- } + + # ---------------------------------------------------------------------------------------------- + # variable|output "..." { + # ---------------------------------------------------------------------------------------------- + # [END] variable/output block + if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt == 0) { + if (braceCnt == 0 && blockCnt > 0) { + blockCnt-- + print $0 + } + } # [START] variable or output block started if ($0 ~ /^[[:space:]]*(variable|output)[[:space:]][[:space:]]*"(.*?)"/) { - # Normalize the braceCnt (should be 1 now) + # Normalize the braceCnt and block (should be 1 now) braceCnt = 1 - # [CLOSE] "default" block - if (blockDefCnt > 0) { - blockDefCnt = 0 - } - blockCnt++ + blockCnt = 1 + # [CLOSE] "default" and "type" block + blockDefaultCnt = 0 + blockTypeCnt = 0 + # Print variable|output line print $0 } - # [START] multiline default statement started - if (blockCnt > 0) { + + # ---------------------------------------------------------------------------------------------- + # default = ... + # ---------------------------------------------------------------------------------------------- + # [END] multiline "default" continues/ends + if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt > 0) { + print $0 + # Count opening blocks + blockDefaultCnt += gsub(/\(/, "") + blockDefaultCnt += gsub(/\[/, "") + blockDefaultCnt += gsub(/\{/, "") + # Count closing blocks + blockDefaultCnt -= gsub(/\)/, "") + blockDefaultCnt -= gsub(/\]/, "") + blockDefaultCnt -= gsub(/\}/, "") + } + # [START] multiline "default" statement started + if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt == 0) { if ($0 ~ /^[[:space:]][[:space:]]*(default)[[:space:]][[:space:]]*=/) { if ($3 ~ "null") { print " default = \"null\"" } else { print $0 - blockDefCnt++ - blockDefStart=1 + # Count opening blocks + blockDefaultCnt += gsub(/\(/, "") + blockDefaultCnt += gsub(/\[/, "") + blockDefaultCnt += gsub(/\{/, "") + # Count closing blocks + blockDefaultCnt -= gsub(/\)/, "") + blockDefaultCnt -= gsub(/\]/, "") + blockDefaultCnt -= gsub(/\}/, "") } } } - # [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 - } - } - } - # [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") { + # ---------------------------------------------------------------------------------------------- + # type = ... + # ---------------------------------------------------------------------------------------------- + # [END] multiline "type" continues/ends + if (blockCnt > 0 && blockTypeCnt > 0 && blockDefaultCnt == 0) { + # The following 'print $0' would print multiline type definitions + #print $0 + # Count opening blocks + blockTypeCnt += gsub(/\(/, "") + blockTypeCnt += gsub(/\[/, "") + blockTypeCnt += gsub(/\{/, "") + # Count closing blocks + blockTypeCnt -= gsub(/\)/, "") + blockTypeCnt -= gsub(/\]/, "") + blockTypeCnt -= gsub(/\}/, "") + } + # [START] multiline "type" statement started + if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt == 0) { + if ($0 ~ /^[[:space:]][[:space:]]*(type)[[:space:]][[:space:]]*=/ ) { + if ($3 ~ "object") { print " type = \"object\"" } else { + # Convert multiline stuff into single line + if ($3 ~ /^[[:space:]]*list[[:space:]]*\([[:space:]]*$/) { + type = "list" + } else if ($3 ~ /^[[:space:]]*string[[:space:]]*\([[:space:]]*$/) { + type = "string" + } else if ($3 ~ /^[[:space:]]*map[[:space:]]*\([[:space:]]*$/) { + type = "map" + } else { + type = $3 + } + # legacy quoted types: "string", "list", and "map" - if ($3 ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) { - print " type = " $3 + if (type ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) { + print " type = " type } else { - print " type = \"" $3 "\"" + print " type = \"" type "\"" } } + # Count opening blocks + blockTypeCnt += gsub(/\(/, "") + blockTypeCnt += gsub(/\[/, "") + blockTypeCnt += gsub(/\{/, "") + # Count closing blocks + blockTypeCnt -= gsub(/\)/, "") + blockTypeCnt -= gsub(/\]/, "") + blockTypeCnt -= gsub(/\}/, "") } } - # [CLOSE] variable/output block - if (blockCnt > 0) { - if (braceCnt == 0 && blockCnt > 0) { - blockCnt-- + + # ---------------------------------------------------------------------------------------------- + # description = ... + # ---------------------------------------------------------------------------------------------- + # [PRINT] single line "description" + if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt == 0) { + if ($0 ~ /^[[:space:]][[:space:]]*description[[:space:]][[:space:]]*=/) { print $0 } } - # [PRINT] Multiline "default" statement - if (blockCnt > 0 && blockDefCnt > 0) { - if (blockDefStart == 1) { - blockDefStart = 0 - } else { - print $0 - } + + # ---------------------------------------------------------------------------------------------- + # value = ... + # ---------------------------------------------------------------------------------------------- + ## [PRINT] single line "value" + #if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt == 0) { + # if ($0 ~ /^[[:space:]][[:space:]]*value[[:space:]][[:space:]]*=/) { + # print $0 + # } + #} + + + # ---------------------------------------------------------------------------------------------- + # Newlines, comments, everything else + # ---------------------------------------------------------------------------------------------- + #if (blockTypeCnt == 0 && blockDefaultCnt == 0) { + # Comments with '#' + if ($0 ~ /^[[:space:]]*#/) { + print $0 + } + # Comments with '//' + if ($0 ~ /^[[:space:]]*\/\//) { + print $0 + } + # Newlines + if ($0 ~ /^[[:space:]]*$/) { + print $0 } + #} } EOF