From f4d9f1edb4eefacfaa0c9e56a972f64b3f4ee16e Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Wed, 27 Jul 2022 19:22:26 +0100 Subject: [PATCH 1/5] Add test to reproduce #354 --- test/test_use_minrequired.sh | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index b53b2fa..c39ea99 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -121,7 +121,7 @@ echo 'min-required' > .terraform-version; cleanup || log 'error' 'Cleanup failed?!'; -log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir'; +log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir with rel path'; minv='1.1.0'; @@ -138,6 +138,24 @@ echo 'min-required' > chdir-dir/.terraform-version cleanup || log 'error' 'Cleanup failed?!'; + +log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir with abs path'; + +minv='1.2.3'; + +mkdir -p chdir-dir +echo "terraform { + required_version = \">=${minv}\" +}" >> chdir-dir/min_required.tf; +echo 'min-required' > chdir-dir/.terraform-version + +( + TFENV_AUTO_INSTALL=true terraform -chdir="${PWD}/chdir-dir" version; + check_active_version "${minv}" chdir-dir; +) || error_and_proceed 'Min required version from -chdir does not match'; + +cleanup || log 'error' 'Cleanup failed?!'; + if [ "${#errors[@]}" -gt 0 ]; then log 'warn' '===== The following use_minrequired tests failed ====='; for error in "${errors[@]}"; do From 4e3c605897a527a117095b5db947ee2b50b36a4f Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Wed, 27 Jul 2022 19:35:04 +0100 Subject: [PATCH 2/5] Fix use of -chdir with an absolute path Fixes #354. --- lib/tfenv-exec.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/tfenv-exec.sh b/lib/tfenv-exec.sh index 189d222..6649df7 100644 --- a/lib/tfenv-exec.sh +++ b/lib/tfenv-exec.sh @@ -5,8 +5,10 @@ set -uo pipefail; function tfenv-exec() { for _arg in ${@:1}; do if [[ "${_arg}" == -chdir=* ]]; then - log 'debug' "Found -chdir arg. Setting TFENV_DIR to: ${_arg#-chdir=}"; - export TFENV_DIR="${PWD}/${_arg#-chdir=}"; + chdir="${_arg#-chdir=}"; + log 'debug' "Found -chdir arg: ${chdir}"; + export TFENV_DIR="${PWD}/$(realpath --relative-to="${PWD}" "$chdir")"; + log 'debug' "Setting TFENV_DIR to: ${TFENV_DIR}"; fi; done; From 9cb216c4118a01748f9cbbb031c90f614f2e32b4 Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Fri, 4 Nov 2022 22:05:45 +0000 Subject: [PATCH 3/5] Fix realpath not available on macOS --- lib/tfenv-exec.sh | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/tfenv-exec.sh b/lib/tfenv-exec.sh index 6649df7..66af4cd 100644 --- a/lib/tfenv-exec.sh +++ b/lib/tfenv-exec.sh @@ -2,12 +2,38 @@ set -uo pipefail; +function realpath-relative-to() { + # A basic implementation of GNU `realpath --relative-to=$1 $2` + # that can also be used on macOS. + + # 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 \$(${target_file%/*})'"; + file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + local relative_to="$(readlink_f "${1}")"; + local path="$(readlink_f "${2}")"; + + echo "${path#"${relative_to}/"}"; + return 0; +} +export -f realpath-relative-to; + function tfenv-exec() { for _arg in ${@:1}; do if [[ "${_arg}" == -chdir=* ]]; then chdir="${_arg#-chdir=}"; log 'debug' "Found -chdir arg: ${chdir}"; - export TFENV_DIR="${PWD}/$(realpath --relative-to="${PWD}" "$chdir")"; + export TFENV_DIR="${PWD}/$(realpath-relative-to "${PWD}" "${chdir}")"; log 'debug' "Setting TFENV_DIR to: ${TFENV_DIR}"; fi; done; From 23aea386b9deab7551f7458221cecbf34ae5b93d Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Fri, 4 Nov 2022 22:47:37 +0000 Subject: [PATCH 4/5] Refactor readlink_f to common helpers --- bin/terraform | 13 ------------- bin/tfenv | 13 ------------- lib/helpers.sh | 28 +++++++++++++++------------- lib/tfenv-exec.sh | 15 --------------- libexec/tfenv---version | 14 -------------- libexec/tfenv-exec | 14 -------------- libexec/tfenv-install | 14 -------------- libexec/tfenv-list | 14 -------------- libexec/tfenv-list-remote | 14 -------------- libexec/tfenv-min-required | 14 -------------- libexec/tfenv-pin | 14 -------------- libexec/tfenv-resolve-version | 14 -------------- libexec/tfenv-uninstall | 14 -------------- libexec/tfenv-use | 14 -------------- libexec/tfenv-version-file | 14 -------------- libexec/tfenv-version-name | 14 -------------- test/run.sh | 14 -------------- test/test_install_and_use.sh | 14 -------------- test/test_list.sh | 14 -------------- test/test_symlink.sh | 14 -------------- test/test_uninstall.sh | 14 -------------- test/test_use_latestallowed.sh | 14 -------------- test/test_use_minrequired.sh | 14 -------------- 23 files changed, 15 insertions(+), 320 deletions(-) diff --git a/bin/terraform b/bin/terraform index cec01d2..c613df9 100755 --- a/bin/terraform +++ b/bin/terraform @@ -11,19 +11,6 @@ function early_death() { }; 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 "${target_file%/*}" || early_death "Failed to 'cd \$(${target_file%/*})' while trying to determine TFENV_ROOT"; - file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; TFENV_SHIM=$(readlink_f "${0}") TFENV_ROOT="${TFENV_SHIM%/*/*}"; [ -n "${TFENV_ROOT}" ] || early_death "Failed to determine TFENV_ROOT"; diff --git a/bin/tfenv b/bin/tfenv index 28c24a1..d2fe313 100755 --- a/bin/tfenv +++ b/bin/tfenv @@ -11,19 +11,6 @@ function early_death() { }; 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 "${target_file%/*}" || early_death "Failed to 'cd \$(${target_file%/*})' while trying to determine TFENV_ROOT"; - file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; TFENV_SHIM=$(readlink_f "${0}") TFENV_ROOT="${TFENV_SHIM%/*/*}"; [ -n "${TFENV_ROOT}" ] || early_death "Failed to determine TFENV_ROOT"; diff --git a/lib/helpers.sh b/lib/helpers.sh index 535232d..695dd7b 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -2,20 +2,22 @@ set -uo pipefail; +# 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 "${target_file%/*}" || early_death "Failed to 'cd \$(${target_file%/*})'"; + file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; +}; +export -f readlink_f; + 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 "${target_file%/*}" || early_death "Failed to 'cd \$(${target_file%/*})' while trying to determine TFENV_ROOT"; - file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; TFENV_SHIM=$(readlink_f "${0}") TFENV_ROOT="${TFENV_SHIM%/*/*}"; [ -n "${TFENV_ROOT}" ] || early_death "Failed to determine TFENV_ROOT"; diff --git a/lib/tfenv-exec.sh b/lib/tfenv-exec.sh index 66af4cd..d0bd247 100644 --- a/lib/tfenv-exec.sh +++ b/lib/tfenv-exec.sh @@ -5,21 +5,6 @@ set -uo pipefail; function realpath-relative-to() { # A basic implementation of GNU `realpath --relative-to=$1 $2` # that can also be used on macOS. - - # 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 \$(${target_file%/*})'"; - file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - local relative_to="$(readlink_f "${1}")"; local path="$(readlink_f "${2}")"; diff --git a/libexec/tfenv---version b/libexec/tfenv---version index 52198f8..57533ac 100755 --- a/libexec/tfenv---version +++ b/libexec/tfenv---version @@ -21,20 +21,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-exec b/libexec/tfenv-exec index 9d3be48..9319330 100755 --- a/libexec/tfenv-exec +++ b/libexec/tfenv-exec @@ -25,20 +25,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-install b/libexec/tfenv-install index d13b384..99c916d 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -11,20 +11,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-list b/libexec/tfenv-list index 3e50c51..8685cab 100755 --- a/libexec/tfenv-list +++ b/libexec/tfenv-list @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-list-remote b/libexec/tfenv-list-remote index 0e8a6fc..b847ad9 100755 --- a/libexec/tfenv-list-remote +++ b/libexec/tfenv-list-remote @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-min-required b/libexec/tfenv-min-required index 80d91b1..bccaf77 100755 --- a/libexec/tfenv-min-required +++ b/libexec/tfenv-min-required @@ -14,20 +14,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-pin b/libexec/tfenv-pin index 1c7077c..8f548bc 100755 --- a/libexec/tfenv-pin +++ b/libexec/tfenv-pin @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-resolve-version b/libexec/tfenv-resolve-version index d0b66bc..b9b45ce 100755 --- a/libexec/tfenv-resolve-version +++ b/libexec/tfenv-resolve-version @@ -14,20 +14,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-uninstall b/libexec/tfenv-uninstall index 83289c8..86acc59 100755 --- a/libexec/tfenv-uninstall +++ b/libexec/tfenv-uninstall @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-use b/libexec/tfenv-use index 0c411cc..5094de2 100755 --- a/libexec/tfenv-use +++ b/libexec/tfenv-use @@ -11,20 +11,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-version-file b/libexec/tfenv-version-file index 153a29e..c320431 100755 --- a/libexec/tfenv-version-file +++ b/libexec/tfenv-version-file @@ -14,20 +14,6 @@ function early_death() { }; 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 diff --git a/libexec/tfenv-version-name b/libexec/tfenv-version-name index db4ee4c..35ec313 100755 --- a/libexec/tfenv-version-name +++ b/libexec/tfenv-version-name @@ -13,20 +13,6 @@ function early_death() { }; 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 diff --git a/test/run.sh b/test/run.sh index 0e1d362..010b437 100755 --- a/test/run.sh +++ b/test/run.sh @@ -11,20 +11,6 @@ function early_death() { }; 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 diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index fd2b6f0..bdc9ac8 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -11,20 +11,6 @@ function early_death() { }; 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 diff --git a/test/test_list.sh b/test/test_list.sh index bcb8d57..93879ea 100755 --- a/test/test_list.sh +++ b/test/test_list.sh @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/test/test_symlink.sh b/test/test_symlink.sh index 3c99bfd..21b674e 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index 70fe803..98a9603 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/test/test_use_latestallowed.sh b/test/test_use_latestallowed.sh index 10af0d5..1032bf5 100755 --- a/test/test_use_latestallowed.sh +++ b/test/test_use_latestallowed.sh @@ -12,20 +12,6 @@ function early_death() { }; 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 diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index c39ea99..5bad29c 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -12,20 +12,6 @@ function early_death() { }; 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 From 7df205d1b3b7c5f64bb65d3165324b1a71962c1f Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Sat, 5 Nov 2022 00:14:38 +0000 Subject: [PATCH 5/5] Fix readlink_f of relative dir Previously `readlink_f example` would result in `path/to/example/example`, duplicating the the relative directory. --- lib/helpers.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helpers.sh b/lib/helpers.sh index 695dd7b..dd5e21a 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -8,7 +8,7 @@ readlink_f() { local file_name; while [ "${target_file}" != "" ]; do - cd "${target_file%/*}" || early_death "Failed to 'cd \$(${target_file%/*})'"; + cd "$(dirname "${target_file}")" || early_death "Failed to 'cd \$(${target_file%/*})'"; file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'"; target_file="$(readlink "${file_name}")"; done;