2016-09-27 20:01:11 +02:00
|
|
|
|
#!/usr/bin/env bash
|
2021-10-14 17:20:25 +01:00
|
|
|
|
set -eo pipefail
|
2016-09-27 20:01:11 +02:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
main() {
|
|
|
|
|
|
initialize_
|
|
|
|
|
|
parse_cmdline_ "$@"
|
|
|
|
|
|
terraform_fmt_
|
|
|
|
|
|
}
|
2018-03-06 13:58:03 +01:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
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")"
|
2018-01-24 13:34:34 +01:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
# source getopt function
|
|
|
|
|
|
# shellcheck source=lib_getopt
|
|
|
|
|
|
. "$_SCRIPT_DIR/lib_getopt"
|
|
|
|
|
|
}
|
2018-04-21 11:22:47 +02:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
parse_cmdline_() {
|
|
|
|
|
|
declare argv
|
|
|
|
|
|
argv=$(getopt -o a: --long args: -- "$@") || return
|
|
|
|
|
|
eval "set -- $argv"
|
2018-03-06 13:58:03 +01:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
for argv; do
|
|
|
|
|
|
case $argv in
|
|
|
|
|
|
-a | --args)
|
|
|
|
|
|
shift
|
|
|
|
|
|
ARGS+=("$1")
|
|
|
|
|
|
shift
|
|
|
|
|
|
;;
|
|
|
|
|
|
--)
|
|
|
|
|
|
shift
|
|
|
|
|
|
FILES=("$@")
|
|
|
|
|
|
break
|
|
|
|
|
|
;;
|
|
|
|
|
|
esac
|
|
|
|
|
|
done
|
|
|
|
|
|
}
|
2018-03-06 13:58:03 +01:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
terraform_fmt_() {
|
2018-01-24 13:34:34 +01:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
declare -a paths
|
|
|
|
|
|
declare -a tfvars_files
|
2018-04-21 11:22:47 +02:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
index=0
|
2018-03-06 13:58:03 +01:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
for file_with_path in "${FILES[@]}"; do
|
|
|
|
|
|
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
2018-04-21 11:22:47 +02:00
|
|
|
|
|
2021-10-14 17:20:25 +01:00
|
|
|
|
paths[index]=$(dirname "$file_with_path")
|
|
|
|
|
|
|
|
|
|
|
|
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 "$@"
|