Skip to content

Commit

Permalink
Merge branch 'master' into download-latest-version-if-regex-is-used
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordrak authored Aug 14, 2021
2 parents ee39309 + 7508248 commit 3ecdd41
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 18 deletions.
1 change: 1 addition & 0 deletions libexec/tfenv-help
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Commands:
list-remote List all installable versions
version-name Print current version
init Update environment to use 'tfenv' correctly.
pin Write the current active version to ./.terraform-version
';

exit 0;
77 changes: 77 additions & 0 deletions libexec/tfenv-pin
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env bash

set -uo pipefail;

####################################
# 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
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 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
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 pin"

[ -d "${TFENV_ROOT}/versions/" ] \
|| log 'error' 'No versions available. Please install one with: tfenv install'

[[ -x "${TFENV_ROOT}/versions" && -r "${TFENV_ROOT}/versions" ]] \
|| log 'error' "tfenv versions directory is inaccessible: ${TFENV_ROOT}/versions";

version_name="$(tfenv-version-name 2>/dev/null || true)" \
&& log 'debug' "tfenv-version-name reported: ${version_name}";

echo "${version_name}" > .terraform-version;
log 'info' "Pinned version by writing \"${version_name}\" to $(pwd)/.terraform-version";

exit 0;
6 changes: 6 additions & 0 deletions libexec/tfenv-resolve-version
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ fi;

log 'debug' "Version Requested: ${version_requested}";

# Accept a v-prefixed version, but strip the v.
if [[ "${version_requested}" =~ ^v.*$ ]]; then
log 'debug' "Version Requested is prefixed with a v. Stripping the v."
version_requested="${version_requested#v*}";
fi;

if [[ "${version_requested}" =~ ^min-required$ ]]; then
log 'info' 'Detecting minimum required version...';
min_required="$(tfenv-min-required)" \
Expand Down
1 change: 1 addition & 0 deletions libexec/tfenv-use
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fi

log debug "Resolving version with: tfenv-resolve-version ${requested_arg}";
declare resolved="$(tfenv-resolve-version ${requested_arg})";
log debug "Resolved to: ${resolved}";

declare version="${resolved%%\:*}";
declare regex="${resolved##*\:}";
Expand Down
6 changes: 6 additions & 0 deletions libexec/tfenv-version-name
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ if [[ "${TFENV_VERSION}" =~ ^latest.*$ ]]; then
fi;
else
log 'debug' 'TFENV_VERSION does not use "latest" keyword';

# Accept a v-prefixed version, but strip the v.
if [[ "${TFENV_VERSION}" =~ ^v.*$ ]]; then
log 'debug' "Version Requested is prefixed with a v. Stripping the v."
TFENV_VERSION="${TFENV_VERSION#v*}";
fi;
fi;

if [[ -z "${TFENV_VERSION}" ]]; then
Expand Down
36 changes: 21 additions & 15 deletions test/test_install_and_use.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ tests__desc=(
'0.11.15-oci'
'latest version matching regex'
'specific version'
'specific version with v prefix'
);

