From fbdd40ac8f8ebbcaecc1f90e762de753b1aab79c Mon Sep 17 00:00:00 2001 From: Martin Etmajer Date: Tue, 13 Nov 2018 12:30:06 +0100 Subject: [PATCH 01/37] Add feature to pass options to terraform-docs. --- terraform_docs.sh | 579 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 552 insertions(+), 27 deletions(-) diff --git a/terraform_docs.sh b/terraform_docs.sh index 781973aa7..1bd79627a 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -1,45 +1,570 @@ #!/usr/bin/env bash + set -e -declare -a paths -declare -a tfvars_files +main() { + declare argv + argv=$(getopt -o a: --long args: -- "$@") || return + eval "set -- $argv" + + declare args + declare files + + for argv; do + case $argv in + (-a|--args) + shift + args="$1" + shift + ;; + (--) + shift + files="$@" + break + ;; + esac + done + + terraform_docs "$args" "$files" +} + +terraform_docs() { + readonly args="$1" + readonly files="$2" + + declare -a paths + declare -a tfvars_files + + index=0 + + for file_with_path in $files; do + file_with_path="${file_with_path// /__REPLACED__SPACE__}" -index=0 + paths[index]=$(dirname "$file_with_path") -for file_with_path in "$@"; do - file_with_path="${file_with_path// /__REPLACED__SPACE__}" + if [[ "$file_with_path" == *".tfvars" ]]; then + tfvars_files+=("$file_with_path") + fi - paths[index]=$(dirname "$file_with_path") + ((index+=1)) + done - if [[ "$file_with_path" == *".tfvars" ]]; then - tfvars_files+=("$file_with_path") - fi + readonly tmp_file=$(mktemp) + readonly text_file="README.md" - ((index+=1)) -done + for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do + path_uniq="${path_uniq//__REPLACED__SPACE__/ }" -readonly tmp_file=$(mktemp) -readonly text_file="README.md" + pushd "$path_uniq" > /dev/null -for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do - path_uniq="${path_uniq//__REPLACED__SPACE__/ }" + if [[ ! -f "$text_file" ]]; then + popd > /dev/null + continue + fi - pushd "$path_uniq" > /dev/null + terraform-docs $args md ./ > "$tmp_file" + + # Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834 + perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file" + + # Replace placeholder with the content of the file + perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", ; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file" + + rm -f "$tmp_file" - if [[ ! -f "$text_file" ]]; then popd > /dev/null - continue - fi + done +} + +getopt() { + # pure-getopt, a drop-in replacement for GNU getopt in pure Bash. + # version 1.4.3 + # + # Copyright 2012-2018 Aron Griffis + # + # Permission is hereby granted, free of charge, to any person obtaining + # a copy of this software and associated documentation files (the + # "Software"), to deal in the Software without restriction, including + # without limitation the rights to use, copy, modify, merge, publish, + # distribute, sublicense, and/or sell copies of the Software, and to + # permit persons to whom the Software is furnished to do so, subject to + # the following conditions: + # + # The above copyright notice and this permission notice shall be included + # in all copies or substantial portions of the Software. + # + # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + _getopt_main() { + # Returns one of the following statuses: + # 0 success + # 1 error parsing parameters + # 2 error in getopt invocation + # 3 internal error + # 4 reserved for -T + # + # For statuses 0 and 1, generates normalized and shell-quoted + # "options -- parameters" on stdout. + + declare parsed status + declare short long name flags + declare have_short=false + + # Synopsis from getopt man-page: + # + # getopt optstring parameters + # getopt [options] [--] optstring parameters + # getopt [options] -o|--options optstring [options] [--] parameters + # + # The first form can be normalized to the third form which + # _getopt_parse() understands. The second form can be recognized after + # first parse when $short hasn't been set. + + if [[ -n ${GETOPT_COMPATIBLE+isset} || $1 == [^-]* ]]; then + # Enable compatibility mode + flags=c$flags + # Normalize first to third synopsis form + set -- -o "$1" -- "${@:2}" + fi + + # First parse always uses flags=p since getopt always parses its own + # arguments effectively in this mode. + parsed=$(_getopt_parse getopt ahl:n:o:qQs:TuV \ + alternative,help,longoptions:,name:,options:,quiet,quiet-output,shell:,test,version \ + p "$@") + status=$? + if [[ $status != 0 ]]; then + if [[ $status == 1 ]]; then + echo "Try \`getopt --help' for more information." >&2 + # Since this is the first parse, convert status 1 to 2 + status=2 + fi + return $status + fi + eval "set -- $parsed" + + while [[ $# -gt 0 ]]; do + case $1 in + (-a|--alternative) + flags=a$flags ;; + + (-h|--help) + _getopt_help + return 2 # as does GNU getopt + ;; + + (-l|--longoptions) + long="$long${long:+,}$2" + shift ;; + + (-n|--name) + name=$2 + shift ;; + + (-o|--options) + short=$2 + have_short=true + shift ;; + + (-q|--quiet) + flags=q$flags ;; + + (-Q|--quiet-output) + flags=Q$flags ;; + + (-s|--shell) + case $2 in + (sh|bash) + flags=${flags//t/} ;; + (csh|tcsh) + flags=t$flags ;; + (*) + echo 'getopt: unknown shell after -s or --shell argument' >&2 + echo "Try \`getopt --help' for more information." >&2 + return 2 ;; + esac + shift ;; + + (-u|--unquoted) + flags=u$flags ;; + + (-T|--test) + return 4 ;; + + (-V|--version) + echo "pure-getopt 1.4.3" + return 0 ;; + + (--) + shift + break ;; + esac + + shift + done + + if ! $have_short; then + # $short was declared but never set, not even to an empty string. + # This implies the second form in the synopsis. + if [[ $# == 0 ]]; then + echo 'getopt: missing optstring argument' >&2 + echo "Try \`getopt --help' for more information." >&2 + return 2 + fi + short=$1 + have_short=true + shift + fi + + if [[ $short == -* ]]; then + # Leading dash means generate output in place rather than reordering, + # unless we're already in compatibility mode. + [[ $flags == *c* ]] || flags=i$flags + short=${short#?} + elif [[ $short == +* ]]; then + # Leading plus means POSIXLY_CORRECT, unless we're already in + # compatibility mode. + [[ $flags == *c* ]] || flags=p$flags + short=${short#?} + fi + + # This should fire if POSIXLY_CORRECT is in the environment, even if + # it's an empty string. That's the difference between :+ and + + flags=${POSIXLY_CORRECT+p}$flags + + _getopt_parse "${name:-getopt}" "$short" "$long" "$flags" "$@" + } + + _getopt_parse() { + # Inner getopt parser, used for both first parse and second parse. + # Returns 0 for success, 1 for error parsing, 3 for internal error. + # In the case of status 1, still generates stdout with whatever could + # be parsed. + # + # $flags is a string of characters with the following meanings: + # a - alternative parsing mode + # c - GETOPT_COMPATIBLE + # i - generate output in place rather than reordering + # p - POSIXLY_CORRECT + # q - disable error reporting + # Q - disable normal output + # t - quote for csh/tcsh + # u - unquoted output + + declare name="$1" short="$2" long="$3" flags="$4" + shift 4 + + # Split $long on commas, prepend double-dashes, strip colons; + # for use with _getopt_resolve_abbrev + declare -a longarr + _getopt_split longarr "$long" + longarr=( "${longarr[@]/#/--}" ) + longarr=( "${longarr[@]%:}" ) + longarr=( "${longarr[@]%:}" ) + + # Parse and collect options and parameters + declare -a opts params + declare o alt_recycled=false error=0 + + while [[ $# -gt 0 ]]; do + case $1 in + (--) + params=( "${params[@]}" "${@:2}" ) + break ;; + + (--*=*) + o=${1%%=*} + if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then + error=1 + elif [[ ,"$long", == *,"${o#--}"::,* ]]; then + opts=( "${opts[@]}" "$o" "${1#*=}" ) + elif [[ ,"$long", == *,"${o#--}":,* ]]; then + opts=( "${opts[@]}" "$o" "${1#*=}" ) + elif [[ ,"$long", == *,"${o#--}",* ]]; then + if $alt_recycled; then o=${o#-}; fi + _getopt_err "$name: option '$o' doesn't allow an argument" + error=1 + else + echo "getopt: assertion failed (1)" >&2 + return 3 + fi + alt_recycled=false + ;; + + (--?*) + o=$1 + if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then + error=1 + elif [[ ,"$long", == *,"${o#--}",* ]]; then + opts=( "${opts[@]}" "$o" ) + elif [[ ,"$long", == *,"${o#--}::",* ]]; then + opts=( "${opts[@]}" "$o" '' ) + elif [[ ,"$long", == *,"${o#--}:",* ]]; then + if [[ $# -ge 2 ]]; then + shift + opts=( "${opts[@]}" "$o" "$1" ) + else + if $alt_recycled; then o=${o#-}; fi + _getopt_err "$name: option '$o' requires an argument" + error=1 + fi + else + echo "getopt: assertion failed (2)" >&2 + return 3 + fi + alt_recycled=false + ;; + + (-*) + if [[ $flags == *a* ]]; then + # Alternative parsing mode! + # Try to handle as a long option if any of the following apply: + # 1. There's an equals sign in the mix -x=3 or -xy=3 + # 2. There's 2+ letters and an abbreviated long match -xy + # 3. There's a single letter and an exact long match + # 4. There's a single letter and no short match + o=${1::2} # temp for testing #4 + if [[ $1 == *=* || $1 == -?? || \ + ,$long, == *,"${1#-}"[:,]* || \ + ,$short, != *,"${o#-}"[:,]* ]]; then + o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2>/dev/null) + case $? in + (0) + # Unambiguous match. Let the long options parser handle + # it, with a flag to get the right error message. + set -- "-$1" "${@:2}" + alt_recycled=true + continue ;; + (1) + # Ambiguous match, generate error and continue. + _getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" >/dev/null + error=1 + shift + continue ;; + (2) + # No match, fall through to single-character check. + true ;; + (*) + echo "getopt: assertion failed (3)" >&2 + return 3 ;; + esac + fi + fi + + o=${1::2} + if [[ "$short" == *"${o#-}"::* ]]; then + if [[ ${#1} -gt 2 ]]; then + opts=( "${opts[@]}" "$o" "${1:2}" ) + else + opts=( "${opts[@]}" "$o" '' ) + fi + elif [[ "$short" == *"${o#-}":* ]]; then + if [[ ${#1} -gt 2 ]]; then + opts=( "${opts[@]}" "$o" "${1:2}" ) + elif [[ $# -ge 2 ]]; then + shift + opts=( "${opts[@]}" "$o" "$1" ) + else + _getopt_err "$name: option requires an argument -- '${o#-}'" + error=1 + fi + elif [[ "$short" == *"${o#-}"* ]]; then + opts=( "${opts[@]}" "$o" ) + if [[ ${#1} -gt 2 ]]; then + set -- "$o" "-${1:2}" "${@:2}" + fi + else + if [[ $flags == *a* ]]; then + # Alternative parsing mode! Report on the entire failed + # option. GNU includes =value but we omit it for sanity with + # very long values. + _getopt_err "$name: unrecognized option '${1%%=*}'" + else + _getopt_err "$name: invalid option -- '${o#-}'" + if [[ ${#1} -gt 2 ]]; then + set -- "$o" "-${1:2}" "${@:2}" + fi + fi + error=1 + fi ;; + + (*) + # GNU getopt in-place mode (leading dash on short options) + # overrides POSIXLY_CORRECT + if [[ $flags == *i* ]]; then + opts=( "${opts[@]}" "$1" ) + elif [[ $flags == *p* ]]; then + params=( "${params[@]}" "$@" ) + break + else + params=( "${params[@]}" "$1" ) + fi + esac + + shift + done + + if [[ $flags == *Q* ]]; then + true # generate no output + else + echo -n ' ' + if [[ $flags == *[cu]* ]]; then + printf '%s -- %s' "${opts[*]}" "${params[*]}" + else + if [[ $flags == *t* ]]; then + _getopt_quote_csh "${opts[@]}" -- "${params[@]}" + else + _getopt_quote "${opts[@]}" -- "${params[@]}" + fi + fi + echo + fi + + return $error + } + + _getopt_err() { + if [[ $flags != *q* ]]; then + printf '%s\n' "$1" >&2 + fi + } + + _getopt_resolve_abbrev() { + # Resolves an abbrevation from a list of possibilities. + # If the abbreviation is unambiguous, echoes the expansion on stdout + # and returns 0. If the abbreviation is ambiguous, prints a message on + # stderr and returns 1. (For first parse this should convert to exit + # status 2.) If there is no match at all, prints a message on stderr + # and returns 2. + declare a q="$1" + declare -a matches + shift + for a; do + if [[ $q == "$a" ]]; then + # Exact match. Squash any other partial matches. + matches=( "$a" ) + break + elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q" ]]; then + # Exact alternative match. Squash any other partial matches. + matches=( "$a" ) + break + elif [[ $a == "$q"* ]]; then + # Abbreviated match. + matches=( "${matches[@]}" "$a" ) + elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q"* ]]; then + # Abbreviated alternative match. + matches=( "${matches[@]}" "${a#-}" ) + fi + done + case ${#matches[@]} in + (0) + [[ $flags == *q* ]] || \ + printf "$name: unrecognized option %s\\n" >&2 \ + "$(_getopt_quote "$q")" + return 2 ;; + (1) + printf '%s' "${matches[0]}"; return 0 ;; + (*) + [[ $flags == *q* ]] || \ + printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \ + "$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" + return 1 ;; + esac + } + + _getopt_split() { + # Splits $2 at commas to build array specified by $1 + declare IFS=, + eval "$1=( \$2 )" + } + + _getopt_quote() { + # Quotes arguments with single quotes, escaping inner single quotes + declare s space q=\' + for s; do + printf "$space'%s'" "${s//$q/$q\\$q$q}" + space=' ' + done + } + + _getopt_quote_csh() { + # Quotes arguments with single quotes, escaping inner single quotes, + # bangs, backslashes and newlines + declare s i c space + for s; do + echo -n "$space'" + for ((i=0; i<${#s}; i++)); do + c=${s:i:1} + case $c in + (\\|\'|!) + echo -n "'\\$c'" ;; + ($'\n') + echo -n "\\$c" ;; + (*) + echo -n "$c" ;; + esac + done + echo -n \' + space=' ' + done + } + + _getopt_help() { + cat <<-EOT >&2 + + Usage: + getopt + getopt [options] [--] + getopt [options] -o|--options [options] [--] + + Parse command options. + + Options: + -a, --alternative allow long options starting with single - + -l, --longoptions the long options to be recognized + -n, --name the name under which errors are reported + -o, --options the short options to be recognized + -q, --quiet disable error reporting by getopt(3) + -Q, --quiet-output no normal output + -s, --shell set quoting conventions to those of + -T, --test test for getopt(1) version + -u, --unquoted do not quote the output + + -h, --help display this help and exit + -V, --version output version information and exit + + For more details see getopt(1). + EOT + } - terraform-docs md ./ > "$tmp_file" + _getopt_version_check() { + if [[ -z $BASH_VERSION ]]; then + echo "getopt: unknown version of bash might not be compatible" >&2 + return 1 + 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 - perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file" + # This is a lexical comparison that should be sufficient forever. + if [[ $BASH_VERSION < 2.05b ]]; then + echo "getopt: bash $BASH_VERSION might not be compatible" >&2 + return 1 + fi - # Replace placeholder with the content of the file - perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", ; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$text_file" + return 0 + } - rm -f "$tmp_file" + _getopt_version_check + _getopt_main "$@" + declare status=$? + unset -f _getopt_main _getopt_err _getopt_parse _getopt_quote \ + _getopt_quote_csh _getopt_resolve_abbrev _getopt_split _getopt_help \ + _getopt_version_check + return $status +} - popd > /dev/null -done +[[ $BASH_SOURCE != "$0" ]] || main "$@" From e2760caf8d70875ca85d25b11a972e1f207217f8 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 11 Dec 2018 20:21:49 +0100 Subject: [PATCH 02/37] Added followup after #25 --- .pre-commit-config.yaml | 2 +- .pre-commit-hooks.yaml | 9 ++++++ README.md | 71 +++++++++++++++++++++++++++++++++-------- hooks.yaml | 9 ++++++ 4 files changed, 77 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 70a21ad98..1c444a95a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: git://github.com/pre-commit/pre-commit-hooks - rev: v1.2.3 + rev: v2.0.0 hooks: - id: check-yaml - id: end-of-file-fixer diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 2fda38ba1..65436d109 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -10,6 +10,15 @@ name: Terraform docs description: Inserts input and output documentation into README.md (using terraform-docs). entry: terraform_docs.sh + args: [--args=--with-aggregate-type-defaults] + language: script + files: (\.tf)$ + exclude: \.terraform\/.*$ + +- id: terraform_docs_without_aggregate_type_defaults + name: Terraform docs (without aggregate type defaults) + description: Inserts input and output documentation into README.md (using terraform-docs). + entry: terraform_docs.sh language: script files: (\.tf)$ exclude: \.terraform\/.*$ diff --git a/README.md b/README.md index ce242a35c..e3e7699ef 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,74 @@ -# pre-commit-terraform hook +# Collection of git hooks for Terraform to be used with [pre-commit framework](http://pre-commit.com/) [![Github tag](https://img.shields.io/github/tag/antonbabenko/pre-commit-terraform.svg)](https://github.com/antonbabenko/pre-commit-terraform/releases) ![](https://img.shields.io/maintenance/yes/2018.svg) [![Help Contribute to Open Source](https://www.codetriage.com/antonbabenko/pre-commit-terraform/badges/users.svg)](https://www.codetriage.com/antonbabenko/pre-commit-terraform) -Several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) in a good shape: -* `terraform_fmt` - Rewrites all Terraform configuration files to a canonical format. -* `terraform_validate_no_variables` - Validates all Terraform configuration files without checking whether all required variables were set. -* `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. -* `terraform_docs` - Inserts input and output documentation into `README.md`. +## How to install -## Notes about hooks +### Step 1 -1. `terraform_validate_no_variables` and `terraform_validate_with_variables` will not work if variables are being set dynamically (eg, when using [Terragrunt](https://github.com/gruntwork-io/terragrunt)). Use `terragrunt validate` command instead. +On MacOSX install the pre-commit package + +```bash +brew install pre-commit +``` -1. `terraform_docs` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. +For other operating systems check the [official documentation](http://pre-commit.com/#install) -## Example +### Step 2 -`.pre-commit-config.yaml`: +Step into the repository you want to have the pre-commit hooks installed and run: -```yaml +```bash +cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.7.3 + rev: v1.7.4 hooks: - id: terraform_fmt - id: terraform_docs +EOF +``` + +### Step 3 + +Install the pre-commit hook + +```bash +pre-commit install +``` + +### Step 4 + +After pre-commit hook has been installed you can run it manually on all files in the repository + +```bash +pre-commit run -a ``` +## Available Hooks + +There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) in a good shape: +* `terraform_fmt` - Rewrites all Terraform configuration files to a canonical format. +* `terraform_validate_no_variables` - Validates all Terraform configuration files without checking whether all required variables were set. +* `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. +* `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. +* `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. + +Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. + +## Notes about hooks + +1. `terraform_validate_no_variables` and `terraform_validate_with_variables` will not work if variables are being set dynamically (eg, when using [Terragrunt](https://github.com/gruntwork-io/terragrunt)). Use `terragrunt validate` command instead. + +1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. + +1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. + Enjoy the clean and documented code! + +## Authors + +This repository is managed by [Anton Babenko](https://github.com/antonbabenko) with help from [these awesome contributors](https://github.com/antonbabenko/pre-commit-terraform/graphs/contributors). + +## License + +MIT licensed. See LICENSE for full details. diff --git a/hooks.yaml b/hooks.yaml index 2fda38ba1..65436d109 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -10,6 +10,15 @@ name: Terraform docs description: Inserts input and output documentation into README.md (using terraform-docs). entry: terraform_docs.sh + args: [--args=--with-aggregate-type-defaults] + language: script + files: (\.tf)$ + exclude: \.terraform\/.*$ + +- id: terraform_docs_without_aggregate_type_defaults + name: Terraform docs (without aggregate type defaults) + description: Inserts input and output documentation into README.md (using terraform-docs). + entry: terraform_docs.sh language: script files: (\.tf)$ exclude: \.terraform\/.*$ From 9aa971c069da7ef66f0a30075e6b68f21f243059 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Thu, 13 Dec 2018 22:16:01 -0500 Subject: [PATCH 03/37] Add new hook for running terraform-docs with replacing README.md from doc in main.tf --- .pre-commit-hooks.yaml | 8 ++++ README.md | 10 +++++ hooks.yaml | 40 ----------------- pre_commit_hooks/__init__.py | 0 pre_commit_hooks/terraform_docs_replace.py | 50 ++++++++++++++++++++++ setup.py | 34 +++++++++++++++ 6 files changed, 102 insertions(+), 40 deletions(-) delete mode 100644 hooks.yaml create mode 100644 pre_commit_hooks/__init__.py create mode 100644 pre_commit_hooks/terraform_docs_replace.py create mode 100644 setup.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 65436d109..64f2ad8ef 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -23,6 +23,14 @@ files: (\.tf)$ exclude: \.terraform\/.*$ +- id: terraform_docs_replace + name: Generate documentation for Terraform modules + language: python + entry: terraform_docs_replace + files: (\.tf)$ + exclude: \.terraform\/.*$ + description: Generates README.md files for Terraform modules + - id: terraform_validate_no_variables name: Terraform validate without variables description: Validates all Terraform configuration files without checking whether all required variables were set (basic check). diff --git a/README.md b/README.md index e3e7699ef..ab1d44df0 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform c * `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. +* `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. @@ -61,6 +62,15 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. +1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. + + 1. Example: + ```yaml + hooks: + - id: terraform_docs_replace + args: ['--with-aggregate-type-defaults', '--sort-inputs-by-required'] + ``` + 1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. Enjoy the clean and documented code! diff --git a/hooks.yaml b/hooks.yaml deleted file mode 100644 index 65436d109..000000000 --- a/hooks.yaml +++ /dev/null @@ -1,40 +0,0 @@ -- id: terraform_fmt - name: Terraform fmt - description: Rewrites all Terraform configuration files to a canonical format. - entry: terraform_fmt.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ - -- id: terraform_docs - name: Terraform docs - description: Inserts input and output documentation into README.md (using terraform-docs). - entry: terraform_docs.sh - args: [--args=--with-aggregate-type-defaults] - language: script - files: (\.tf)$ - exclude: \.terraform\/.*$ - -- id: terraform_docs_without_aggregate_type_defaults - name: Terraform docs (without aggregate type defaults) - description: Inserts input and output documentation into README.md (using terraform-docs). - entry: terraform_docs.sh - language: script - files: (\.tf)$ - exclude: \.terraform\/.*$ - -- id: terraform_validate_no_variables - name: Terraform validate without variables - description: Validates all Terraform configuration files without checking whether all required variables were set (basic check). - entry: terraform_validate_no_variables.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ - -- id: terraform_validate_with_variables - name: Terraform validate with variables - description: Validates all Terraform configuration files and checks whether all required variables were specified. - entry: terraform_validate_with_variables.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ diff --git a/pre_commit_hooks/__init__.py b/pre_commit_hooks/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pre_commit_hooks/terraform_docs_replace.py b/pre_commit_hooks/terraform_docs_replace.py new file mode 100644 index 000000000..31fbd1acd --- /dev/null +++ b/pre_commit_hooks/terraform_docs_replace.py @@ -0,0 +1,50 @@ +import argparse +import os +import subprocess +import sys + + +def main(argv=None): + parser = argparse.ArgumentParser( + description="""Run terraform-docs on a set of files. Follows the standard convention of + pulling the documentation from main.tf in order to replace the entire + README.md file each time.""" + ) + parser.add_argument( + '--sort-inputs-by-required', dest='sort', action='store_true', + ) + parser.add_argument( + '--with-aggregate-type-defaults', dest='aggregate', action='store_true', + ) + parser.add_argument('filenames', nargs='*', help='Filenames to check.') + args = parser.parse_args(argv) + + dirs = [] + for filename in args.filenames: + if os.path.realpath(filename) not in dirs: + dirs.append(os.path.dirname(filename)) + + retval = 0 + + for dir in dirs: + try: + procArgs = [] + procArgs.append('terraform-docs') + if args.sort: + procArgs.append('--sort-inputs-by-required') + if args.aggregate: + procArgs.append('--with-aggregate-type-defaults') + procArgs.append('md') + procArgs.append(dir) + procArgs.append("| sed -e '$ d' -e 'N;/^\\n$/D;P;D'") + procArgs.append('>') + procArgs.append('{}/README.md'.format(dir)) + subprocess.check_call(" ".join(procArgs), shell=True) + except subprocess.CalledProcessError as e: + print(e) + retval = 1 + return retval + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..0b2d6e378 --- /dev/null +++ b/setup.py @@ -0,0 +1,34 @@ +from setuptools import find_packages +from setuptools import setup + + +setup( + name='pre-commit-terraform', + description='Pre-commit hooks for Terraform', + url='https://github.com/antonbabenko/pre-commit-terraform', + version_format='{tag}+{gitsha}', + + author='Andrew Roth', + author_email='roth.andy@gmail.com', + + classifiers=[ + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy', + ], + + packages=find_packages(exclude=('tests*', 'testing*')), + install_requires=[ + 'setuptools-git-version', + ], + entry_points={ + 'console_scripts': [ + 'terraform_docs_replace = pre_commit_hooks.terraform_docs_replace:main', + ], + }, +) From debe93a82be27d9dab99858372a33cdde196a320 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 09:23:54 -0500 Subject: [PATCH 04/37] Address requested changes --- .pre-commit-hooks.yaml | 4 ++-- setup.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 64f2ad8ef..e83763601 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -24,12 +24,12 @@ exclude: \.terraform\/.*$ - id: terraform_docs_replace - name: Generate documentation for Terraform modules + name: Terraform docs (overwrite README.md) language: python entry: terraform_docs_replace files: (\.tf)$ exclude: \.terraform\/.*$ - description: Generates README.md files for Terraform modules + description: Overwrite content of README.md with terraform-docs - id: terraform_validate_no_variables name: Terraform validate without variables diff --git a/setup.py b/setup.py index 0b2d6e378..44ae516ea 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='pre-commit-terraform', - description='Pre-commit hooks for Terraform', + description='Pre-commit hooks for terraform_docs_replace, url='https://github.com/antonbabenko/pre-commit-terraform', version_format='{tag}+{gitsha}', From cbd26b20c7dcae9b15ccecc7bf76a5e1c0ffdc81 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 10:45:59 -0500 Subject: [PATCH 05/37] Add `--dest` argument --- README.md | 4 ++-- pre_commit_hooks/terraform_docs_replace.py | 9 +++++++-- setup.py | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ab1d44df0..84ed48114 100644 --- a/README.md +++ b/README.md @@ -62,13 +62,13 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. -1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. +1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets your change the name of the file that gets created/modified 1. Example: ```yaml hooks: - id: terraform_docs_replace - args: ['--with-aggregate-type-defaults', '--sort-inputs-by-required'] + args: ['--with-aggregate-type-defaults', '--sort-inputs-by-required', '--dest=TEST.md'] ``` 1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. diff --git a/pre_commit_hooks/terraform_docs_replace.py b/pre_commit_hooks/terraform_docs_replace.py index 31fbd1acd..7e3bc2fb8 100644 --- a/pre_commit_hooks/terraform_docs_replace.py +++ b/pre_commit_hooks/terraform_docs_replace.py @@ -10,6 +10,9 @@ def main(argv=None): pulling the documentation from main.tf in order to replace the entire README.md file each time.""" ) + parser.add_argument( + '--dest', dest='dest', default='README.md', + ) parser.add_argument( '--sort-inputs-by-required', dest='sort', action='store_true', ) @@ -21,7 +24,9 @@ def main(argv=None): dirs = [] for filename in args.filenames: - if os.path.realpath(filename) not in dirs: + if (os.path.realpath(filename) not in dirs and \ + len(os.path.realpath(filename).strip()) > 0 and \ + (filename.endswith(".tf") or filename.endswith(".tfvars"))): dirs.append(os.path.dirname(filename)) retval = 0 @@ -38,7 +43,7 @@ def main(argv=None): procArgs.append(dir) procArgs.append("| sed -e '$ d' -e 'N;/^\\n$/D;P;D'") procArgs.append('>') - procArgs.append('{}/README.md'.format(dir)) + procArgs.append("./{dir}/{dest}".format(dir=dir,dest=args.dest)) subprocess.check_call(" ".join(procArgs), shell=True) except subprocess.CalledProcessError as e: print(e) diff --git a/setup.py b/setup.py index 44ae516ea..246de8c2b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='pre-commit-terraform', - description='Pre-commit hooks for terraform_docs_replace, + description='Pre-commit hooks for terraform_docs_replace', url='https://github.com/antonbabenko/pre-commit-terraform', version_format='{tag}+{gitsha}', From d3fe87daeac07704ae27e02e87df719dd7c9ac9f Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 16:16:42 -0500 Subject: [PATCH 06/37] Address requested changes --- README.md | 8 +++++++- setup.py | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 84ed48114..1bceb9517 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. -1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets your change the name of the file that gets created/modified +1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets your change the name of the file that gets created/modified. 1. Example: ```yaml @@ -73,6 +73,12 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. +## Notes for developers + +1. Python hooks are supported now too. All you have to do is: + 1. add a line to the `console_sripts` array in `entry_points` in `setup.py` + 1. Put your python script in the `pre_commit_hooks` folder + Enjoy the clean and documented code! ## Authors diff --git a/setup.py b/setup.py index 246de8c2b..4c2b47668 100644 --- a/setup.py +++ b/setup.py @@ -8,8 +8,7 @@ url='https://github.com/antonbabenko/pre-commit-terraform', version_format='{tag}+{gitsha}', - author='Andrew Roth', - author_email='roth.andy@gmail.com', + author='Contributors', classifiers=[ 'License :: OSI Approved :: MIT License', From fe3ba02d25e3c7baa7f0fd3f97aaf3f2c3db62c6 Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 16:19:54 -0500 Subject: [PATCH 07/37] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bceb9517..5e114729d 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. -1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets your change the name of the file that gets created/modified. +1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets you change the name of the file that gets created/modified. 1. Example: ```yaml From 15c9f394d32708ad3d2735af9c73cb03a6bad1af Mon Sep 17 00:00:00 2001 From: rothandrew Date: Fri, 14 Dec 2018 17:03:14 -0500 Subject: [PATCH 08/37] Fix bug not letting terraform_docs_replace work in the root directory of a repo --- pre_commit_hooks/terraform_docs_replace.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pre_commit_hooks/terraform_docs_replace.py b/pre_commit_hooks/terraform_docs_replace.py index 7e3bc2fb8..05fec622e 100644 --- a/pre_commit_hooks/terraform_docs_replace.py +++ b/pre_commit_hooks/terraform_docs_replace.py @@ -25,7 +25,6 @@ def main(argv=None): dirs = [] for filename in args.filenames: if (os.path.realpath(filename) not in dirs and \ - len(os.path.realpath(filename).strip()) > 0 and \ (filename.endswith(".tf") or filename.endswith(".tfvars"))): dirs.append(os.path.dirname(filename)) @@ -40,7 +39,7 @@ def main(argv=None): if args.aggregate: procArgs.append('--with-aggregate-type-defaults') procArgs.append('md') - procArgs.append(dir) + procArgs.append("./{dir}".format(dir=dir)) procArgs.append("| sed -e '$ d' -e 'N;/^\\n$/D;P;D'") procArgs.append('>') procArgs.append("./{dir}/{dest}".format(dir=dir,dest=args.dest)) From 57d924d5d4621d6e2635c8daea931b657f66f050 Mon Sep 17 00:00:00 2001 From: Chris Gilmer Date: Fri, 8 Feb 2019 15:24:06 -0800 Subject: [PATCH 09/37] Require terraform-docs runs in serial to avoid pre-commit doing parallel operations on similar file paths --- .pre-commit-hooks.yaml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index e83763601..afb5d6066 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -9,6 +9,7 @@ - id: terraform_docs name: Terraform docs description: Inserts input and output documentation into README.md (using terraform-docs). + require_serial: true entry: terraform_docs.sh args: [--args=--with-aggregate-type-defaults] language: script @@ -18,6 +19,7 @@ - id: terraform_docs_without_aggregate_type_defaults name: Terraform docs (without aggregate type defaults) description: Inserts input and output documentation into README.md (using terraform-docs). + require_serial: true entry: terraform_docs.sh language: script files: (\.tf)$ @@ -25,11 +27,12 @@ - id: terraform_docs_replace name: Terraform docs (overwrite README.md) - language: python + description: Overwrite content of README.md with terraform-docs + require_serial: true entry: terraform_docs_replace + language: python files: (\.tf)$ exclude: \.terraform\/.*$ - description: Overwrite content of README.md with terraform-docs - id: terraform_validate_no_variables name: Terraform validate without variables From bc0e68bfd3ec0c777fcb511baf94af323d40c620 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Mon, 18 Feb 2019 18:52:10 +0100 Subject: [PATCH 10/37] Added chglog (hi @robinbowes :)) --- .chglog/CHANGELOG.tpl.md | 51 ++++++++++++++++++++++++++++++++++++++++ .chglog/config.yml | 10 ++++++++ CHANGELOG.md | 0 Makefile | 7 ++++++ 4 files changed, 68 insertions(+) create mode 100644 .chglog/CHANGELOG.tpl.md create mode 100644 .chglog/config.yml create mode 100644 CHANGELOG.md create mode 100644 Makefile diff --git a/.chglog/CHANGELOG.tpl.md b/.chglog/CHANGELOG.tpl.md new file mode 100644 index 000000000..a58b0e452 --- /dev/null +++ b/.chglog/CHANGELOG.tpl.md @@ -0,0 +1,51 @@ +{{ if .Versions -}} + +## [Unreleased] +{{ if .Unreleased.CommitGroups -}} +{{ range .Unreleased.CommitGroups -}} +### {{ .Title }} +{{ range .Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} +{{ else }} +{{ range .Unreleased.Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} +{{ end -}} + +{{ range .Versions }} + +## {{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }} - {{ datetime "2006-01-02" .Tag.Date }} +{{ if .CommitGroups -}} +{{ range .CommitGroups -}} +### {{ .Title }} +{{ range .Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} +{{ else }} +{{ range .Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} + +{{- if .NoteGroups -}} +{{ range .NoteGroups -}} +### {{ .Title }} +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} + +{{- if .Versions }} +[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD +{{ range .Versions -}} +{{ if .Tag.Previous -}} +[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }} +{{ end -}} +{{ end -}} +{{ end -}} diff --git a/.chglog/config.yml b/.chglog/config.yml new file mode 100644 index 000000000..3529dbcd6 --- /dev/null +++ b/.chglog/config.yml @@ -0,0 +1,10 @@ +style: github +template: CHANGELOG.tpl.md +info: + title: CHANGELOG + repository_url: https://github.com/antonbabenko/pre-commit-terraform +options: + header: + pattern: "^(.*)$" + pattern_maps: + - Subject diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..e69de29bb diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..558dac5a6 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: changelog release + +changelog: + git-chglog -o CHANGELOG.md --next-tag `semtag final -s minor -o` + +release: + semtag final -s minor From 66214dcc4b092a81e0146b2e66745627386b90ee Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Mon, 18 Feb 2019 18:52:46 +0100 Subject: [PATCH 11/37] Added CHANGELOG.md --- CHANGELOG.md | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb..eb4b86403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1,141 @@ + +## [Unreleased] + + + + +## [v1.9.0] - 2019-02-18 + +- Added chglog (hi [@robinbowes](https://github.com/robinbowes) :)) +- Merge pull request [#33](https://github.com/antonbabenko/pre-commit-terraform/issues/33) from chrisgilmerproj/run_terraform_docs_in_serial +- Require terraform-docs runs in serial to avoid pre-commit doing parallel operations on similar file paths + + + +## [v1.8.1] - 2018-12-15 + +- Merge pull request [#30](https://github.com/antonbabenko/pre-commit-terraform/issues/30) from RothAndrew/feature/fix_issue_29 +- Fix bug not letting terraform_docs_replace work in the root directory of a repo + + + +## [v1.8.0] - 2018-12-14 + +- Merge pull request [#27](https://github.com/antonbabenko/pre-commit-terraform/issues/27) from RothAndrew/feature/new_hook +- fix typo +- Address requested changes +- Add `--dest` argument +- Address requested changes +- Add new hook for running terraform-docs with replacing README.md from doc in main.tf + + + +## [v1.7.4] - 2018-12-11 + +- Merge remote-tracking branch 'origin/master' into pr25 +- Added followup after [#25](https://github.com/antonbabenko/pre-commit-terraform/issues/25) +- Merge pull request [#25](https://github.com/antonbabenko/pre-commit-terraform/issues/25) from getcloudnative/feat-pass-terraform-docs-opts +- Add feature to pass options to terraform-docs. +- Added license file (fixed [#21](https://github.com/antonbabenko/pre-commit-terraform/issues/21)) + + + +## [v1.7.3] - 2018-05-24 + +- Updated README +- Only run validate if .tf files exist in the directory. ([#20](https://github.com/antonbabenko/pre-commit-terraform/issues/20)) + + + +## [v1.7.2] - 2018-05-20 + +- Replace terraform_docs use of GNU sed with perl ([#15](https://github.com/antonbabenko/pre-commit-terraform/issues/15)) +- Fixes use of md5 for tempfile name ([#16](https://github.com/antonbabenko/pre-commit-terraform/issues/16)) + + + +## [v1.7.1] - 2018-05-16 + +- Run terraform_docs only if README.md is present +- Run terraform_docs only if README.md is present + + + +## [v1.7.0] - 2018-05-16 + +- Added terraform-docs integration ([#13](https://github.com/antonbabenko/pre-commit-terraform/issues/13)) + + + +## [v1.6.0] - 2018-04-21 + +- Allow to have spaces in directories ([#11](https://github.com/antonbabenko/pre-commit-terraform/issues/11)) + + + +## [v1.5.0] - 2018-03-06 + +- Bump new version +- Format tfvars files explicitely, because terraform fmt ignores them ([#9](https://github.com/antonbabenko/pre-commit-terraform/issues/9)) + + + +## [v1.4.0] - 2018-01-24 + +- Updated readme +- Show failed path +- Show failed path +- Show failed path +- Updated scripts +- Added scripts to validate terraform files + + + +## [v1.3.0] - 2018-01-15 + +- Added badges +- Added formatting for tfvars (fixes [#4](https://github.com/antonbabenko/pre-commit-terraform/issues/4)) ([#6](https://github.com/antonbabenko/pre-commit-terraform/issues/6)) +- Merge pull request [#5](https://github.com/antonbabenko/pre-commit-terraform/issues/5) from schneems/schneems/codetriage-badge +- [ci skip] Get more Open Source Helpers + + + +## [v1.2.0] - 2017-06-08 + +- Renamed shell script file to the correct one +- Updated .pre-commit-hooks.yaml +- Updated sha in README +- Merge pull request [#3](https://github.com/antonbabenko/pre-commit-terraform/issues/3) from pecigonzalo/master +- Exclude .terraform even on subfolders + + + +## [v1.1.0] - 2017-02-04 + +- Copied to .pre-commit-hooks.yaml for compatibility (closes [#1](https://github.com/antonbabenko/pre-commit-terraform/issues/1)) + + + +## v1.0.0 - 2016-09-27 + +- Updated README +- Ready, probably :) +- Initial commit +- Initial commit + + +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.9.0...HEAD +[v1.9.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.8.1...v1.9.0 +[v1.8.1]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.8.0...v1.8.1 +[v1.8.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.7.4...v1.8.0 +[v1.7.4]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.7.3...v1.7.4 +[v1.7.3]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.7.2...v1.7.3 +[v1.7.2]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.7.1...v1.7.2 +[v1.7.1]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.7.0...v1.7.1 +[v1.7.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.6.0...v1.7.0 +[v1.6.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.5.0...v1.6.0 +[v1.5.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.4.0...v1.5.0 +[v1.4.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.3.0...v1.4.0 +[v1.3.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.2.0...v1.3.0 +[v1.2.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.1.0...v1.2.0 +[v1.1.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.0.0...v1.1.0 From beb4a677531027e2991cade71b9567995939845a Mon Sep 17 00:00:00 2001 From: Josiah Halme Date: Thu, 21 Feb 2019 19:38:50 +1100 Subject: [PATCH 12/37] Add exit code for 'terraform validate' so pre-commit check fails (#34) --- terraform_validate_with_variables.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/terraform_validate_with_variables.sh b/terraform_validate_with_variables.sh index 706443ade..01d5536f8 100755 --- a/terraform_validate_with_variables.sh +++ b/terraform_validate_with_variables.sh @@ -3,6 +3,7 @@ set -e declare -a paths index=0 +error=0 for file_with_path in "$@"; do file_with_path="${file_with_path// /__REPLACED__SPACE__}" @@ -17,6 +18,7 @@ for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do pushd "$path_uniq" > /dev/null if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]] ; then if ! terraform validate -check-variables=true ; then + error=1 echo echo "Failed path: $path_uniq" echo "================================" @@ -24,3 +26,7 @@ for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do fi popd > /dev/null done + +if [[ -n "${error}" ]] ; then + exit 1 +fi From 2c842d94615268be20391c248b91b070e60c5912 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Thu, 21 Feb 2019 09:43:31 +0100 Subject: [PATCH 13/37] Bump new version --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb4b86403..2fca9c7e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,16 @@ + +## [v1.10.0] - 2019-02-21 + +- Add exit code for 'terraform validate' so pre-commit check fails ([#34](https://github.com/antonbabenko/pre-commit-terraform/issues/34)) + + ## [v1.9.0] - 2019-02-18 +- Added CHANGELOG.md - Added chglog (hi [@robinbowes](https://github.com/robinbowes) :)) - Merge pull request [#33](https://github.com/antonbabenko/pre-commit-terraform/issues/33) from chrisgilmerproj/run_terraform_docs_in_serial - Require terraform-docs runs in serial to avoid pre-commit doing parallel operations on similar file paths @@ -124,7 +131,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.9.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.10.0...HEAD +[v1.10.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.9.0...v1.10.0 [v1.9.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.8.1...v1.9.0 [v1.8.1]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.8.0...v1.8.1 [v1.8.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.7.4...v1.8.0 From a3771b5119231be9677934b88ee47203e9cb5a90 Mon Sep 17 00:00:00 2001 From: Tyler Christiansen Date: Fri, 1 Mar 2019 00:48:50 -0800 Subject: [PATCH 14/37] fix check for errors at the end (#35) --- terraform_validate_with_variables.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terraform_validate_with_variables.sh b/terraform_validate_with_variables.sh index 01d5536f8..355a91372 100755 --- a/terraform_validate_with_variables.sh +++ b/terraform_validate_with_variables.sh @@ -27,6 +27,6 @@ for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do popd > /dev/null done -if [[ -n "${error}" ]] ; then +if [[ "${error}" -ne 0 ]] ; then exit 1 fi From 7eb9ca3f2f70302227d1315a66a5a868562c02b7 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Fri, 1 Mar 2019 09:49:34 +0100 Subject: [PATCH 15/37] Updated changelog --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fca9c7e4..40cc5bac8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,16 @@ + +## [v1.11.0] - 2019-03-01 + +- fix check for errors at the end ([#35](https://github.com/antonbabenko/pre-commit-terraform/issues/35)) + + ## [v1.10.0] - 2019-02-21 +- Bump new version - Add exit code for 'terraform validate' so pre-commit check fails ([#34](https://github.com/antonbabenko/pre-commit-terraform/issues/34)) @@ -131,7 +138,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.10.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.11.0...HEAD +[v1.11.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.10.0...v1.11.0 [v1.10.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.9.0...v1.10.0 [v1.9.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.8.1...v1.9.0 [v1.8.1]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.8.0...v1.8.1 From 249c02bb64e9f8a9b08a11c723fd78590cdccea2 Mon Sep 17 00:00:00 2001 From: Guido Dobboletta Date: Tue, 5 Mar 2019 02:16:55 -0600 Subject: [PATCH 16/37] Update README.md (#36) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e114729d..d26f97f20 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Step into the repository you want to have the pre-commit hooks installed and run ```bash cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.7.4 + rev: v1.11.0 hooks: - id: terraform_fmt - id: terraform_docs From 418a5ec782f8ed37d804a539ef1b2d3c14bee8bf Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Sat, 6 Apr 2019 21:19:39 +0200 Subject: [PATCH 17/37] Fixed broken "maintained badge" --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d26f97f20..08749368b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Collection of git hooks for Terraform to be used with [pre-commit framework](http://pre-commit.com/) -[![Github tag](https://img.shields.io/github/tag/antonbabenko/pre-commit-terraform.svg)](https://github.com/antonbabenko/pre-commit-terraform/releases) ![](https://img.shields.io/maintenance/yes/2018.svg) [![Help Contribute to Open Source](https://www.codetriage.com/antonbabenko/pre-commit-terraform/badges/users.svg)](https://www.codetriage.com/antonbabenko/pre-commit-terraform) +[![Github tag](https://img.shields.io/github/tag/antonbabenko/pre-commit-terraform.svg)](https://github.com/antonbabenko/pre-commit-terraform/releases) ![](https://img.shields.io/maintenance/yes/2019.svg) [![Help Contribute to Open Source](https://www.codetriage.com/antonbabenko/pre-commit-terraform/badges/users.svg)](https://www.codetriage.com/antonbabenko/pre-commit-terraform) ## How to install From 5725b11b558a03eb787623d53a0c7c157cb75f3b Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Mon, 27 May 2019 09:33:32 -0700 Subject: [PATCH 18/37] Added note about incompatibility of terraform-docs with Terraform 0.12 (#41) --- .pre-commit-config.yaml | 2 +- README.md | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1c444a95a..f80cfc805 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: git://github.com/pre-commit/pre-commit-hooks - rev: v2.0.0 + rev: v2.2.3 hooks: - id: check-yaml - id: end-of-file-fixer diff --git a/README.md b/README.md index 08749368b..fac045ae5 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,10 @@ Step into the repository you want to have the pre-commit hooks installed and run ```bash cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.11.0 + rev: v1.12.0 hooks: - id: terraform_fmt - - id: terraform_docs + # - id: terraform_docs # terraform-docs is not working with Terraform 0.12 yet (read note in README) EOF ``` @@ -73,6 +73,8 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. +1. `terraform-docs` is not working with Terraform 0.12 yet (see [issue #62](https://github.com/segmentio/terraform-docs/issues/62)), so remember to disable `terraform_docs`, `terraform_docs_replace` and `terraform_docs_without_aggregate_type_defaults` hooks in your `.pre-commit-config.yaml` + ## Notes for developers 1. Python hooks are supported now too. All you have to do is: From dbf91086f31cef3fdf3652d62eae57ffce943568 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Mon, 27 May 2019 09:34:00 -0700 Subject: [PATCH 19/37] Updated CHANGELOG --- CHANGELOG.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40cc5bac8..ad55efa94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,18 @@ + +## [v1.12.0] - 2019-05-27 + +- Added note about incompatibility of terraform-docs with Terraform 0.12 ([#41](https://github.com/antonbabenko/pre-commit-terraform/issues/41)) +- Fixed broken "maintained badge" +- Update README.md ([#36](https://github.com/antonbabenko/pre-commit-terraform/issues/36)) + + ## [v1.11.0] - 2019-03-01 +- Updated changelog - fix check for errors at the end ([#35](https://github.com/antonbabenko/pre-commit-terraform/issues/35)) @@ -138,7 +147,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.11.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.12.0...HEAD +[v1.12.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.11.0...v1.12.0 [v1.11.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.10.0...v1.11.0 [v1.10.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.9.0...v1.10.0 [v1.9.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.8.1...v1.9.0 From 8cddce38b0953e48c32909ba75408f50c6075e07 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Mon, 17 Jun 2019 12:47:06 +0200 Subject: [PATCH 20/37] Added support for terraform_docs for Terraform 0.12 (#45) --- README.md | 10 ++-- terraform_docs.sh | 113 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 115 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fac045ae5..b39066b79 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,10 @@ ### Step 1 -On MacOSX install the pre-commit package +On MacOSX install the `pre-commit` and `awk` (required for Terraform 0.12) package ```bash -brew install pre-commit +brew install pre-commit awk ``` For other operating systems check the [official documentation](http://pre-commit.com/#install) @@ -21,10 +21,10 @@ Step into the repository you want to have the pre-commit hooks installed and run ```bash cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.12.0 + rev: v1.13.0 hooks: - id: terraform_fmt - # - id: terraform_docs # terraform-docs is not working with Terraform 0.12 yet (read note in README) + - id: terraform_docs EOF ``` @@ -73,7 +73,7 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo 1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. -1. `terraform-docs` is not working with Terraform 0.12 yet (see [issue #62](https://github.com/segmentio/terraform-docs/issues/62)), so remember to disable `terraform_docs`, `terraform_docs_replace` and `terraform_docs_without_aggregate_type_defaults` hooks in your `.pre-commit-config.yaml` +1. `terraform-docs` works with Terraform 0.12 but support is hackish (it requires `awk` to be installed) and may contain bugs. You can follow the native support of Terraform 0.12 in `terraform-docs` in [issue #62](https://github.com/segmentio/terraform-docs/issues/62). ## Notes for developers diff --git a/terraform_docs.sh b/terraform_docs.sh index 1bd79627a..191a4a2bd 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -25,12 +25,25 @@ main() { esac done - terraform_docs "$args" "$files" + local hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) + + 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) + + TMP_AWK_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.awk)" + terraform_docs_awk $TMP_AWK_FILE + terraform_docs "$TMP_AWK_FILE" "$args" "$files" + rm -f "$TMP_AWK_FILE" + else + terraform_docs "0" "$args" "$files" + fi + } terraform_docs() { - readonly args="$1" - readonly files="$2" + readonly terraform_docs_awk_file="$1" + readonly args="$2" + readonly files="$3" declare -a paths declare -a tfvars_files @@ -62,7 +75,14 @@ terraform_docs() { continue fi + if [[ "$terraform_docs_awk_file" == "0" ]]; then terraform-docs $args md ./ > "$tmp_file" + else + TMP_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.tf)" + awk -f "$terraform_docs_awk_file" ./*.tf > "$TMP_FILE" + terraform-docs $args md "$TMP_FILE" > "$tmp_file" + rm -f "$TMP_FILE" + 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 perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file" @@ -76,6 +96,93 @@ terraform_docs() { done } +terraform_docs_awk() { + readonly output_file=$1 + + cat <<"EOF" > $output_file +# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs` +# 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 + +{ + 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 + } + + # [START] multiline default statement started + if (blockCnt > 0) { + if ($1 == "default") { + print $0 + if ($NF ~ /[\[\(\{]/) { + blockDefCnt++ + blockDefStart=1 + } + } + } + + # [PRINT] single line "description" + if (blockDefCnt == 0) { + if ($1 == "description") { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + print $0 + } + } + + # [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 "\"" + } + } + } + + # [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 + } + } +} +EOF + +} + getopt() { # pure-getopt, a drop-in replacement for GNU getopt in pure Bash. # version 1.4.3 From 9300d0f1942bf03f6a41225a445368014b2f5f62 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Mon, 17 Jun 2019 12:47:58 +0200 Subject: [PATCH 21/37] Updated CHANGELOG --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad55efa94..bc5a04e33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,16 @@ + +## [v1.13.0] - 2019-06-17 + +- Added support for terraform_docs for Terraform 0.12 ([#45](https://github.com/antonbabenko/pre-commit-terraform/issues/45)) + + ## [v1.12.0] - 2019-05-27 +- Updated CHANGELOG - Added note about incompatibility of terraform-docs with Terraform 0.12 ([#41](https://github.com/antonbabenko/pre-commit-terraform/issues/41)) - Fixed broken "maintained badge" - Update README.md ([#36](https://github.com/antonbabenko/pre-commit-terraform/issues/36)) @@ -147,7 +154,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.12.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.13.0...HEAD +[v1.13.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.12.0...v1.13.0 [v1.12.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.11.0...v1.12.0 [v1.11.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.10.0...v1.11.0 [v1.10.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.9.0...v1.10.0 From 35e0356188b64a4c5af9a4e7200d936e514cba71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szczepaniak?= Date: Mon, 17 Jun 2019 13:09:31 +0200 Subject: [PATCH 22/37] Upgraded to work with Terraform >= 0.12 (#44) --- .pre-commit-hooks.yaml | 16 +++--------- README.md | 5 +--- ...with_variables.sh => terraform_validate.sh | 4 +-- terraform_validate_no_variables.sh | 26 ------------------- 4 files changed, 6 insertions(+), 45 deletions(-) rename terraform_validate_with_variables.sh => terraform_validate.sh (84%) delete mode 100755 terraform_validate_no_variables.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index afb5d6066..ee10f8228 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -27,25 +27,17 @@ - id: terraform_docs_replace name: Terraform docs (overwrite README.md) - description: Overwrite content of README.md with terraform-docs + description: Overwrite content of README.md with terraform-docs. require_serial: true entry: terraform_docs_replace language: python files: (\.tf)$ exclude: \.terraform\/.*$ -- id: terraform_validate_no_variables +- id: terraform_validate name: Terraform validate without variables - description: Validates all Terraform configuration files without checking whether all required variables were set (basic check). - entry: terraform_validate_no_variables.sh - language: script - files: (\.tf|\.tfvars)$ - exclude: \.terraform\/.*$ - -- id: terraform_validate_with_variables - name: Terraform validate with variables - description: Validates all Terraform configuration files and checks whether all required variables were specified. - entry: terraform_validate_with_variables.sh + description: Validates all Terraform configuration files. + entry: terraform_validate.sh language: script files: (\.tf|\.tfvars)$ exclude: \.terraform\/.*$ diff --git a/README.md b/README.md index b39066b79..370ac9eff 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,7 @@ pre-commit run -a There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) in a good shape: * `terraform_fmt` - Rewrites all Terraform configuration files to a canonical format. -* `terraform_validate_no_variables` - Validates all Terraform configuration files without checking whether all required variables were set. -* `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. +* `terraform_validate` - Validates all Terraform configuration files. * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. * `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md @@ -58,8 +57,6 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo ## Notes about hooks -1. `terraform_validate_no_variables` and `terraform_validate_with_variables` will not work if variables are being set dynamically (eg, when using [Terragrunt](https://github.com/gruntwork-io/terragrunt)). Use `terragrunt validate` command instead. - 1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. 1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets you change the name of the file that gets created/modified. diff --git a/terraform_validate_with_variables.sh b/terraform_validate.sh similarity index 84% rename from terraform_validate_with_variables.sh rename to terraform_validate.sh index 355a91372..6f7eccac2 100755 --- a/terraform_validate_with_variables.sh +++ b/terraform_validate.sh @@ -15,16 +15,14 @@ done for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do path_uniq="${path_uniq//__REPLACED__SPACE__/ }" - pushd "$path_uniq" > /dev/null if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]] ; then - if ! terraform validate -check-variables=true ; then + if ! terraform validate $path_uniq; then error=1 echo echo "Failed path: $path_uniq" echo "================================" fi fi - popd > /dev/null done if [[ "${error}" -ne 0 ]] ; then diff --git a/terraform_validate_no_variables.sh b/terraform_validate_no_variables.sh deleted file mode 100755 index 2e190448a..000000000 --- a/terraform_validate_no_variables.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash -set -e - -declare -a paths -index=0 - -for file_with_path in "$@"; do - file_with_path="${file_with_path// /__REPLACED__SPACE__}" - - paths[index]=$(dirname "$file_with_path") - (( "index+=1" )) -done - -for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do - path_uniq="${path_uniq//__REPLACED__SPACE__/ }" - - pushd "$path_uniq" > /dev/null - if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]] ; then - if ! terraform validate -check-variables=false ; then - echo - echo "Failed path: $path_uniq" - echo "================================" - fi - fi - popd > /dev/null -done From 060249f2fe4df62f413ae412eff40820560e21de Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Mon, 17 Jun 2019 13:12:16 +0200 Subject: [PATCH 23/37] Updated CHANGELOG --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc5a04e33..5d79b6ab2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,16 @@ + +## [v1.14.0] - 2019-06-17 + +- Upgraded to work with Terraform >= 0.12 ([#44](https://github.com/antonbabenko/pre-commit-terraform/issues/44)) + + ## [v1.13.0] - 2019-06-17 +- Updated CHANGELOG - Added support for terraform_docs for Terraform 0.12 ([#45](https://github.com/antonbabenko/pre-commit-terraform/issues/45)) @@ -154,7 +161,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.13.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.14.0...HEAD +[v1.14.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.13.0...v1.14.0 [v1.13.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.12.0...v1.13.0 [v1.12.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.11.0...v1.12.0 [v1.11.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.10.0...v1.11.0 From d678da98103da31aee9c05b07b6ca9f68191e061 Mon Sep 17 00:00:00 2001 From: Leonhardt Wille Date: Mon, 17 Jun 2019 14:01:24 +0200 Subject: [PATCH 24/37] Fix version in README.md (#46) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 370ac9eff..667236005 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Step into the repository you want to have the pre-commit hooks installed and run ```bash cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.13.0 + rev: v1.14.0 hooks: - id: terraform_fmt - id: terraform_docs From 10854fcfa2202bd677446b2d298a0ea6119941fb Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 18 Jun 2019 13:58:49 +0200 Subject: [PATCH 25/37] Fixed awk script for terraform-docs (kudos @cytopia) and mktemp on Mac (closes #47, #48, #49) --- terraform_docs.sh | 54 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/terraform_docs.sh b/terraform_docs.sh index 191a4a2bd..06e6f33e5 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -30,10 +30,10 @@ main() { 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) - TMP_AWK_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.awk)" - terraform_docs_awk $TMP_AWK_FILE - terraform_docs "$TMP_AWK_FILE" "$args" "$files" - rm -f "$TMP_AWK_FILE" + tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}terraform-docs-XXXXXXXXXX") + terraform_docs_awk "$tmp_file_awk" + terraform_docs "$tmp_file_awk" "$args" "$files" + rm -f "$tmp_file_awk" else terraform_docs "0" "$args" "$files" fi @@ -78,10 +78,14 @@ terraform_docs() { if [[ "$terraform_docs_awk_file" == "0" ]]; then terraform-docs $args md ./ > "$tmp_file" else - TMP_FILE="$(mktemp --tmpdir terraform-docs-XXXXXXXXXX.tf)" - awk -f "$terraform_docs_awk_file" ./*.tf > "$TMP_FILE" - terraform-docs $args md "$TMP_FILE" > "$tmp_file" - rm -f "$TMP_FILE" + # Can't append extension for mktemp, so renaming instead + tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}terraform-docs-XXXXXXXXXX") + mv "$tmp_file_docs" "$tmp_file_docs.tf" + 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 # 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/issues/62 +# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk + { - if ( /\{/ ) { + if ( $0 ~ /\{/ ) { braceCnt++ } - if ( /\}/ ) { + if ( $0 ~ /\}/ ) { braceCnt-- } # [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 if (blockDefCnt > 0) { blockDefCnt = 0 @@ -126,9 +134,11 @@ terraform_docs_awk() { # [START] multiline default statement started if (blockCnt > 0) { - if ($1 == "default") { - print $0 - if ($NF ~ /[\[\(\{]/) { + if ($0 ~ /^[[:space:]][[:space:]]*(default)[[:space:]][[:space:]]*=/) { + if ($3 ~ "null") { + print " default = \"null\"" + } else { + print $0 blockDefCnt++ blockDefStart=1 } @@ -136,19 +146,21 @@ terraform_docs_awk() { } # [PRINT] single line "description" - if (blockDefCnt == 0) { - if ($1 == "description") { - # [CLOSE] "default" block - if (blockDefCnt > 0) { - blockDefCnt = 0 + if (blockCnt > 0) { + if (blockDefCnt == 0) { + if ($0 ~ /^[[:space:]][[:space:]]*description[[:space:]][[:space:]]*=/) { + # [CLOSE] "default" block + if (blockDefCnt > 0) { + blockDefCnt = 0 + } + print $0 } - print $0 } } # [PRINT] single line "type" if (blockCnt > 0) { - if ($1 == "type" ) { + if ($0 ~ /^[[:space:]][[:space:]]*type[[:space:]][[:space:]]*=/ ) { # [CLOSE] "default" block if (blockDefCnt > 0) { blockDefCnt = 0 From 59ffa65527d7628b0f87b665c167643515940a2f Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 18 Jun 2019 14:00:29 +0200 Subject: [PATCH 26/37] Updated CHANGELOG --- CHANGELOG.md | 11 ++++++++++- README.md | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d79b6ab2..935f1913c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,17 @@ + +## [v1.15.0] - 2019-06-18 + +- Fixed awk script for terraform-docs (kudos [@cytopia](https://github.com/cytopia)) and mktemp on Mac (closes [#47](https://github.com/antonbabenko/pre-commit-terraform/issues/47), [#48](https://github.com/antonbabenko/pre-commit-terraform/issues/48), [#49](https://github.com/antonbabenko/pre-commit-terraform/issues/49)) +- Fix version in README.md ([#46](https://github.com/antonbabenko/pre-commit-terraform/issues/46)) + + ## [v1.14.0] - 2019-06-17 +- Updated CHANGELOG - Upgraded to work with Terraform >= 0.12 ([#44](https://github.com/antonbabenko/pre-commit-terraform/issues/44)) @@ -161,7 +169,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.14.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.15.0...HEAD +[v1.15.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.14.0...v1.15.0 [v1.14.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.13.0...v1.14.0 [v1.13.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.12.0...v1.13.0 [v1.12.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.11.0...v1.12.0 diff --git a/README.md b/README.md index 667236005..1ff6d400a 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Step into the repository you want to have the pre-commit hooks installed and run ```bash cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.14.0 + rev: v1.15.0 hooks: - id: terraform_fmt - id: terraform_docs From c9ecd72f5f5014b983ca2e87c431cbb8b2cc7eed Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 18 Jun 2019 21:17:57 +0200 Subject: [PATCH 27/37] Add slash to mktemp dir (fixed #50) --- terraform_docs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terraform_docs.sh b/terraform_docs.sh index 06e6f33e5..464dc946d 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -30,7 +30,7 @@ main() { 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) - tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}terraform-docs-XXXXXXXXXX") + tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") terraform_docs_awk "$tmp_file_awk" terraform_docs "$tmp_file_awk" "$args" "$files" rm -f "$tmp_file_awk" @@ -79,7 +79,7 @@ terraform_docs() { terraform-docs $args md ./ > "$tmp_file" else # Can't append extension for mktemp, so renaming instead - tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}terraform-docs-XXXXXXXXXX") + tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") mv "$tmp_file_docs" "$tmp_file_docs.tf" tmp_file_docs_tf="$tmp_file_docs.tf" From 5d8d926848047866342fd9e211f3f81c5157cdfd Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 18 Jun 2019 21:18:18 +0200 Subject: [PATCH 28/37] Updated CHANGELOG --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 935f1913c..5522d64af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,16 @@ + +## [v1.16.0] - 2019-06-18 + +- Add slash to mktemp dir (fixed [#50](https://github.com/antonbabenko/pre-commit-terraform/issues/50)) + + ## [v1.15.0] - 2019-06-18 +- Updated CHANGELOG - Fixed awk script for terraform-docs (kudos [@cytopia](https://github.com/cytopia)) and mktemp on Mac (closes [#47](https://github.com/antonbabenko/pre-commit-terraform/issues/47), [#48](https://github.com/antonbabenko/pre-commit-terraform/issues/48), [#49](https://github.com/antonbabenko/pre-commit-terraform/issues/49)) - Fix version in README.md ([#46](https://github.com/antonbabenko/pre-commit-terraform/issues/46)) @@ -169,7 +176,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.15.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.16.0...HEAD +[v1.16.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.15.0...v1.16.0 [v1.15.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.14.0...v1.15.0 [v1.14.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.13.0...v1.14.0 [v1.13.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.12.0...v1.13.0 From 3147a7c0ee5d06d783346a968aa2c0c4e104608b Mon Sep 17 00:00:00 2001 From: Eric Gonzales Date: Wed, 19 Jun 2019 06:10:21 -0400 Subject: [PATCH 29/37] Fix typo in README (#51) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ff6d400a..c05352dec 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo ## Notes for developers 1. Python hooks are supported now too. All you have to do is: - 1. add a line to the `console_sripts` array in `entry_points` in `setup.py` + 1. add a line to the `console_scripts` array in `entry_points` in `setup.py` 1. Put your python script in the `pre_commit_hooks` folder Enjoy the clean and documented code! From 83629157c22800f59ab59642f82abef3df31721a Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 25 Jun 2019 14:34:46 +0200 Subject: [PATCH 30/37] Fixed enquoted types in terraform_docs (fixed #52) --- terraform_docs.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/terraform_docs.sh b/terraform_docs.sh index 464dc946d..0935b69e6 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -169,7 +169,12 @@ terraform_docs_awk() { if (type ~ "object") { print " type = \"object\"" } else { - print " type = \"" $3 "\"" + # legacy quoted types: "string", "list", and "map" + if ($3 ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) { + print " type = " $3 + } else { + print " type = \"" $3 "\"" + } } } } From 4bebeac734da116cc5fe535643252724321521a7 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 25 Jun 2019 14:35:00 +0200 Subject: [PATCH 31/37] Updated CHANGELOG --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5522d64af..ebbb6fba7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,16 @@ + +## [v1.17.0] - 2019-06-25 + +- Fixed enquoted types in terraform_docs (fixed [#52](https://github.com/antonbabenko/pre-commit-terraform/issues/52)) + + ## [v1.16.0] - 2019-06-18 +- Updated CHANGELOG - Add slash to mktemp dir (fixed [#50](https://github.com/antonbabenko/pre-commit-terraform/issues/50)) @@ -176,7 +183,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.16.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.17.0...HEAD +[v1.17.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.16.0...v1.17.0 [v1.16.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.15.0...v1.16.0 [v1.15.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.14.0...v1.15.0 [v1.14.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.13.0...v1.14.0 From d8dfc2c0345a8a846ca7841e4bd58b7deab91810 Mon Sep 17 00:00:00 2001 From: Scott Crooks Date: Tue, 20 Aug 2019 20:38:40 +0200 Subject: [PATCH 32/37] Formatter for Terragrunt HCL files (#60) * Formatter for Terragrunt HCL files * Adding Terragrunt documentation --- .pre-commit-hooks.yaml | 8 ++++++++ README.md | 1 + terragrunt_fmt.sh | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100755 terragrunt_fmt.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index ee10f8228..789d7095a 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -41,3 +41,11 @@ language: script files: (\.tf|\.tfvars)$ exclude: \.terraform\/.*$ + +- id: terragrunt_fmt + name: Terragrunt fmt + description: Rewrites all Terragrunt configuration files to a canonical format. + entry: terragrunt_fmt.sh + language: script + files: (\.hcl)$ + exclude: \.terraform\/.*$ diff --git a/README.md b/README.md index c05352dec..539195b70 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform c * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. * `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md +* `terragrunt_fmt` - Rewrites all Terragrunt configuration files to a canonical format. Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. diff --git a/terragrunt_fmt.sh b/terragrunt_fmt.sh new file mode 100755 index 000000000..ee23131e7 --- /dev/null +++ b/terragrunt_fmt.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +set -e + +declare -a paths + +index=0 + +for file_with_path in "$@"; do + file_with_path="${file_with_path// /__REPLACED__SPACE__}" + + paths[index]=$(dirname "$file_with_path") + + let "index+=1" +done + +for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do + path_uniq="${path_uniq//__REPLACED__SPACE__/ }" + + pushd "$path_uniq" > /dev/null + terragrunt hclfmt + popd > /dev/null +done From 7eb805fb0e1f2acb5dbfe8b04ff72fb3b31f5a2c Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 20 Aug 2019 21:16:30 +0200 Subject: [PATCH 33/37] Updated README with terragrunt_fmt hook --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 539195b70..baca5ebcf 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Step into the repository you want to have the pre-commit hooks installed and run ```bash cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.15.0 + rev: v1.18.0 hooks: - id: terraform_fmt - id: terraform_docs @@ -46,13 +46,13 @@ pre-commit run -a ## Available Hooks -There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) in a good shape: +There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) and Terragrunt configurations (`*.hcl`) in a good shape: * `terraform_fmt` - Rewrites all Terraform configuration files to a canonical format. * `terraform_validate` - Validates all Terraform configuration files. * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. * `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md -* `terragrunt_fmt` - Rewrites all Terragrunt configuration files to a canonical format. +* `terragrunt_fmt` - Rewrites all Terragrunt configuration files (`*.hcl`) to a canonical format. Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. From dc8cf4844190ce81f214485917639d7cf7c308b9 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 20 Aug 2019 21:17:41 +0200 Subject: [PATCH 34/37] Updated CHANGELOG --- CHANGELOG.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebbb6fba7..a5681f642 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,19 @@ + +## [v1.18.0] - 2019-08-20 + +- Updated README with terragrunt_fmt hook +- Formatter for Terragrunt HCL files ([#60](https://github.com/antonbabenko/pre-commit-terraform/issues/60)) + + ## [v1.17.0] - 2019-06-25 +- Updated CHANGELOG - Fixed enquoted types in terraform_docs (fixed [#52](https://github.com/antonbabenko/pre-commit-terraform/issues/52)) +- Fix typo in README ([#51](https://github.com/antonbabenko/pre-commit-terraform/issues/51)) @@ -183,7 +192,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.17.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.18.0...HEAD +[v1.18.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.17.0...v1.18.0 [v1.17.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.16.0...v1.17.0 [v1.16.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.15.0...v1.16.0 [v1.15.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.14.0...v1.15.0 From e0d3d614225017064ad47921ced90f04f7e15bd5 Mon Sep 17 00:00:00 2001 From: Costin GALAN Date: Tue, 20 Aug 2019 22:31:28 +0300 Subject: [PATCH 35/37] Added support for TFLint with --deep parameter (#53) Added support for TFLint (https://github.com/wata727/tflint). Signed-off-by: Costin Galan --- .pre-commit-hooks.yaml | 8 ++++++++ terraform_tflint.sh | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 terraform_tflint.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 789d7095a..c7f403941 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -42,6 +42,14 @@ files: (\.tf|\.tfvars)$ exclude: \.terraform\/.*$ +- id: terraform_tflint + name: Terraform validate with tflint + description: Validates all Terraform configuration files with TFLint. + entry: terraform_tflint.sh + language: script + files: (\.tf|\.tfvars)$ + exclude: \.terraform\/.*$ + - id: terragrunt_fmt name: Terragrunt fmt description: Rewrites all Terragrunt configuration files to a canonical format. diff --git a/terraform_tflint.sh b/terraform_tflint.sh new file mode 100755 index 000000000..4e6c23e76 --- /dev/null +++ b/terraform_tflint.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -e + +declare -a paths +declare -a tfvars_files + +index=0 + +for file_with_path in "$@"; do + file_with_path="${file_with_path// /__REPLACED__SPACE__}" + + paths[index]=$(dirname "$file_with_path") + + let "index+=1" +done + +for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do + path_uniq="${path_uniq//__REPLACED__SPACE__/ }" + + pushd "$path_uniq" > /dev/null + tflint --deep + popd > /dev/null +done From d5640fd4dc0e07ab4276409c1e8d2e8f9f1f1ddc Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 20 Aug 2019 21:34:42 +0200 Subject: [PATCH 36/37] Updated README with terraform_tflint hook --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index baca5ebcf..6637beec6 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform c * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. * `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md +* `terraform_tflint` - Validates all Terraform configuration files with [TFLint](https://github.com/wata727/tflint). * `terragrunt_fmt` - Rewrites all Terragrunt configuration files (`*.hcl`) to a canonical format. Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. From c823172b8436519494f4b466b0eac3929697b4ed Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 20 Aug 2019 21:34:52 +0200 Subject: [PATCH 37/37] Updated CHANGELOG --- CHANGELOG.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5681f642..8496bac9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,17 @@ + +## [v1.19.0] - 2019-08-20 + +- Updated README with terraform_tflint hook +- Added support for TFLint with --deep parameter ([#53](https://github.com/antonbabenko/pre-commit-terraform/issues/53)) + + ## [v1.18.0] - 2019-08-20 +- Updated CHANGELOG - Updated README with terragrunt_fmt hook - Formatter for Terragrunt HCL files ([#60](https://github.com/antonbabenko/pre-commit-terraform/issues/60)) @@ -192,7 +200,8 @@ - Initial commit -[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.18.0...HEAD +[Unreleased]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.19.0...HEAD +[v1.19.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.18.0...v1.19.0 [v1.18.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.17.0...v1.18.0 [v1.17.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.16.0...v1.17.0 [v1.16.0]: https://github.com/antonbabenko/pre-commit-terraform/compare/v1.15.0...v1.16.0