Skip to content

Instantly share code, notes, and snippets.

@OriBenHur
Last active June 22, 2022 10:53
Show Gist options
  • Save OriBenHur/a3ddf6d674aceeb6fb3331643c06e30c to your computer and use it in GitHub Desktop.
Save OriBenHur/a3ddf6d674aceeb6fb3331643c06e30c to your computer and use it in GitHub Desktop.
terraform
function get_remote_digest() {
local repo="${1}"
local tag="${2}"
local token
token=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${repo}:pull" | jq -r '.token')
digest="$(curl -I -s -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/${repo}/manifests/${tag}" | sed 's/: /=/g' | grep -i docker-content-digest | cut -d= -f2 | tr -d '\r')"
echo "${digest}"
}
function get_digest() {
local repo="${1}"
digest=$(docker inspect --format='{{index .RepoDigests 0}}' "${repo}" | cut -d@ -f2)
echo "${digest}"
}
function terraform() {
local ARGS=() ENVIRONMENT=()
local EXTRA ENVS KEY VAL CHDIR EXTRA_ARGS LOCAL_ARGS=("$@")
if [[ ! "$PWD" =~ ${HOME} ]]; then
echo -e "\033[0;31mThis function can be used only from a folder that is part of the user home dir"
[[ $(type "$(where terraform | tail -1)") =~ "bash function" ]] && echo -e "install terraform and use it\033[0m" || echo -e "Use $(where terraform | tail -1) insted of terraform\033[0m"
return 1
fi
ENVS=( $(env | grep TF_) )
for ENV in "${ENVS[@]}"; do
KEY=$(cut -d= -f1 <<<"${ENV}")
VAL=$(cut -d= -f2 <<<"${ENV}")
if [[ $KEY =~ (PATH|FILE) ]]; then
mkdir -p "${VAL%/*}"
[[ $VAL =~ ${HOME} ]] && VAL="${VAL//$HOME//workspace}" || VAL="${PWD/#$HOME\//}/${VAL}"
fi
ENVIRONMENT+=("-e ${KEY}=${VAL}")
done
if [[ "${LOCAL_ARGS[@]}" =~ -chdir ]]; then
PDIR=$(echo "${LOCAL_ARGS[@]}" | grep -Eo 'chdir=.*' | cut -d' ' -f1 | cut -d = -f2)
[[ "${PDIR}" =~ $HOME ]] && CHDIR="${PDIR//$HOME//workspace}" || CHDIR="${PDIR/#$HOME\//}/${PDIR}"
ARGS+=("-chdir=${CHDIR}")
else
[[ "${PWD}" != "${HOME}" ]] && ARGS+=("-chdir=${PWD/#$HOME\//}")
fi
if [[ "${LOCAL_ARGS[@]}" =~ console ]]; then
EXTRA='-it'
elif [[ "${LOCAL_ARGS[@]}" =~ autocomplete ]]; then
command terraform "${LOCAL_ARGS[@]}"
return
else
EXTRA='-i'
fi
EXTRA_ARGS=(${LOCAL_ARGS[@]/"-chdir=${PDIR}"})
[[ "$(get_digest "hashicorp/terraform")" != "$(get_remote_digest "hashicorp/terraform" "latest")" ]] && docker pull hashicorp/terraform:latest
command docker run --platform linux/amd64 --rm -u "$(id -u)" ${EXTRA} \
-e "HOME=/workspace" \
$(echo ${ENVIRONMENT[*]}) \
-v "${HOME}:/workspace" \
-w /workspace \
hashicorp/terraform \
$(echo ${ARGS[*]}) $(echo ${EXTRA_ARGS[*]})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment