Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor readlink_f to common helpers #366

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions bin/terraform
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
13 changes: 0 additions & 13 deletions bin/tfenv
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
28 changes: 15 additions & 13 deletions lib/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$(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}";
};
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";
Expand Down
17 changes: 15 additions & 2 deletions lib/tfenv-exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

set -uo pipefail;

function realpath-relative-to() {
# A basic implementation of GNU `realpath --relative-to=$1 $2`
# that can also be used on macOS.
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
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;

Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv---version
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-exec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-install
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-list
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-list-remote
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-min-required
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-pin
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-resolve-version
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-use
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-version-file
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions libexec/tfenv-version-name
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions test/test_install_and_use.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading