pre-commit-opentofu/terraform_fmt.sh

34 lines
710 B
Bash
Raw Normal View History

2016-09-27 20:01:11 +02:00
#!/usr/bin/env bash
2018-01-24 13:34:34 +01:00
set -e
2016-09-27 20:01:11 +02:00
2018-01-24 13:34:34 +01:00
declare -a paths
declare -a tfvars_files
2018-01-24 13:34:34 +01:00
index=0
for file_with_path in "$@"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
2018-01-24 13:34:34 +01:00
paths[index]=$(dirname "$file_with_path")
if [[ "$file_with_path" == *".tfvars" ]]; then
tfvars_files+=("$file_with_path")
fi
2018-01-24 13:34:34 +01:00
let "index+=1"
done
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
2018-01-24 13:34:34 +01:00
pushd "$path_uniq" > /dev/null
terraform fmt
popd > /dev/null
2016-09-27 20:01:11 +02:00
done
# terraform.tfvars are excluded by `terraform fmt`
for tfvars_file in "${tfvars_files[@]}"; do
tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }"
terraform fmt "$tfvars_file"
done