Skip to content

Commit

Permalink
extends terraform apply to automatically do terraform init
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Gohlke committed Jan 26, 2024
1 parent 88f0845 commit 1004ed0
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tools/terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ mkdir -p "$TF_PLUGIN_CACHE_DIR"
# enable completion
complete -C "$(command -v terraform)" terraform

alias tf_apply='terraform apply -auto-approve'
alias tf_destroy='terraform destroy'
alias tf_init='terraform init'
alias tf_import='terraform import'
Expand All @@ -33,6 +32,29 @@ alias tf_state_rm='terraform state rm'
alias tf_taint='terraform taint'
alias tf_validate='terraform validate'

# terraform apply -auto-approve ...
# catches missing 'terraform init'
function tf_apply {
local tmp_err=$(mktemp)
# redirect stderr to file and stderr
# to grep error message
(
# shellcheck disable=SC2068
terraform apply -auto-approve $@
) 2> >(tee "$tmp_err" 2>&2)

local exit_code=$?
if [[ $exit_code -gt 0 ]]; then
if grep -q "Module not installed" "$tmp_err"; then
echo "ℹ️ HINT: missing 'terraform init' ... I'll do it 🚕"
tf_init
echo "ℹ️ HINT: retry 'terraform apply --auto-approve $*' ... I'll do it 🚕"
# shellcheck disable=SC2068
terraform apply -auto-approve $@
fi
fi
}

function tf_pin_provider_versions {

local versions="versions.tf"
Expand Down

0 comments on commit 1004ed0

Please sign in to comment.