-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new hook for
terraform providers lock
operation (#173)
- Loading branch information
1 parent
f7e28e1
commit d69e86d
Showing
4 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eo pipefail | ||
|
||
main() { | ||
initialize_ | ||
parse_cmdline_ "$@" | ||
terraform_providers_lock_ | ||
} | ||
|
||
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")" | ||
|
||
# source getopt function | ||
# shellcheck source=lib_getopt | ||
. "$_SCRIPT_DIR/lib_getopt" | ||
} | ||
|
||
parse_cmdline_() { | ||
declare argv | ||
argv=$(getopt -o a: --long args: -- "$@") || return | ||
eval "set -- $argv" | ||
|
||
for argv; do | ||
case $argv in | ||
-a | --args) | ||
shift | ||
ARGS+=("$1") | ||
shift | ||
;; | ||
--) | ||
shift | ||
FILES=("$@") | ||
break | ||
;; | ||
esac | ||
done | ||
} | ||
|
||
terraform_providers_lock_() { | ||
local -a paths | ||
local index=0 | ||
local file_with_path | ||
|
||
for file_with_path in "${FILES[@]}"; do | ||
file_with_path="${file_with_path// /__REPLACED__SPACE__}" | ||
|
||
paths[index]=$(dirname "$file_with_path") | ||
|
||
((index += 1)) | ||
done | ||
|
||
local path_uniq | ||
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do | ||
path_uniq="${path_uniq//__REPLACED__SPACE__/ }" | ||
|
||
if [[ ! -d "${path_uniq}/.terraform" ]]; then | ||
set +e | ||
init_output=$(terraform -chdir="${path_uniq}" init -backend=false 2>&1) | ||
init_code=$? | ||
set -e | ||
|
||
if [[ $init_code != 0 ]]; then | ||
echo "Init before validation failed: $path_uniq" | ||
echo "$init_output" | ||
exit 1 | ||
fi | ||
fi | ||
|
||
terraform -chdir="${path_uniq}" providers lock "${ARGS[@]}" | ||
done | ||
} | ||
|
||
# global arrays | ||
declare -a ARGS | ||
declare -a FILES | ||
|
||
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@" |