Skip to content

Commit

Permalink
feat: A few wrappers to work better with Terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed Oct 1, 2024
1 parent ec0fad3 commit 5fc3311
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions roles/dotfiles/files/shell/zsh.d/terraform.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
set -o pipefail

#
# General utilities
#
utils::panic() {
local -r message="${1}"
local -r exit_code="${2}"

[[ -n "${message}" ]] && echo -e "${message}"
return "${exit_code}"
}

#
# Configurations
#
DEFAULT_CLOUD_DEPLOYMENT_WORKSPACE="${HOME}/workspace/ibm/quantum/projects/infra/cloud-deployment"
DEFAULT_PLAN_FILENAME="this.tfplan"

CLOUD_DEPLOYMENT_WORKSPACE=${CLOUD_DEPLOYMENT_WORKSPACE:-${DEFAULT_CLOUD_DEPLOYMENT_WORKSPACE}}
PLAN_FILENAME=${PLAN_FILENAME:-${DEFAULT_PLAN_FILENAME}}

[[ -d "${CLOUD_DEPLOYMENT_WORKSPACE}" ]] || utils::panic "Warning: I couldn't find the ${CLOUD_DEPLOYMENT_WORKSPACE}" 1
[[ -x "${CLOUD_DEPLOYMENT_WORKSPACE}/tools/tfinit.sh" ]] || utils::panic "Warning: I couldn't find the ${CLOUD_DEPLOYMENT_WORKSPACE}/tfinit.sh script" 2


#
# Wrappers
#
terraform::state::init() {
${CLOUD_DEPLOYMENT_WORKSPACE}/tools/tfinit.sh
}

terraform::state::cleanup() {
[[ -d ".terraform" ]] && rm -fr .terraform
[[ -f "${PLAN_FILENAME}" ]] && rm -f "${PLAN_FILENAME}"
}

terraform::state::upgrade() {
terraform::state::cleanup
${CLOUD_DEPLOYMENT_WORKSPACE}/tools/tfinit.sh --upgrade --force
}

terraform::plan() {
terraform plan --lock=false --out="${PLAN_FILENAME}"
}

terraform::apply() {
[[ -f "${PLAN_FILENAME}" ]] && terraform apply "${PLAN_FILENAME}"
}


# autoloads
autoload terraform::state::init
autoload terraform::state::cleanup
autoload terraform::state::upgrade
autoload terraform::plan
autoload terraform::apply


# aliases
alias tf='terraform'
alias tfi='terraform::state::init'
alias tfc='terraform::state::cleanup'
alias tfu='terraform::state::upgrade'
alias tfp='terraform::plan'
alias tfa='terraform::apply'

0 comments on commit 5fc3311

Please sign in to comment.