|
1 | 1 | #!/usr/bin/env bash
|
2 |
| -set -e |
| 2 | +set -eo pipefail |
3 | 3 |
|
4 |
| -declare -a paths |
5 |
| -declare -a tfvars_files |
| 4 | +main() { |
| 5 | + initialize_ |
| 6 | + parse_cmdline_ "$@" |
| 7 | + terraform_fmt_ |
| 8 | +} |
6 | 9 |
|
7 |
| -index=0 |
| 10 | +initialize_() { |
| 11 | + # get directory containing this script |
| 12 | + local dir |
| 13 | + local source |
| 14 | + source="${BASH_SOURCE[0]}" |
| 15 | + while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink |
| 16 | + dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)" |
| 17 | + source="$(readlink "$source")" |
| 18 | + # if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located |
| 19 | + [[ $source != /* ]] && source="$dir/$source" |
| 20 | + done |
| 21 | + _SCRIPT_DIR="$(dirname "$source")" |
8 | 22 |
|
9 |
| -for file_with_path in "$@"; do |
10 |
| - file_with_path="${file_with_path// /__REPLACED__SPACE__}" |
| 23 | + # source getopt function |
| 24 | + # shellcheck source=lib_getopt |
| 25 | + . "$_SCRIPT_DIR/lib_getopt" |
| 26 | +} |
11 | 27 |
|
12 |
| - paths[index]=$(dirname "$file_with_path") |
| 28 | +parse_cmdline_() { |
| 29 | + declare argv |
| 30 | + argv=$(getopt -o a: --long args: -- "$@") || return |
| 31 | + eval "set -- $argv" |
13 | 32 |
|
14 |
| - if [[ "$file_with_path" == *".tfvars" ]]; then |
15 |
| - tfvars_files+=("$file_with_path") |
16 |
| - fi |
| 33 | + for argv; do |
| 34 | + case $argv in |
| 35 | + -a | --args) |
| 36 | + shift |
| 37 | + ARGS+=("$1") |
| 38 | + shift |
| 39 | + ;; |
| 40 | + --) |
| 41 | + shift |
| 42 | + FILES=("$@") |
| 43 | + break |
| 44 | + ;; |
| 45 | + esac |
| 46 | + done |
| 47 | +} |
17 | 48 |
|
18 |
| - let "index+=1" |
19 |
| -done |
| 49 | +terraform_fmt_() { |
20 | 50 |
|
21 |
| -for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do |
22 |
| - path_uniq="${path_uniq//__REPLACED__SPACE__/ }" |
| 51 | + declare -a paths |
| 52 | + declare -a tfvars_files |
23 | 53 |
|
24 |
| - pushd "$path_uniq" > /dev/null |
25 |
| - terraform fmt |
26 |
| - popd > /dev/null |
27 |
| -done |
| 54 | + index=0 |
28 | 55 |
|
29 |
| -# terraform.tfvars are excluded by `terraform fmt` |
30 |
| -for tfvars_file in "${tfvars_files[@]}"; do |
31 |
| - tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }" |
| 56 | + for file_with_path in "${FILES[@]}"; do |
| 57 | + file_with_path="${file_with_path// /__REPLACED__SPACE__}" |
32 | 58 |
|
33 |
| - terraform fmt "$tfvars_file" |
34 |
| -done |
| 59 | + paths[index]=$(dirname "$file_with_path") |
| 60 | + |
| 61 | + if [[ "$file_with_path" == *".tfvars" ]]; then |
| 62 | + tfvars_files+=("$file_with_path") |
| 63 | + fi |
| 64 | + |
| 65 | + ((index += 1)) |
| 66 | + done |
| 67 | + |
| 68 | + for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do |
| 69 | + path_uniq="${path_uniq//__REPLACED__SPACE__/ }" |
| 70 | + |
| 71 | + ( |
| 72 | + cd "$path_uniq" |
| 73 | + terraform fmt "${ARGS[@]}" |
| 74 | + ) |
| 75 | + done |
| 76 | + |
| 77 | + # terraform.tfvars are excluded by `terraform fmt` |
| 78 | + for tfvars_file in "${tfvars_files[@]}"; do |
| 79 | + tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }" |
| 80 | + |
| 81 | + terraform fmt "${ARGS[@]}" "$tfvars_file" |
| 82 | + done |
| 83 | +} |
| 84 | + |
| 85 | +# global arrays |
| 86 | +declare -a ARGS=() |
| 87 | +declare -a FILES=() |
| 88 | + |
| 89 | +[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@" |
0 commit comments