tests__kv=(
Expand All @@ -105,17 +106,19 @@ tests__kv=(
"$(tfenv list-remote | grep '^0\.11\.' | head -n 1),latest:^0.11."
'0.11.15-oci,0.11.15-oci'
'0.8.8,latest:^0.8'
"0.7.13,0.7.13"
'0.7.13,0.7.13'
'0.14.6,v0.14.6'
);

tests_count=${#tests__desc[@]};

declare desc kv k v;
declare desc kv k v test_num;

for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do
for ((test_iter=0; test_iter<${tests_count}; ++test_iter )) ; do
cleanup || log 'error' 'Cleanup failed?!';
desc=${tests__desc[${test_num}]};
kv="${tests__kv[${test_num}]}";
test_num=$((test_iter + 1));
desc=${tests__desc[${test_iter}]};
kv="${tests__kv[${test_iter}]}";
v="${kv%,*}";
k="${kv##*,}";
log 'info' "## Param Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} )";
Expand All @@ -124,10 +127,11 @@ for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do
|| error_and_proceed "## Param Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) failed";
done;

for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do
for ((test_iter=0; test_iter<${tests_count}; ++test_iter )) ; do
cleanup || log 'error' 'Cleanup failed?!';
desc=${tests__desc[${test_num}]};
kv="${tests__kv[${test_num}]}";
test_num=$((test_iter + 1));
desc=${tests__desc[${test_iter}]};
kv="${tests__kv[${test_iter}]}";
v="${kv%,*}";
k="${kv##*,}";
log 'info' "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} )";
Expand All @@ -138,10 +142,11 @@ for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do
|| error_and_proceed "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) failed";
done;

for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do
for ((test_iter=0; test_iter<${tests_count}; ++test_iter )) ; do
cleanup || log 'error' 'Cleanup failed?!';
desc=${tests__desc[${test_num}]};
kv="${tests__kv[${test_num}]}";
test_num=$((test_iter + 1));
desc=${tests__desc[${test_iter}]};
kv="${tests__kv[${test_iter}]}";
v="${kv%,*}";
k="${kv##*,}";
log 'info' "## TFENV_TERRAFORM_VERSION Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} )";
Expand Down Expand Up @@ -201,15 +206,16 @@ neg_tests__desc=(

neg_tests__kv=(
'9.9.9'
"latest:word"
'latest:word'
);

neg_tests_count=${#neg_tests__desc[@]};

for ((test_num=0; test_num<${neg_tests_count}; ++test_num )) ; do
for ((test_iter=0; test_iter<${neg_tests_count}; ++test_iter )) ; do
cleanup || log 'error' 'Cleanup failed?!';
desc=${neg_tests__desc[${test_num}]}
k="${neg_tests__kv[${test_num}]}";
test_num=$((test_iter + 1));
desc=${neg_tests__desc[${test_iter}]}
k="${neg_tests__kv[${test_iter}]}";
expected_error_message="No versions matching '${k}' found in remote";
log 'info' "## Invalid Version Test ${test_num}/${neg_tests_count}: ${desc} ( ${k} )";
[ -z "$(tfenv install "${k}" 2>&1 | grep "${expected_error_message}")" ] \
Expand Down
7 changes: 4 additions & 3 deletions test/test_list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ declare -a errors=();
log 'info' '### List local versions';
cleanup || log 'error' "Cleanup failed?!";

for v in 0.7.2 0.7.13 0.9.1 0.9.2 0.9.11; do
for v in 0.7.2 0.7.13 0.9.1 0.9.2 v0.9.11 0.14.6; do
log 'info' "## Installing version ${v} to construct list";
tfenv install "${v}" \
&& log 'debug' "Install of version ${v} succeeded" \
Expand All @@ -65,12 +65,13 @@ tfenv list \
&& log 'debug' "List succeeded with no default set" \
|| error_and_proceed "List failed with no default set";

tfenv use 0.9.11;
tfenv use 0.14.6;

log 'info' '## Comparing "tfenv list" with default set';
result="$(tfenv list)";
expected="$(cat << EOS
* 0.9.11 (set by $(tfenv version-file))
* 0.14.6 (set by $(tfenv version-file))
0.9.11
0.9.2
0.9.1
0.7.13
Expand Down
2 changes: 2 additions & 0 deletions test/test_uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ tests__keywords=(
'0.11.15-oci'
'latest'
'latest:^0.8'
'v0.14.6'
);

tests__versions=(
'0.9.1'
'0.11.15-oci'
"$(tfenv list-remote | head -n1)"
"$(tfenv list-remote | grep -e "^0.8" | head -n1)"
'0.14.6'
);

tests_count=${#tests__keywords[@]};
Expand Down

0 comments on commit 3ecdd41

Please sign in to comment.