fix: Correctly handle arrays in terraform_docs.sh (#141)

This commit is contained in:
Sergei Ivanov 2020-09-07 14:50:57 +01:00 committed by GitHub
commit f2cab31bc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 9 deletions

View file

@ -1,6 +1,6 @@
repos: repos:
- repo: git://github.com/pre-commit/pre-commit-hooks - repo: git://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0 rev: v3.2.0
hooks: hooks:
- id: check-yaml - id: check-yaml
- id: end-of-file-fixer - id: end-of-file-fixer
@ -9,7 +9,7 @@ repos:
- id: check-merge-conflict - id: check-merge-conflict
- id: check-executables-have-shebangs - id: check-executables-have-shebangs
- repo: git://github.com/jumanjihouse/pre-commit-hooks - repo: git://github.com/jumanjihouse/pre-commit-hooks
rev: 2.0.0 rev: 2.1.4
hooks: hooks:
- id: shfmt - id: shfmt
args: ['-l', '-i', '2', '-ci', '-sr', '-w'] args: ['-l', '-i', '2', '-ci', '-sr', '-w']

View file

@ -4,7 +4,7 @@ set -eo pipefail
main() { main() {
initialize_ initialize_
parse_cmdline_ "$@" parse_cmdline_ "$@"
terraform_docs_ "$ARGS" "$FILES" terraform_docs_ "${ARGS[*]}" "${FILES[@]}"
} }
initialize_() { initialize_() {
@ -48,7 +48,8 @@ parse_cmdline_() {
terraform_docs_() { terraform_docs_() {
local -r args="$1" local -r args="$1"
local -r files="$2" shift
local -a -r files=("$@")
local hack_terraform_docs local hack_terraform_docs
hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) || true hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) || true
@ -63,7 +64,7 @@ terraform_docs_() {
if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred) if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred)
terraform_docs "0" "$args" "$files" terraform_docs "0" "$args" "${files[@]}"
elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used
@ -75,12 +76,12 @@ terraform_docs_() {
local tmp_file_awk local tmp_file_awk
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_awk "$tmp_file_awk"
terraform_docs "$tmp_file_awk" "$args" "$files" terraform_docs "$tmp_file_awk" "$args" "${files[@]}"
rm -f "$tmp_file_awk" rm -f "$tmp_file_awk"
else # Using terraform 0.11 and no awk script is needed for that else # Using terraform 0.11 and no awk script is needed for that
terraform_docs "0" "$args" "$files" terraform_docs "0" "$args" "${files[@]}"
fi fi
} }
@ -88,14 +89,15 @@ terraform_docs_() {
terraform_docs() { terraform_docs() {
local -r terraform_docs_awk_file="$1" local -r terraform_docs_awk_file="$1"
local -r args="$2" local -r args="$2"
local -r files="$3" shift 2
local -a -r files=("$@")
declare -a paths declare -a paths
declare -a tfvars_files declare -a tfvars_files
local index=0 local index=0
local file_with_path local file_with_path
for file_with_path in $files; do for file_with_path in "${files[@]}"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}" file_with_path="${file_with_path// /__REPLACED__SPACE__}"
paths[index]=$(dirname "$file_with_path") paths[index]=$(dirname "$file_with_path")