Skip to content

Commit edcdc05

Browse files
authored
Merge branch 'master' into support-v-prefix
2 parents ed15385 + 3769e99 commit edcdc05

7 files changed

Lines changed: 57 additions & 28 deletions

File tree

bin/terraform

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export TFENV_ROOT;
3535
if [ -n "${TFENV_HELPERS:-""}" ]; then
3636
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
3737
else
38-
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
38+
[ "${TFENV_DEBUG:-0}" -gt 0 ] && >&2 echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
3939
if source "${TFENV_ROOT}/lib/helpers.sh"; then
4040
log 'debug' 'Helpers sourced successfully';
4141
else

bin/tfenv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export TFENV_ROOT;
3535
if [ -n "${TFENV_HELPERS:-""}" ]; then
3636
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
3737
else
38-
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
38+
[ "${TFENV_DEBUG:-0}" -gt 0 ] && >&2 echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
3939
if source "${TFENV_ROOT}/lib/helpers.sh"; then
4040
log 'debug' 'Helpers sourced successfully';
4141
else

lib/bashlog.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function log() {
3131

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

lib/helpers.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ fi
3232
export TFENV_CONFIG_DIR;
3333

3434
if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then
35-
[ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ] || export DEBUG="${TFENV_DEBUG:-0}";
35+
# Only reset DEBUG if TFENV_DEBUG is set, and DEBUG is unset or already a number
36+
if [[ "${DEBUG:-0}" =~ ^[0-9]+$ ]] && [ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ]; then
37+
export DEBUG="${TFENV_DEBUG:-0}";
38+
fi;
3639
if [[ "${TFENV_DEBUG}" -gt 2 ]]; then
3740
export PS4='+ [${BASH_SOURCE##*/}:${LINENO}] ';
3841
set -x;

libexec/tfenv-install

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fi;
4747
for dir in libexec bin; do
4848
case ":${PATH}:" in
4949
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
50-
*)
50+
*)
5151
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
5252
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
5353
;;
@@ -63,7 +63,8 @@ done;
6363
declare requested="${1:-""}";
6464

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

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

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

libexec/tfenv-min-required

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ find_min_required() {
8585
else
8686
found_min_required="$(echo "$found_min_required")";
8787
#echo "Min required version is detected as ${found_min_required}";
88+
89+
# Probably not an advisable way to choose a terraform version,
90+
# but this is the way this functionality works in terraform:
91+
# add .0 to versions without a minor and/or patch version (e.g. 12.0)
92+
while ! [[ "${found_min_required}" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; do
93+
found_min_required="${found_min_required}.0";
94+
done;
95+
8896
echo "${found_min_required}";
8997
exit 0;
9098
fi;

test/test_use_minrequired.sh

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,44 +50,56 @@ fi;
5050

5151
declare -a errors=();
5252

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

56-
v='0.8.8';
57-
minv='0.8.0';
58-
minv_tag='0.13.0-rc1'
59-
(
60-
tfenv install "${v}" || true;
61-
tfenv use "${v}" || exit 1;
62-
check_active_version "${v}" || exit 1;
63-
) || error_and_proceed "Installing specific version ${v}";
64-
6555

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

68-
echo "terraform {
58+
minv='0.8.0';
6959

60+
echo "terraform {
7061
required_version = \">=${minv}\"
71-
}" >> min_required.tf;
72-
73-
tfenv install min-required;
74-
tfenv use min-required;
62+
}" > min_required.tf;
7563

76-
check_active_version "${minv}" || error_and_proceed 'Min required version does not match';
64+
(
65+
tfenv install min-required;
66+
tfenv use min-required;
67+
check_active_version "${minv}";
68+
) || error_and_proceed 'Min required version does not match';
7769

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

72+
8073
log 'info' '### Install min-required tagged version (#.#.#-tag#)'
8174

75+
minv='0.13.0-rc1'
76+
8277
echo "terraform {
78+
required_version = \">=${minv}\"
79+
}" > min_required.tf;
8380

84-
required_version = \">=${minv_tag}\"
85-
}" >> min_required.tf;
81+
(
82+
tfenv install min-required;
83+
tfenv use min-required;
84+
check_active_version "${minv}";
85+
) || error_and_proceed 'Min required tagged-version does not match';
86+
87+
cleanup || log 'error' 'Cleanup failed?!';
88+
89+
90+
log 'info' '### Install min-required incomplete version (#.#.<missing>)'
8691

87-
tfenv install min-required
88-
tfenv use min-required
92+
minv='0.12';
8993

90-
check_active_version "${minv_tag}" || error_and_proceed 'Min required version does not match';
94+
echo "terraform {
95+
required_version = \">=${minv}\"
96+
}" >> min_required.tf;
97+
98+
(
99+
tfenv install min-required;
100+
tfenv use min-required;
101+
check_active_version "${minv}.0";
102+
) || error_and_proceed 'Min required incomplete-version does not match';
91103

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

0 commit comments

Comments
 (0)