Skip to content

Commit

Permalink
Merge branch 'master' into support-v-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordrak authored Aug 14, 2021
2 parents ed15385 + 3769e99 commit edcdc05
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bin/terraform
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ 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";
[ "${TFENV_DEBUG:-0}" -gt 0 ] && >&2 echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
if source "${TFENV_ROOT}/lib/helpers.sh"; then
log 'debug' 'Helpers sourced successfully';
else
Expand Down
2 changes: 1 addition & 1 deletion bin/tfenv
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ 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";
[ "${TFENV_DEBUG:-0}" -gt 0 ] && >&2 echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
if source "${TFENV_ROOT}/lib/helpers.sh"; then
log 'debug' 'Helpers sourced successfully';
else
Expand Down
2 changes: 1 addition & 1 deletion lib/bashlog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function log() {

local level="${1}";
local upper="$(echo "${level}" | awk '{print toupper($0)}')";
local debug_level="${DEBUG:-0}";
local debug_level="${TFENV_DEBUG:-0}";
local stdout_colours="${BASHLOG_COLOURS:-1}";
local stdout_extra="${BASHLOG_EXTRA:-0}";

Expand Down
5 changes: 4 additions & 1 deletion lib/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ fi
export TFENV_CONFIG_DIR;

if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then
[ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ] || export DEBUG="${TFENV_DEBUG:-0}";
# Only reset DEBUG if TFENV_DEBUG is set, and DEBUG is unset or already a number
if [[ "${DEBUG:-0}" =~ ^[0-9]+$ ]] && [ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ]; then
export DEBUG="${TFENV_DEBUG:-0}";
fi;
if [[ "${TFENV_DEBUG}" -gt 2 ]]; then
export PS4='+ [${BASH_SOURCE##*/}:${LINENO}] ';
set -x;
Expand Down
12 changes: 9 additions & 3 deletions libexec/tfenv-install
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fi;
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}";
;;
Expand All @@ -63,7 +63,8 @@ done;
declare requested="${1:-""}";

log debug "Resolving version with: tfenv-resolve-version ${requested}";
declare resolved="$(tfenv-resolve-version ${requested})";
declare resolved;
resolved="$(tfenv-resolve-version ${requested})" || log 'error' "Failed to resolve ${requested} version";

declare version="${resolved%%\:*}";
declare regex="${resolved##*\:}";
Expand Down Expand Up @@ -133,7 +134,12 @@ shasums_sig="${shasums_name}${shasums_signing_key_postfix}.sig";
log 'info' "Installing Terraform v${version}";

# Create a local temporary directory for downloads
download_tmp="$(mktemp -d tfenv_download.XXXXXX)" || log 'error' "Unable to create temporary download directory in $(pwd)";
tmpdir_arg="--tmpdir"
if [[ $(uname) == 'Darwin' ]]; then
# MacOS uses an old version of `mktemp` which only supports the deprecated `-t` option
tmpdir_arg="-t"
fi
download_tmp="$(mktemp -d ${tmpdir_arg} tfenv_download.XXXXXX)" || log 'error' "Unable to create temporary download directory in $(pwd)";
# Clean it up in case of error
trap "rm -rf ${download_tmp}" EXIT;

Expand Down
8 changes: 8 additions & 0 deletions libexec/tfenv-min-required
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ find_min_required() {
else
found_min_required="$(echo "$found_min_required")";
#echo "Min required version is detected as ${found_min_required}";

# Probably not an advisable way to choose a terraform version,
# but this is the way this functionality works in terraform:
# add .0 to versions without a minor and/or patch version (e.g. 12.0)
while ! [[ "${found_min_required}" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; do
found_min_required="${found_min_required}.0";
done;

echo "${found_min_required}";
exit 0;
fi;
Expand Down
54 changes: 33 additions & 21 deletions test/test_use_minrequired.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,56 @@ fi;

declare -a errors=();

log 'info' '### Install not min-required version';
cleanup || log 'error' 'Cleanup failed?!';

v='0.8.8';
minv='0.8.0';
minv_tag='0.13.0-rc1'
(
tfenv install "${v}" || true;
tfenv use "${v}" || exit 1;
check_active_version "${v}" || exit 1;
) || error_and_proceed "Installing specific version ${v}";


log 'info' '### Install min-required normal version (#.#.#)';

echo "terraform {
minv='0.8.0';

echo "terraform {
required_version = \">=${minv}\"
}" >> min_required.tf;

tfenv install min-required;
tfenv use min-required;
}" > min_required.tf;

check_active_version "${minv}" || error_and_proceed 'Min required version does not match';
(
tfenv install min-required;
tfenv use min-required;
check_active_version "${minv}";
) || error_and_proceed 'Min required version does not match';

cleanup || log 'error' 'Cleanup failed?!';


log 'info' '### Install min-required tagged version (#.#.#-tag#)'

minv='0.13.0-rc1'

echo "terraform {
required_version = \">=${minv}\"
}" > min_required.tf;

required_version = \">=${minv_tag}\"
}" >> min_required.tf;
(
tfenv install min-required;
tfenv use min-required;
check_active_version "${minv}";
) || error_and_proceed 'Min required tagged-version does not match';

cleanup || log 'error' 'Cleanup failed?!';


log 'info' '### Install min-required incomplete version (#.#.<missing>)'

tfenv install min-required
tfenv use min-required
minv='0.12';

check_active_version "${minv_tag}" || error_and_proceed 'Min required version does not match';
echo "terraform {
required_version = \">=${minv}\"
}" >> min_required.tf;

(
tfenv install min-required;
tfenv use min-required;
check_active_version "${minv}.0";
) || error_and_proceed 'Min required incomplete-version does not match';

cleanup || log 'error' 'Cleanup failed?!';

Expand Down

0 comments on commit edcdc05

Please sign in to comment.