Skip to content

Commit 7893f15

Browse files
committed
Fix #301: find min-required version through -chdir
This commit makes it possible to use `min-required` with the terraform configuration (including the `required_version` spec) in a subdirectory as `terraform -chdir=subdir`. Prior to this commit, this behaviour does not work (`-chdir` cannot be used with `tfenv`'s `terraform` wrapper & `min-required` version) because `tfenv` expects to find the configuration in `$PWD`.
1 parent 18ef0fa commit 7893f15

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/tfenv-exec.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
set -uo pipefail;
44

55
function tfenv-exec() {
6+
for _arg in ${@:1}; do
7+
if [[ "$_arg" == -chdir=* ]]; then
8+
log 'debug' "Found -chdir arg: ${_arg#-chdir=}"
9+
export TFENV_DIR="${PWD}/${_arg#-chdir=}";
10+
break;
11+
else
12+
export TFENV_DIR="${PWD}";
13+
fi;
14+
log 'info' "Set TFENV_DIR to $TFENV_DIR"
15+
done;
16+
617
log 'debug' 'Getting version from tfenv-version-name';
718
TFENV_VERSION="$(tfenv-version-name)" \
819
&& log 'debug' "TFENV_VERSION is ${TFENV_VERSION}" \

lib/tfenv-min-required.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -uo pipefail;
44

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

88
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]\+\)\?')";
99

0 commit comments

Comments
 (0)