mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
feat: Allow passing of args to terraform_fmt (#147)
This commit is contained in:
parent
8a6e4bf4e6
commit
de2f6249ee
1 changed files with 78 additions and 23 deletions
101
terraform_fmt.sh
101
terraform_fmt.sh
|
|
@ -1,34 +1,89 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -e
|
set -eo pipefail
|
||||||
|
|
||||||
declare -a paths
|
main() {
|
||||||
declare -a tfvars_files
|
initialize_
|
||||||
|
parse_cmdline_ "$@"
|
||||||
|
terraform_fmt_
|
||||||
|
}
|
||||||
|
|
||||||
index=0
|
initialize_() {
|
||||||
|
# get directory containing this script
|
||||||
|
local dir
|
||||||
|
local source
|
||||||
|
source="${BASH_SOURCE[0]}"
|
||||||
|
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink
|
||||||
|
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)"
|
||||||
|
source="$(readlink "$source")"
|
||||||
|
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
|
||||||
|
[[ $source != /* ]] && source="$dir/$source"
|
||||||
|
done
|
||||||
|
_SCRIPT_DIR="$(dirname "$source")"
|
||||||
|
|
||||||
for file_with_path in "$@"; do
|
# source getopt function
|
||||||
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
# shellcheck source=lib_getopt
|
||||||
|
. "$_SCRIPT_DIR/lib_getopt"
|
||||||
|
}
|
||||||
|
|
||||||
paths[index]=$(dirname "$file_with_path")
|
parse_cmdline_() {
|
||||||
|
declare argv
|
||||||
|
argv=$(getopt -o a: --long args: -- "$@") || return
|
||||||
|
eval "set -- $argv"
|
||||||
|
|
||||||
if [[ "$file_with_path" == *".tfvars" ]]; then
|
for argv; do
|
||||||
tfvars_files+=("$file_with_path")
|
case $argv in
|
||||||
fi
|
-a | --args)
|
||||||
|
shift
|
||||||
|
ARGS+=("$1")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--)
|
||||||
|
shift
|
||||||
|
FILES=("$@")
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
let "index+=1"
|
terraform_fmt_() {
|
||||||
done
|
|
||||||
|
|
||||||
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
|
declare -a paths
|
||||||
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
|
declare -a tfvars_files
|
||||||
|
|
||||||
pushd "$path_uniq" > /dev/null
|
index=0
|
||||||
terraform fmt
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
|
|
||||||
# terraform.tfvars are excluded by `terraform fmt`
|
for file_with_path in "${FILES[@]}"; do
|
||||||
for tfvars_file in "${tfvars_files[@]}"; do
|
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
||||||
tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }"
|
|
||||||
|
|
||||||
terraform fmt "$tfvars_file"
|
paths[index]=$(dirname "$file_with_path")
|
||||||
done
|
|
||||||
|
if [[ "$file_with_path" == *".tfvars" ]]; then
|
||||||
|
tfvars_files+=("$file_with_path")
|
||||||
|
fi
|
||||||
|
|
||||||
|
((index += 1))
|
||||||
|
done
|
||||||
|
|
||||||
|
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
|
||||||
|
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
|
||||||
|
|
||||||
|
(
|
||||||
|
cd "$path_uniq"
|
||||||
|
terraform fmt "${ARGS[@]}"
|
||||||
|
)
|
||||||
|
done
|
||||||
|
|
||||||
|
# terraform.tfvars are excluded by `terraform fmt`
|
||||||
|
for tfvars_file in "${tfvars_files[@]}"; do
|
||||||
|
tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }"
|
||||||
|
|
||||||
|
terraform fmt "${ARGS[@]}" "$tfvars_file"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# global arrays
|
||||||
|
declare -a ARGS=()
|
||||||
|
declare -a FILES=()
|
||||||
|
|
||||||
|
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue