Commit 023ca7d1 authored by nimrod's avatar nimrod
Browse files

Refactor the Terraform aliases and functions.

First of all, a lot of common code is now in a single file (the tf shell
script). Another benefit is that the `tf` script can be used by others,
as a way to make workspaces and variable files easier.
parent 70e67c2f
Loading
Loading
Loading
Loading
+4 −52
Original line number Diff line number Diff line
@@ -146,8 +146,10 @@ alias screenshot-cleanup='find "$HOME/Pictures" -name "Screenshot from *.png" -d
alias smtp-server='python3 -m smtpd -ndc DebuggingServer'
alias sudo="sudo "
alias sudome="sudome "
alias tfa='terraform apply tfplan'
alias tfvf='tfv && terraform fmt -diff'
alias tfa='tf apply tfplan'
alias tfaa='tf apply -auto-approve'
alias tfp='tf plan -out tfplan'
alias tfvf='tf validate && tf fmt -diff'
alias todo="vim \$HOME/Documents/TODO.yml"
# shellcheck disable=SC2142
alias tolower='awk "{print tolower(\$0)}"'
@@ -257,56 +259,6 @@ sync_podcasts () (
    unison podcasts
)

tfaa () {
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
    then
        terraform apply -auto-approve "$@"
    else
        terraform apply -auto-approve -var-file "$workspace.tfvars" "$@"
    fi
}

tfi () {
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
    then
        terraform import "$@"
    else
        terraform import -var-file "$workspace.tfvars" "$@"
    fi
}

tfp () {
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
    then
        terraform plan -out tfplan "$@"
    else
        terraform plan -out tfplan -var-file "$workspace.tfvars" "$@"
    fi
}

tfr () {
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
    then
        terraform refresh "$@"
    else
        terraform refresh -var-file "$workspace.tfvars" "$@"
    fi
}

tfv () {
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
    then
        terraform validate "$@"
    else
        terraform validate -var-file "$workspace.tfvars" "$@"
    fi
}

toux () {
    touch "$@"
    chmod +x "$@"

Documents/bin/tf

0 → 100644
+21 −0
Original line number Diff line number Diff line
#!/bin/sh
set -eu

# A wrapper for Terraform to include a variables file is one exists that matches
# the workspace name (on commands that support that).

workspace="$(terraform workspace show)"
if [ ! -f "$workspace.tfvars" ]
then
    exec terraform "$@"
fi

for arg in "$@"
do
    if [ "$arg" = "plan" ] || [ "$arg" = "console" ] || [ "$arg" = "import" ] ||
        [ "$arg" = "refresh" ]
    then
        exec terraform "$@" "-var-file=$workspace.tfvars"
    fi
done
exec terraform "$@"