Fixed awk script for terraform-docs (kudos @cytopia) and mktemp on Mac (closes #47, #48, #49)

This commit is contained in:
Anton Babenko 2019-06-18 13:58:49 +02:00
commit 10854fcfa2

View file

@ -30,10 +30,10 @@ main() {
if [[ "$hack_terraform_docs" == "1" ]]; then if [[ "$hack_terraform_docs" == "1" ]]; then
which awk 2>&1 >/dev/null || ( echo "awk is required for terraform-docs hack to work with Terraform 0.12"; exit 1) which awk 2>&1 >/dev/null || ( echo "awk is required for terraform-docs hack to work with Terraform 0.12"; exit 1)
TMP_AWK_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.awk)" tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}terraform-docs-XXXXXXXXXX")
terraform_docs_awk $TMP_AWK_FILE terraform_docs_awk "$tmp_file_awk"
terraform_docs "$TMP_AWK_FILE" "$args" "$files" terraform_docs "$tmp_file_awk" "$args" "$files"
rm -f "$TMP_AWK_FILE" rm -f "$tmp_file_awk"
else else
terraform_docs "0" "$args" "$files" terraform_docs "0" "$args" "$files"
fi fi
@ -78,10 +78,14 @@ terraform_docs() {
if [[ "$terraform_docs_awk_file" == "0" ]]; then if [[ "$terraform_docs_awk_file" == "0" ]]; then
terraform-docs $args md ./ > "$tmp_file" terraform-docs $args md ./ > "$tmp_file"
else else
TMP_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.tf)" # Can't append extension for mktemp, so renaming instead
awk -f "$terraform_docs_awk_file" ./*.tf > "$TMP_FILE" tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}terraform-docs-XXXXXXXXXX")
terraform-docs $args md "$TMP_FILE" > "$tmp_file" mv "$tmp_file_docs" "$tmp_file_docs.tf"
rm -f "$TMP_FILE" tmp_file_docs_tf="$tmp_file_docs.tf"
awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
terraform-docs $args md "$tmp_file_docs_tf" > "$tmp_file"
rm -f "$tmp_file_docs_tf"
fi fi
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834 # Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
@ -105,17 +109,21 @@ terraform_docs_awk() {
# https://github.com/segmentio/terraform-docs/ # https://github.com/segmentio/terraform-docs/
# https://github.com/segmentio/terraform-docs/issues/62 # https://github.com/segmentio/terraform-docs/issues/62
# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk
{ {
if ( /\{/ ) { if ( $0 ~ /\{/ ) {
braceCnt++ braceCnt++
} }
if ( /\}/ ) { if ( $0 ~ /\}/ ) {
braceCnt-- braceCnt--
} }
# [START] variable or output block started # [START] variable or output block started
if ($0 ~ /(variable|output) "(.*?)"/) { if ($0 ~ /^[[:space:]]*(variable|output)[[:space:]][[:space:]]*"(.*?)"/) {
# Normalize the braceCnt (should be 1 now)
braceCnt = 1
# [CLOSE] "default" block # [CLOSE] "default" block
if (blockDefCnt > 0) { if (blockDefCnt > 0) {
blockDefCnt = 0 blockDefCnt = 0
@ -126,9 +134,11 @@ terraform_docs_awk() {
# [START] multiline default statement started # [START] multiline default statement started
if (blockCnt > 0) { if (blockCnt > 0) {
if ($1 == "default") { if ($0 ~ /^[[:space:]][[:space:]]*(default)[[:space:]][[:space:]]*=/) {
print $0 if ($3 ~ "null") {
if ($NF ~ /[\[\(\{]/) { print " default = \"null\""
} else {
print $0
blockDefCnt++ blockDefCnt++
blockDefStart=1 blockDefStart=1
} }
@ -136,19 +146,21 @@ terraform_docs_awk() {
} }
# [PRINT] single line "description" # [PRINT] single line "description"
if (blockDefCnt == 0) { if (blockCnt > 0) {
if ($1 == "description") { if (blockDefCnt == 0) {
# [CLOSE] "default" block if ($0 ~ /^[[:space:]][[:space:]]*description[[:space:]][[:space:]]*=/) {
if (blockDefCnt > 0) { # [CLOSE] "default" block
blockDefCnt = 0 if (blockDefCnt > 0) {
blockDefCnt = 0
}
print $0
} }
print $0
} }
} }
# [PRINT] single line "type" # [PRINT] single line "type"
if (blockCnt > 0) { if (blockCnt > 0) {
if ($1 == "type" ) { if ($0 ~ /^[[:space:]][[:space:]]*type[[:space:]][[:space:]]*=/ ) {
# [CLOSE] "default" block # [CLOSE] "default" block
if (blockDefCnt > 0) { if (blockDefCnt > 0) {
blockDefCnt = 0 blockDefCnt = 0