From a7729d80e7dbab788857981f0cebd01df7a22419 Mon Sep 17 00:00:00 2001 From: Samuel Phan Date: Thu, 15 Jun 2023 19:20:03 +0200 Subject: [PATCH] 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