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
23 changes: 21 additions & 2 deletions libexec/tfenv-list
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,31 @@ export version_source;
# Register for whether a default terraform version has yet been set
declare -i default_set=0;

detect_arch() {
local binary="${1}";
if [ ! -f "${binary}" ]; then
echo 'unknown';
return;
fi;
local file_output;
file_output="$(file -b "${binary}" 2>/dev/null)" || { echo 'unknown'; return; };
case "${file_output}" in
*x86-64*|*x86_64*) echo 'amd64';;
*aarch64*|*arm64*) echo 'arm64';;
*386*|*i386*|*80386*) echo '386';;
*ARM,*) echo 'arm';;
*) echo 'unknown';;
esac;
};

print_version () {
local arch;
arch="$(detect_arch "${TFENV_CONFIG_DIR}/versions/${1}/terraform")";
if [ "${1}" == "${version_name}" ]; then
echo "* ${1} (set by ${version_source})";
echo "* ${1} (${arch}) (set by ${version_source})";
default_set=1;
else
echo " ${1}";
echo " ${1} (${arch})";
fi;
};

Expand Down
2 changes: 1 addition & 1 deletion libexec/tfenv-uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fi;

log 'debug' "Processing uninstall for version ${version}, using regex ${regex}";

version="$(tfenv-list | sed -E 's/^(\*| )? //g; s/ \(set by .+\)$//' | grep -e "${regex}" | head -n 1)";
version="$(tfenv-list | sed -E 's/^(\*| )? //g; s/ \([^)]+\)//g' | grep -e "${regex}" | head -n 1)";
[ -n "${version}" ] || log 'error' "No versions matching '${regex}' found in local";

dst_path="${TFENV_CONFIG_DIR}/versions/${version}";
Expand Down
28 changes: 22 additions & 6 deletions test/test_list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,29 @@ tfenv use 0.14.6;

log 'info' '## Comparing "tfenv list" with default set';
result="$(tfenv list)";

# Detect arch from each installed binary — can't assume host arch
# because older versions may only have amd64 builds (e.g. macOS arm64 via Rosetta)
detect_arch() {
local binary="${1}";
local file_output;
file_output="$(file -b "${binary}" 2>/dev/null)" || { echo 'unknown'; return; };
case "${file_output}" in
*x86-64*|*x86_64*) echo 'amd64';;
*aarch64*|*arm64*) echo 'arm64';;
*386*|*i386*|*80386*) echo '386';;
*ARM,*) echo 'arm';;
*) echo 'unknown';;
esac;
};

expected="$(cat << EOS
* 0.14.6 (set by $(tfenv version-file))
0.9.11
0.9.2
0.9.1
0.7.13
0.7.2
* 0.14.6 ($(detect_arch "${TFENV_CONFIG_DIR}/versions/0.14.6/terraform")) (set by $(tfenv version-file))
0.9.11 ($(detect_arch "${TFENV_CONFIG_DIR}/versions/0.9.11/terraform"))
0.9.2 ($(detect_arch "${TFENV_CONFIG_DIR}/versions/0.9.2/terraform"))
0.9.1 ($(detect_arch "${TFENV_CONFIG_DIR}/versions/0.9.1/terraform"))
0.7.13 ($(detect_arch "${TFENV_CONFIG_DIR}/versions/0.7.13/terraform"))
0.7.2 ($(detect_arch "${TFENV_CONFIG_DIR}/versions/0.7.2/terraform"))
EOS
)";

Expand Down
Loading