Skip to content

Commit

Permalink
Merge pull request tfutils#303 from OJFord/fix-301
Browse files Browse the repository at this point in the history
Fix tfutils#301: find min-required version through -chdir
  • Loading branch information
Zordrak authored Jul 14, 2022
2 parents e289caf + 256bb81 commit 2d90024
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
17 changes: 16 additions & 1 deletion lib/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,20 @@ export -f curlw;

function check_active_version() {
local v="${1}";
[ -n "$(${TFENV_ROOT}/bin/terraform version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ];
local maybe_chdir=;
if [ -n "${2:-}" ]; then
maybe_chdir="-chdir=${2}";
fi;

local active_version="$(${TFENV_ROOT}/bin/terraform ${maybe_chdir} version | grep '^Terraform')";

if ! grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?\$" <(echo "${active_version}"); then
log 'debug' "Expected version ${v} but found ${active_version}";
return 1;
fi;

log 'debug' "Active version ${v} as expected";
return 0;
};
export -f check_active_version;

Expand Down Expand Up @@ -113,6 +126,8 @@ function cleanup() {
rm -rf ./.terraform-version;
log 'debug' "Deleting ${pwd}/min_required.tf";
rm -rf ./min_required.tf;
log 'debug' "Deleting ${pwd}/chdir-dir";
rm -rf ./chdir-dir;
};
export -f cleanup;

Expand Down
27 changes: 19 additions & 8 deletions lib/tfenv-exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
set -uo pipefail;

function tfenv-exec() {
for _arg in ${@:1}; do
if [[ "${_arg}" == -chdir=* ]]; then
log 'debug' "Found -chdir arg: ${_arg#-chdir=}";
export TFENV_DIR="${PWD}/${_arg#-chdir=}";
break;
else
export TFENV_DIR="${PWD}";
fi;
log 'info' "Set TFENV_DIR to ${TFENV_DIR}";
done;

log 'debug' 'Getting version from tfenv-version-name';
TFENV_VERSION="$(tfenv-version-name)" \
&& log 'debug' "TFENV_VERSION is ${TFENV_VERSION}" \
|| {
# Errors will be logged from tfenv-version name,
# we don't need to trouble STDERR with repeat information here
log 'debug' 'Failed to get version from tfenv-version-name';
return 1;
};
&& log 'debug' "TFENV_VERSION is ${TFENV_VERSION}" \
|| {
# Errors will be logged from tfenv-version name,
# we don't need to trouble STDERR with repeat information here
log 'debug' 'Failed to get version from tfenv-version-name';
return 1;
};
export TFENV_VERSION;

if [ ! -d "${TFENV_CONFIG_DIR}/versions/${TFENV_VERSION}" ]; then
Expand All @@ -20,7 +31,7 @@ function tfenv-exec() {
TFENV_VERSION_SOURCE="$(tfenv-version-file)";
else
TFENV_VERSION_SOURCE='TFENV_TERRAFORM_VERSION';
fi
fi;
log 'info' "version '${TFENV_VERSION}' is not installed (set by ${TFENV_VERSION_SOURCE}). Installing now as TFENV_AUTO_INSTALL==true";
tfenv-install;
else
Expand Down
2 changes: 1 addition & 1 deletion lib/tfenv-min-required.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -uo pipefail;

function tfenv-min-required() {
local path="${1:-.}";
local path="${1:-${TFENV_DIR:-.}}";

local versions="$( echo $(cat ${path}/{*.tf,*.tf.json} 2>/dev/null | grep -Eh '^\s*[^#]*\s*required_version') | grep -o '[~=!<>]\{0,2\}\s*\([0-9]\+\.\?\)\{2,3\}\(-[a-z]\+[0-9]\+\)\?')";

Expand Down
18 changes: 18 additions & 0 deletions test/test_use_minrequired.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ echo 'min-required' > .terraform-version;

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


log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir';

minv='1.1.0';

mkdir -p chdir-dir
echo "terraform {
required_version = \">=${minv}\"
}" >> chdir-dir/min_required.tf;
echo 'min-required' > chdir-dir/.terraform-version

(
TFENV_AUTO_INSTALL=true terraform -chdir=chdir-dir version;
check_active_version "${minv}" chdir-dir;
) || error_and_proceed 'Min required version from -chdir does not match';

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

if [ "${#errors[@]}" -gt 0 ]; then
log 'warn' '===== The following use_minrequired tests failed =====';
for error in "${errors[@]}"; do
Expand Down

0 comments on commit 2d90024

Please sign in to comment.