diff --git a/bin/terraform b/bin/terraform index 1259d99..08137ce 100755 --- a/bin/terraform +++ b/bin/terraform @@ -1,37 +1,66 @@ #!/usr/bin/env bash set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ -n "${TFENV_DEBUG:-""}" ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ -n "${TFENV_DEBUG:-""}" ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ -n "${TFENV_DEBUG:-""}" ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + log 'debug' "program=\"${0##*/}\""; -declare tfenv_path="$(dirname "$(command -v "${0}")")/tfenv"; +declare tfenv_path="${TFENV_ROOT}/bin/tfenv"; log 'debug' "Exec: \"${tfenv_path}\" exec \"${@}\""; exec "${tfenv_path}" exec "${@}" \ diff --git a/bin/tfenv b/bin/tfenv index 11c2908..ccc135d 100755 --- a/bin/tfenv +++ b/bin/tfenv @@ -1,7 +1,14 @@ #!/usr/bin/env bash -set -euo pipefail; +set -uo pipefail; -declare arg="${1:-""}"; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; if [ -z "${TFENV_ROOT:-""}" ]; then # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac @@ -10,35 +17,48 @@ if [ -z "${TFENV_ROOT:-""}" ]; then local file_name; while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})"; - file_name="$(basename "${target_file}")"; + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; target_file="$(readlink "${file_name}")"; done; echo "$(pwd -P)/${file_name}"; - } + }; TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - TFENV_ROOT="${TFENV_ROOT%/}" -fi + TFENV_ROOT="${TFENV_ROOT%/}"; +fi; export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ -n "${TFENV_DEBUG:-""}" ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; -log 'debug' "Prepending ${TFENV_ROOT}/libexec to PATH"; -PATH="${TFENV_ROOT}/libexec:${PATH}"; -export PATH; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + +declare arg="${1:-""}"; log 'debug' "Setting TFENV_DIR to ${PWD}"; export TFENV_DIR="${PWD}" @@ -52,8 +72,9 @@ abort() { echo "tfenv: ${*}"; fi; } >&2; - exit 1; -} +}; + +log 'debug' "tfenv argument is: ${arg}"; case "${arg}" in "") @@ -61,7 +82,8 @@ case "${arg}" in { tfenv---version; tfenv-help; - } | abort; + } | abort && exit 1; +exit 1; ;; -v | --version ) log 'debug' 'tfenv version requested...'; @@ -79,7 +101,7 @@ case "${arg}" in { echo "No such command '${arg}'"; tfenv-help; - } | abort; + } | abort && exit 1; fi; shift 1; log 'debug' "Exec: \"${command_path}\" \"${@}\""; diff --git a/lib/bashlog.sh b/lib/bashlog.sh index 4f4aa0c..3499cf6 100755 --- a/lib/bashlog.sh +++ b/lib/bashlog.sh @@ -147,7 +147,7 @@ function log() { 'error') echo -e "${std_line}" >&2; if [ "${debug_level}" -gt 1 ]; then - echo -e "Here's a shell to debug with. 'exit 0' to continue. Other exit codes will abort - parent shell will terminate."; + echo -e "Here's a shell for debugging the current environment. 'exit 0' to resume script from here. Non-zero exit code will abort - parent shell will terminate." >&2; bash || exit "${?}"; else exit 1; diff --git a/lib/helpers.sh b/lib/helpers.sh index d1e7854..07477e7 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -2,6 +2,28 @@ set -uo pipefail; +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; +else + TFENV_ROOT="${TFENV_ROOT%/}"; +fi; +export TFENV_ROOT; + if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then [ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ] || export DEBUG="${TFENV_DEBUG:-0}"; if [[ "${TFENV_DEBUG}" -gt 2 ]]; then @@ -10,7 +32,6 @@ if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then fi; fi; -[ -n "${TFENV_ROOT:-""}" ] || export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)" source "${TFENV_ROOT}/lib/bashlog.sh"; # Curl wrapper to switch TLS option for each OS @@ -28,7 +49,7 @@ export -f curlw; check_version() { v="${1}"; - [ -n "$(terraform --version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ]; + [ -n "$(${TFENV_ROOT}/bin/terraform --version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ]; } export -f check_version; diff --git a/libexec/tfenv---version b/libexec/tfenv---version index 760d34c..6bc8e28 100755 --- a/libexec/tfenv---version +++ b/libexec/tfenv---version @@ -11,34 +11,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + log 'debug' 'Scraping tfenv version from CHANGELOG.md'; version="$(awk '/^##/{ print $2; exit}' "${TFENV_ROOT}/CHANGELOG.md")" \ && log 'debug' "Found version '${version}' in CHANGELOG.md" \ diff --git a/libexec/tfenv-exec b/libexec/tfenv-exec index 04876a6..a1359dc 100755 --- a/libexec/tfenv-exec +++ b/libexec/tfenv-exec @@ -15,34 +15,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}"; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}"; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + log 'debug' 'Getting version from tfenv-version-name'; TFENV_VERSION="$(tfenv-version-name)" \ && log 'debug' "TFENV_VERSION is ${TFENV_VERSION}" \ diff --git a/libexec/tfenv-help b/libexec/tfenv-help index a21bccc..49239bb 100755 --- a/libexec/tfenv-help +++ b/libexec/tfenv-help @@ -2,34 +2,6 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; -else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; -fi; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; - fi; -fi; - echo 'Usage: tfenv [] Commands: diff --git a/libexec/tfenv-install b/libexec/tfenv-install index dfafdf7..50efff9 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -2,34 +2,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + [ "${#}" -gt 1 ] && log 'error' 'usage: tfenv install []'; declare version_requested version regex; @@ -37,13 +66,26 @@ declare arg="${1:-""}"; if [ -z "${arg}" ]; then version_file="$(tfenv-version-file)"; + log 'debug' "Version File: ${version_file}"; if [ "${version_file}" != "${TFENV_ROOT}/version" ]; then - version_requested="$(cat "${version_file}" || true)"; + log 'debug' "Version File (${version_file}) is not the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)"; + version_requested="$(cat "${version_file}")" \ + || log 'error' "Failed to open ${version_file}"; + elif [ -f "${version_file}" ]; then + log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)"; + version_requested="$(cat "${version_file}")" \ + || log 'error' "Failed to open ${version_file}"; + else + log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version) but it doesn't exist"; + log 'info' 'No version requested on the command line or in the version file search path. Installing "latest"'; + version_requested='latest'; fi; else version_requested="${arg}"; fi; +log 'debug' "Version Requested: ${version_requested}"; + if [[ "${version_requested}" =~ ^min-required$ ]]; then log 'info' 'Detecting minimal required version...'; found_min_required="$(tfenv-min-required)"; @@ -67,7 +109,10 @@ else regex="^${version_requested}$"; fi; -[ -n "${version}" ] || log 'error' 'Version is not specified'; +[ -n "${version}" ] || log 'error' 'Version is not specified. This should not be possible as we default to latest'; + +log 'debug' "Processing install for version ${version}, using regex ${regex}"; + version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"; [ -n "${version}" ] || log 'error' "No versions matching '${arg}' found in remote"; diff --git a/libexec/tfenv-list b/libexec/tfenv-list index fbcd0d8..b8e232a 100755 --- a/libexec/tfenv-list +++ b/libexec/tfenv-list @@ -2,34 +2,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + [ "${#}" -ne 0 ] \ && log 'error' "usage: tfenv list" diff --git a/libexec/tfenv-list-remote b/libexec/tfenv-list-remote index d9018cc..7b59d6b 100755 --- a/libexec/tfenv-list-remote +++ b/libexec/tfenv-list-remote @@ -2,34 +2,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + if [ "${#}" -ne 0 ];then echo "usage: tfenv list-remote" 1>&2 exit 1; diff --git a/libexec/tfenv-min-required b/libexec/tfenv-min-required index 12a9813..b886e93 100755 --- a/libexec/tfenv-min-required +++ b/libexec/tfenv-min-required @@ -4,34 +4,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + bailout() { log 'error' 'Error: Could not determine required_version based on your terraform sources. Make sure at least one of your *tf files includes a required version section like @@ -45,7 +74,7 @@ see https://www.terraform.io/docs/configuration/terraform.html for details'; find_min_required() { local root="${1}"; - versions="$(grep -h -R required_version --include '*tf' "$root"/* | tr -c -d '0-9. ~=!<>' )"; + versions="$(grep -h -R required_version --include '*tf' "${root}"/* | tr -c -d '0-9. ~=!<>' )"; if [[ "${versions}" =~ ([~=!<>]{0,2}[[:blank:]]*[0-9]+[0-9.]+)[^0-9]* ]]; then found_min_required="${BASH_REMATCH[1]}"; @@ -64,4 +93,4 @@ find_min_required() { bailout; }; -find_min_required "${TFENV_DIR}"; +find_min_required "${TFENV_DIR:-$(pwd)}"; diff --git a/libexec/tfenv-uninstall b/libexec/tfenv-uninstall index f6a7ce7..19a506e 100755 --- a/libexec/tfenv-uninstall +++ b/libexec/tfenv-uninstall @@ -2,45 +2,92 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + [ "${#}" -gt 1 ] && log 'error' 'usage: tfenv uninstall []'; declare version_requested version regex; +declare arg="${1:-""}"; -if [ -z "${1}" ]; then +if [ -z "${arg:-""}" ]; then version_file="$(tfenv-version-file)"; + log 'debug' "Version File: ${version_file}"; if [ "${version_file}" != "${TFENV_ROOT}/version" ]; then - version_requested="$(cat "${version_file}" || true)"; + log 'debug' "Version File (${version_file}) is not the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)"; + version_requested="$(cat "${version_file}")" \ + || log 'error' "Failed to open ${version_file}"; + elif [ -f "${version_file}" ]; then + log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)"; + version_requested="$(cat "${version_file}")" \ + || log 'error' "Failed to open ${version_file}"; + else + log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version) but it doesn't exist"; + log 'info' 'No version requested on the command line or in the version file search path. Installing "latest"'; + version_requested='latest'; fi; else - version_requested="${1}"; + version_requested="${arg}"; +fi; + +log 'debug' "Version Requested: ${version_requested}"; + +if [[ "${version_requested}" =~ ^min-required$ ]]; then + log 'error' 'min-required is an unsupported option for uninstall'; fi; if [[ "${version_requested}" =~ ^latest\:.*$ ]]; then @@ -54,9 +101,12 @@ else regex="^${version_requested}$"; fi; -[ -n "${version}" ] || log 'error' "Version is not specified"; +[ -z "${version:-""}" ] && log 'error' "Version not specified on the command line or on version file search path."; + +log 'debug' "Processing uninstall for version ${version}, using regex ${regex}"; + version="$(tfenv-list | sed -E 's/^(\*| )? //g; s/ \(set by .+\)$//' | grep -e "${regex}" | head -n 1)"; -[ -n "${version}" ] || log 'error' "No versions matching '${1}' found in local"; +[ -n "${version}" ] || log 'error' "No versions matching '${regex}' found in local"; dst_path="${TFENV_ROOT}/versions/${version}"; if [ -f "${dst_path}/terraform" ]; then diff --git a/libexec/tfenv-use b/libexec/tfenv-use index 3de9b03..b07dcca 100755 --- a/libexec/tfenv-use +++ b/libexec/tfenv-use @@ -2,34 +2,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + [ "${#}" -ne 1 ] && log 'error' 'usage: tfenv use '; declare version_requested version regex min_required version_file; diff --git a/libexec/tfenv-version-file b/libexec/tfenv-version-file index 0f1f360..7dfb976 100755 --- a/libexec/tfenv-version-file +++ b/libexec/tfenv-version-file @@ -4,34 +4,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + find_local_version_file() { log 'debug' "Looking for a version file in ${1}"; local root="${1}"; diff --git a/libexec/tfenv-version-name b/libexec/tfenv-version-name index 471204f..8e5f501 100755 --- a/libexec/tfenv-version-name +++ b/libexec/tfenv-version-name @@ -3,34 +3,63 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +# Ensure libexec and bin are in $PATH +for dir in libexec bin; do + case ":${PATH}:" in + *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";; + *) + log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now"; + export PATH="${TFENV_ROOT}/${dir}:${PATH}"; + ;; + esac; +done; + +##################### +# Begin Script Body # +##################### + [ -d "${TFENV_ROOT}/versions" ] \ || log 'error' 'No versions of terraform installed. Please install one with: tfenv install'; diff --git a/test/run.sh b/test/run.sh index d50d443..6abb640 100755 --- a/test/run.sh +++ b/test/run.sh @@ -2,34 +2,52 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +##################### +# Begin Script Body # +##################### + export PATH="${TFENV_ROOT}/bin:${PATH}"; errors=(); diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index 43874fe..3ed0181 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -2,34 +2,52 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +##################### +# Begin Script Body # +##################### + test_install_and_use() { # Takes a static version and the optional keyword to install it with local k="${2-""}"; @@ -76,7 +94,7 @@ for desc in "${!string_tests[@]}"; do kv="${string_tests[${desc}]}"; v="${kv%,*}"; k="${kv##*,}"; - log 'info' "## ./.terraform-version Test ${test_num}/${string_tests[*]}: ${desc} ( ${k} / ${v} )"; + log 'info' "## ./.terraform-version Test ${test_num}/${#string_tests[*]}: ${desc} ( ${k} / ${v} )"; log 'info' "Writing ${k} to ./.terraform-version"; echo "${k}" > ./.terraform-version; test_install_and_use "${v}" \ @@ -86,7 +104,7 @@ for desc in "${!string_tests[@]}"; do done; cleanup || log 'error' 'Cleanup failed?!'; -log 'info' '## \${HOME}/.terraform-version Test Preparation'; +log 'info' '## ${HOME}/.terraform-version Test Preparation'; declare v1="$(tfenv list-remote | grep -e "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | head -n 2 | tail -n 1)"; declare v2="$(tfenv list-remote | grep -e "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | head -n 1)"; if [ -f "${HOME}/.terraform-version" ]; then diff --git a/test/test_list.sh b/test/test_list.sh index 905c143..365555f 100755 --- a/test/test_list.sh +++ b/test/test_list.sh @@ -2,34 +2,52 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +##################### +# Begin Script Body # +##################### + declare -a errors=(); log 'info' '### List local versions'; diff --git a/test/test_symlink.sh b/test/test_symlink.sh index 012fb61..3a5cc69 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -2,34 +2,52 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +##################### +# Begin Script Body # +##################### + declare -a errors=(); log 'info' '### Testing symlink functionality'; @@ -39,16 +57,14 @@ log 'info' "## Creating/clearing ${TFENV_BIN_DIR}" rm -rf "${TFENV_BIN_DIR}" && mkdir "${TFENV_BIN_DIR}"; log 'info' "## Symlinking ${PWD}/bin/* into ${TFENV_BIN_DIR}"; ln -s "${PWD}"/bin/* "${TFENV_BIN_DIR}"; -log 'info' "## Adding ${TFENV_BIN_DIR} to \$PATH"; -export PATH="${TFENV_BIN_DIR}:${PATH}"; cleanup || log 'error' 'Cleanup failed?!'; log 'info' '## Installing 0.8.2'; -tfenv install 0.8.2 || error_and_proceed 'Install failed'; +${TFENV_BIN_DIR}/tfenv install 0.8.2 || error_and_proceed 'Install failed'; log 'info' '## Using 0.8.2'; -tfenv use 0.8.2 || error_and_proceed 'Use failed'; +${TFENV_BIN_DIR}/tfenv use 0.8.2 || error_and_proceed 'Use failed'; log 'info' '## Check-Version for 0.8.2'; check_version 0.8.2 || error_and_proceed 'Version check failed'; diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index b286c8b..e03cc04 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -2,34 +2,52 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +##################### +# Begin Script Body # +##################### + declare -a errors=(); function test_uninstall() { @@ -50,7 +68,7 @@ tests['0.11.15-oci']='0.11.15-oci'; tests['latest']="$(tfenv list-remote | head -n1)"; tests['latest:^0.8']="$(tfenv list-remote | grep -e "^0.8" | head -n1)"; -declare test_num=1; +declare -i test_num=1; for k in "${!tests[@]}"; do log 'info' "Test ${test_num}/${#tests[@]}: Testing uninstall of version ${tests[${k}]} via keyword ${k}"; test_uninstall "${k}" "${tests[${k}]}" \ diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index 16ed6e0..c0a8b11 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -2,34 +2,52 @@ set -uo pipefail; -# Ensure we can execute standalone -if [ -n "${TFENV_ROOT:-""}" ]; then - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2; - fi; +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; else - export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"; - if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then - [ -n "${TFENV_HELPERS:-""}" ] \ - && log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \ - || echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2; - fi; + TFENV_ROOT="${TFENV_ROOT%/}"; fi; +export TFENV_ROOT; if [ -n "${TFENV_HELPERS:-""}" ]; then log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; else - [ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; if source "${TFENV_ROOT}/lib/helpers.sh"; then log 'debug' 'Helpers sourced successfully'; else - echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2; - exit 1; + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; fi; fi; +##################### +# Begin Script Body # +##################### + declare -a errors=(); log 'info' '### Install not min-required version';