Skip to content

Commit

Permalink
terraform_validate: Support passing of env vars and args
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbowes committed Jul 6, 2020
1 parent 6c1e13c commit a7afaf6
Showing 1 changed file with 102 additions and 37 deletions.
139 changes: 102 additions & 37 deletions terraform_validate.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,115 @@
#!/usr/bin/env bash
set -e

declare -a paths
index=0
error=0
main() {
initialize_
parse_cmdline_ "$@"
terraform_validate_
}

for file_with_path in "$@"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
initialize_() {
# get directory containing this script
local dir
local source
source="${BASH_SOURCE[0]}"
while [[ -h $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")"

paths[index]=$(dirname "$file_with_path")
(("index+=1"))
done
# source getopt function
# shellcheck source=lib_getopt
. "$_SCRIPT_DIR/lib_getopt"
}

for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
parse_cmdline_() {
declare argv
argv=$(getopt -o e:a: --long envs:args: -- "$@") || return
eval "set -- $argv"

if [[ -n "$(find $path_uniq -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
for argv; do
case $argv in
-a | --args)
shift
ARGS+=("$1")
shift
;;
-e | --envs)
shift
ENVS+=("$1")
shift
;;
--)
shift
FILES=("$@")
break
;;
esac
done
}

starting_path=$(realpath "$path_uniq")
terraform_path="$path_uniq"
terraform_validate_() {

# Find the relevant .terraform directory (indicating a 'terraform init'),
# but fall through to the current directory.
while [[ "$terraform_path" != "." ]]; do
if [[ -d "$terraform_path/.terraform" ]]; then
break
else
terraform_path=$(dirname "$terraform_path")
# Setup environment variables
for var in "${ENVS[@]}"; do
export "${!var}"
done

declare -a paths
index=0
error=0

for file_with_path in "${FILES[@]}"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"

paths[index]=$(dirname "$file_with_path")
((index+=1))
done

for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"

if [[ -n "$(find "$path_uniq" -maxdepth 1 -name '*.tf' -print -quit)" ]]; then

starting_path=$(realpath "$path_uniq")
terraform_path="$path_uniq"

# Find the relevant .terraform directory (indicating a 'terraform init'),
# but fall through to the current directory.
while [[ $terraform_path != "." ]]; do
if [[ -d $terraform_path/.terraform ]]; then
break
else
terraform_path=$(dirname "$terraform_path")
fi
done

validate_path="${path_uniq#"$terraform_path"}"

# Change to the directory that has been initialized, run validation, then
# change back to the starting directory.
cd "$(realpath "$terraform_path")"
if ! terraform validate "${ARGS[@]}" "$validate_path"; then
error=1
echo
echo "Failed path: $path_uniq"
echo "================================"
fi
done

validate_path="${path_uniq#"$terraform_path"}"

# Change to the directory that has been initialized, run validation, then
# change back to the starting directory.
cd "$(realpath "$terraform_path")"
if ! terraform validate $validate_path; then
error=1
echo
echo "Failed path: $path_uniq"
echo "================================"
cd "$starting_path"
fi
cd "$starting_path"
done

if [[ $error -ne 0 ]]; then
exit 1
fi
done
}

# global arrays
declare -a ARGS
declare -a ENVS
declare -a FILES

if [[ "${error}" -ne 0 ]]; then
exit 1
fi
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"

0 comments on commit a7afaf6

Please sign in to comment.