From a7729d80e7dbab788857981f0cebd01df7a22419 Mon Sep 17 00:00:00 2001 From: Samuel Phan Date: Thu, 15 Jun 2023 19:20:03 +0200 Subject: [PATCH 1/4] fix: filter out Artifactory version & sort versions fix #400 #401 --- README.md | 50 ++++++++++++++++++++++++++++++++++++++- libexec/tfenv-list-remote | 18 +++++++++++--- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9155dbe..c0a0436 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ $ brew install tfenv ``` Install via Arch User Repository (AUR) - + ```console $ yay --sync tfenv ``` @@ -220,6 +220,54 @@ functionality will be restored. $ TFENV_REVERSE_REMOTE=1 tfenv list-remote ``` +##### `TFENV_SORT_VERSIONS_REMOTE` + +Integer (Default: 0) + +When using a custom remote, such as Artifactory, instead of the Hashicorp servers, +the list of terraform versions returned by the curl of the remote directory may be inverted +and sorted alphabetically, and not in a version-aware sort. + +For example, you will have something like this: + +``` +1.0.0 +1.0.10 +1.0.11 +1.0.1 +1.0.2 +1.0.3 +1.0.4 +1.0.5 +1.0.6 +1.0.7 +1.0.8 +1.0.9 +``` + +instead of: + +``` +1.0.0 +1.0.1 +1.0.2 +1.0.3 +1.0.4 +1.0.5 +1.0.6 +1.0.7 +1.0.8 +1.0.9 +1.0.10 +1.0.11 +``` + +To use `sort --version-sort` over all the list of versions, set this environment variable: + +```console +$ TFENV_SORT_VERSIONS_REMOTE=1 tfenv list-remote +``` + ##### `TFENV_CONFIG_DIR` Path (Default: `$TFENV_ROOT`) diff --git a/libexec/tfenv-list-remote b/libexec/tfenv-list-remote index 0e8a6fc..42b05fa 100755 --- a/libexec/tfenv-list-remote +++ b/libexec/tfenv-list-remote @@ -73,8 +73,20 @@ remote_versions="$(curlw -sSf "${TFENV_REMOTE}/terraform/")" \ #log 'debug' "Remote versions available: ${remote_versions}"; # Even in debug mode this is too verbose +# Remove Artifactory version +remote_versions="$(grep -v -E "Artifactory/[0-9]+\.[0-9]+\.[0-9]+" <<< "${remote_versions}")" + +# Grep the versions +remote_versions="$(grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta|alpha|oci)-?[0-9]*)?" <<< "${remote_versions}" | uniq)" + +# Sort versions +if [[ "${TFENV_SORT_VERSIONS_REMOTE:-0}" -eq 1 ]]; then + remote_versions="$(sort --version-sort <<< "${remote_versions}")" +fi + +# Reverse versions if [[ "${TFENV_REVERSE_REMOTE:-0}" -eq 1 ]]; then - grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta|alpha|oci)-?[0-9]*)?" <<<"${remote_versions}" | uniq | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }'; -else - grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta|alpha|oci)-?[0-9]*)?" <<<"${remote_versions}" | uniq; + remote_versions="$(awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' <<< "${remote_versions}")" fi; + +cat <<< "${remote_versions}" \ No newline at end of file From 5db1cd29978f3593e1cf0d886cff5af43062ad67 Mon Sep 17 00:00:00 2001 From: Samuel Phan Date: Sun, 1 Oct 2023 23:58:38 +0200 Subject: [PATCH 2/4] chore: add empty line at the end of libexec/tfenv-list-remote --- libexec/tfenv-list-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/tfenv-list-remote b/libexec/tfenv-list-remote index 42b05fa..c53dcd9 100755 --- a/libexec/tfenv-list-remote +++ b/libexec/tfenv-list-remote @@ -89,4 +89,4 @@ if [[ "${TFENV_REVERSE_REMOTE:-0}" -eq 1 ]]; then remote_versions="$(awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' <<< "${remote_versions}")" fi; -cat <<< "${remote_versions}" \ No newline at end of file +cat <<< "${remote_versions}" From 6f60a1a30ec7632277b9b1952e71b5056a430fed Mon Sep 17 00:00:00 2001 From: dvaumoron Date: Wed, 4 Oct 2023 18:13:45 +0200 Subject: [PATCH 3/4] test fix : remove terraform versions prior to 0.11.9 --- test/test_install_and_use.sh | 4 ++-- test/test_list.sh | 10 ++++------ test/test_symlink.sh | 12 ++++++------ test/test_uninstall.sh | 8 ++++---- test/test_use_minrequired.sh | 2 +- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index fd2b6f0..fcca894 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -105,8 +105,8 @@ tests__kv=( "$(tfenv list-remote | grep 'rc' | head -n 1),latest:rc" "$(tfenv list-remote | grep '^0\.11\.' | head -n 1),latest:^0.11." '0.11.15-oci,0.11.15-oci' - '0.8.8,latest:^0.8' - '0.7.13,0.7.13' + '0.11.14,latest:^0.11' + '0.11.9,0.11.9' '0.14.6,v0.14.6' ); diff --git a/test/test_list.sh b/test/test_list.sh index bcb8d57..980d487 100755 --- a/test/test_list.sh +++ b/test/test_list.sh @@ -53,7 +53,7 @@ declare -a errors=(); log 'info' '### List local versions'; cleanup || log 'error' "Cleanup failed?!"; -for v in 0.7.2 0.7.13 0.9.1 0.9.2 v0.9.11 0.14.6; do +for v in 0.11.9 0.11.11 v0.11.13 0.14.6; do log 'info' "## Installing version ${v} to construct list"; tfenv install "${v}" \ && log 'debug' "Install of version ${v} succeeded" \ @@ -71,11 +71,9 @@ log 'info' '## Comparing "tfenv list" with default set'; result="$(tfenv list)"; 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.11.13 + 0.11.11 + 0.11.9 EOS )"; diff --git a/test/test_symlink.sh b/test/test_symlink.sh index 3c99bfd..43cf91d 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -60,14 +60,14 @@ ln -s "${PWD}"/bin/* "${TFENV_BIN_DIR}"; cleanup || log 'error' 'Cleanup failed?!'; -log 'info' '## Installing 0.8.2'; -${TFENV_BIN_DIR}/tfenv install 0.8.2 || error_and_proceed 'Install failed'; +log 'info' '## Installing 0.11.14'; +${TFENV_BIN_DIR}/tfenv install 0.11.14 || error_and_proceed 'Install failed'; -log 'info' '## Using 0.8.2'; -${TFENV_BIN_DIR}/tfenv use 0.8.2 || error_and_proceed 'Use failed'; +log 'info' '## Using 0.11.14'; +${TFENV_BIN_DIR}/tfenv use 0.11.14 || error_and_proceed 'Use failed'; -log 'info' '## Check-Version for 0.8.2'; -check_active_version 0.8.2 || error_and_proceed 'Version check failed'; +log 'info' '## Check-Version for 0.11.14'; +check_active_version 0.11.14 || error_and_proceed 'Version check failed'; if [ "${#errors[@]}" -gt 0 ]; then log 'warn' '===== The following symlink tests failed ====='; diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index 70fe803..7ef2fbc 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -63,18 +63,18 @@ log 'info' '### Test Suite: Uninstall Local Versions'; cleanup || log 'error' 'Cleanup failed?!'; tests__keywords=( - '0.9.1' + '0.11.10' '0.11.15-oci' 'latest' - 'latest:^0.8' + 'latest:^0.11' 'v0.14.6' ); tests__versions=( - '0.9.1' + '0.11.10' '0.11.15-oci' "$(tfenv list-remote | head -n1)" - "$(tfenv list-remote | grep -e "^0.8" | head -n1)" + "$(tfenv list-remote | grep -e "^0.11" | head -n1)" '0.14.6' ); diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index b53b2fa..0bad275 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -55,7 +55,7 @@ cleanup || log 'error' 'Cleanup failed?!'; log 'info' '### Install min-required normal version (#.#.#)'; -minv='0.8.0'; +minv='0.11.9'; echo "terraform { required_version = \">=${minv}\" From 97bac44cb7beeb36032633281fa4b6b9cdc4feb5 Mon Sep 17 00:00:00 2001 From: dvaumoron Date: Thu, 5 Oct 2023 08:55:25 +0200 Subject: [PATCH 4/4] test fix : latest:^0.11 is 0.11.15 --- test/test_install_and_use.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index fcca894..36b155c 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -105,7 +105,7 @@ tests__kv=( "$(tfenv list-remote | grep 'rc' | head -n 1),latest:rc" "$(tfenv list-remote | grep '^0\.11\.' | head -n 1),latest:^0.11." '0.11.15-oci,0.11.15-oci' - '0.11.14,latest:^0.11' + '0.11.15,latest:^0.11' '0.11.9,0.11.9' '0.14.6,v0.14.6' );