Skip to content

Commit

Permalink
Fix realpath not available on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
OJFord committed Feb 10, 2024
1 parent b5782f8 commit 9ea34f6
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/tfenv-exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9ea34f6

Please sign in to comment.