Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for terraform-docs 0.8.0 with proper support for Terraform 0.12 syntax (bye-bye awk) #85

Merged
merged 12 commits into from
Jan 21, 2020
Merged
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<a name="unreleased"></a>
## [Unreleased]


- added support of different versions of terraform-docs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHANGELOG is automatically generated during release from the commit messages which is going to be like the title of this PR.

There is no need to write anything in CHANGELOG.md, because it will be overridden.


<a name="v1.22.0"></a>
## [v1.22.0] - 2020-01-13
Expand Down
36 changes: 19 additions & 17 deletions terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ main() {

local hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not enable hack_terraform_docs when terraform-docs is >= v0.8.0. This will allows us to maintain temporary 2 versions of terraform-docs.

@antonbabenko @konstantin-recurly Any thoughts ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It makes perfect sense to stay compatible for as long as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the logic and added the check for terraform-docs version.
It is checking if terraform-docs version is >= v0.8.0 also added a check if terraform-docs is installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some testing results:
terraform-docs v0.7.0

terraform-docs version
terraform-docs version v0.0.0- v0.7.0 darwin/amd64 BuildDate: 2019-12-15T00:53:30+0000

❯ pre-commit run -a
Terraform fmt............................................................Passed
Terraform docs...........................................................Passed

terraform-docs v0.8.0

❯ terraform-docs version
terraform-docs version v0.8.0-rc.2 d52122d darwin/amd64 BuildDate: 2020-01-12T20:38:24+0000

❯ pre-commit run -a
Terraform fmt............................................................Passed
Terraform docs...........................................................Failed
hookid: terraform_docs

Files were modified by this hook.

❯ pre-commit run -a
Terraform fmt............................................................Passed
Terraform docs...........................................................Passed

no terraform-docs installed

❯ terraform-docs version
zsh: command not found: terraform-docs

❯ pre-commit run -a
Terraform fmt............................................................Passed
Terraform docs...........................................................Failed
hookid: terraform_docs

terraform-docs is required


if [[ "$hack_terraform_docs" == "1" ]]; then
which terraform-docs 2>&1 >/dev/null || ( echo "terraform-docs is required"; exit 1)
local terraform_docs_version=$(terraform-docs version | head -1 | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can update to use grep -E as it is more used and egrep is there for historical reasons - https://unix.stackexchange.com/a/17950/70625


if [[ "$(check_terraform_docs_version "$terraform_docs_version")" == "1" ]]; then

terraform_docs "0" "$args" "$files"

elif [[ "$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)

tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
Expand All @@ -40,6 +47,17 @@ main() {

}

check_terraform_docs_version() {
readonly currentver="$1"

requiredver="0.8.0"
if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not critical, but try to follow this point from Shell Style Guide by Google - https://google.github.io/styleguide/shell.xml?showone=Test,_%5B_and_%5B%5B#Test,_%5B_and_%5B%5B

Please update to use [[ instead of [.

echo "1"
else
echo "0"
fi
}

terraform_docs() {
readonly terraform_docs_awk_file="$1"
readonly args="$2"
Expand Down Expand Up @@ -108,19 +126,14 @@ terraform_docs_awk() {
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
# https://github.com/segmentio/terraform-docs/
# 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 ( $0 ~ /\{/ ) {
braceCnt++
}

if ( $0 ~ /\}/ ) {
braceCnt--
}


# ----------------------------------------------------------------------------------------------
# variable|output "..." {
# ----------------------------------------------------------------------------------------------
Expand All @@ -142,8 +155,6 @@ terraform_docs_awk() {
# Print variable|output line
print $0
}


# ----------------------------------------------------------------------------------------------
# default = ...
# ----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -177,8 +188,6 @@ terraform_docs_awk() {
}
}
}


# ----------------------------------------------------------------------------------------------
# type = ...
# ----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -211,7 +220,6 @@ terraform_docs_awk() {
} else {
type = $3
}

# legacy quoted types: "string", "list", and "map"
if (type ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) {
print " type = " type
Expand All @@ -229,8 +237,6 @@ terraform_docs_awk() {
blockTypeCnt -= gsub(/\}/, "")
}
}


# ----------------------------------------------------------------------------------------------
# description = ...
# ----------------------------------------------------------------------------------------------
Expand All @@ -240,8 +246,6 @@ terraform_docs_awk() {
print $0
}
}


# ----------------------------------------------------------------------------------------------
# value = ...
# ----------------------------------------------------------------------------------------------
Expand All @@ -251,8 +255,6 @@ terraform_docs_awk() {
# print $0
# }
#}


# ----------------------------------------------------------------------------------------------
# Newlines, comments, everything else
# ----------------------------------------------------------------------------------------------
Expand Down