Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
if: contains(matrix.os, 'ubuntu')
with:
install: true
- uses: 'docker/build-push-action@v5'
- uses: 'docker/build-push-action@v6'
if: contains(matrix.os, 'ubuntu')
with:
context: .
Expand All @@ -54,10 +54,13 @@ jobs:
strategy:
matrix:
os:
- 'macos-11'
- 'macos-10.15'
- 'ubuntu-22.04'
- 'ubuntu-20.04'
- 'ubuntu-18.04'
- 'macos-14'
- 'macos-13'
- 'macos-12'
- 'macos-11'
- 'windows-2022'
- 'windows-2019'
steps:
- uses: 'actions/checkout@v4'
Expand All @@ -73,7 +76,7 @@ jobs:
if: contains(matrix.os, 'ubuntu')
with:
install: true
- uses: 'docker/build-push-action@v5'
- uses: 'docker/build-push-action@v6'
if: contains(matrix.os, 'ubuntu')
with:
context: .
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ bash:
```console
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile
```

zsh:
```console
$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.zprofile
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.zprofile
```

fish:
```console
echo 'set -x PATH $HOME/.tfenv/bin $PATH' >> ~/.config/fish/config.fish
```

For WSL users:
Expand Down
15 changes: 11 additions & 4 deletions lib/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,17 @@ export -f check_default_version;
function cleanup() {
log 'info' 'Performing cleanup';
local pwd="$(pwd)";
log 'debug' "Deleting ${pwd}/version";
rm -rf ./version;
log 'debug' "Deleting ${pwd}/versions";
rm -rf ./versions;

# Safety check to ensure TFENV_CONFIG_DIR is set and not empty
if [ -z "${TFENV_CONFIG_DIR:-""}" ]; then
log 'error' 'TFENV_CONFIG_DIR is not set, cannot perform cleanup safely';
return 1;
fi;

log 'debug' "Deleting ${TFENV_CONFIG_DIR}/version";
rm -rf "${TFENV_CONFIG_DIR}/version";
log 'debug' "Deleting ${TFENV_CONFIG_DIR}/versions";
rm -rf "${TFENV_CONFIG_DIR}/versions";
log 'debug' "Deleting ${pwd}/.terraform-version";
rm -rf ./.terraform-version;
log 'debug' "Deleting ${pwd}/latest_allowed.tf";
Expand Down
32 changes: 30 additions & 2 deletions lib/tfenv-exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,39 @@

set -uo pipefail;

function realpath-relative-to() {
# A basic implementation of GNU `realpath --relative-to=$1 $2`
# that can also be used on macOS.

# 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 \$(${target_file%/*})'";
file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'";
target_file="$(readlink "${file_name}")";
done;

echo "$(pwd -P)/${file_name}";
};

local relative_to="$(readlink_f "${1}")";
local path="$(readlink_f "${2}")";

echo "${path#"${relative_to}/"}";
return 0;
}
export -f realpath-relative-to;

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

Expand Down
2 changes: 1 addition & 1 deletion lib/tfenv-version-name.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function tfenv-version-name() {
&& log 'debug' "TFENV_VERSION_FILE retrieved from tfenv-version-file: ${TFENV_VERSION_FILE}" \
|| log 'error' 'Failed to retrieve TFENV_VERSION_FILE from tfenv-version-file';

TFENV_VERSION="$(cat "${TFENV_VERSION_FILE}" || true)" \
TFENV_VERSION="$(cat "${TFENV_VERSION_FILE}" || true | tr -d '\r')" \
&& log 'debug' "TFENV_VERSION specified in TFENV_VERSION_FILE: ${TFENV_VERSION}";

TFENV_VERSION_SOURCE="${TFENV_VERSION_FILE}";
Expand Down
4 changes: 2 additions & 2 deletions libexec/tfenv-resolve-version
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ if [ -z "${arg}" -a -z "${TFENV_TERRAFORM_VERSION:-""}" ]; then

if [ "${version_file}" != "${TFENV_CONFIG_DIR}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
version_requested="$(cat "${version_file}" | tr -d '\r')" \
|| log 'error' "Failed to open ${version_file}";

elif [ -f "${version_file}" ]; then
log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
version_requested="$(cat "${version_file}" | tr -d '\r')" \
|| log 'error' "Failed to open ${version_file}";

# Absolute fallback
Expand Down
46 changes: 2 additions & 44 deletions test/run.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,12 @@
#!/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;
# Source common test setup
source "$(dirname "${0}")/test_common.sh";

#####################
# Begin Script Body #
#####################

export PATH="${TFENV_ROOT}/bin:${PATH}";

errors=();
if [ "${#}" -ne 0 ]; then
targets="$@";
Expand Down
51 changes: 51 additions & 0 deletions test/test_common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

# Common test setup header
# Source this file at the beginning of test scripts to set up the test environment

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 local tfenv binaries take precedence
export PATH="${TFENV_ROOT}/bin:${PATH}";
44 changes: 2 additions & 42 deletions test/test_install_and_use.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
#!/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;
# Source common test setup
source "$(dirname "${0}")/test_common.sh";

#####################
# Begin Script Body #
Expand Down
45 changes: 2 additions & 43 deletions test/test_list.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,7 @@
#!/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;
# Source common test setup
source "$(dirname "${0}")/test_common.sh";

#####################
# Begin Script Body #
Expand Down
Loading
Loading