Skip to content

Commit

Permalink
fix: filter out Artifactory version & sort versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Phan committed Jun 15, 2023
1 parent 1ccfddb commit a7729d8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $ brew install tfenv
```

Install via Arch User Repository (AUR)

```console
$ yay --sync tfenv
```
Expand Down Expand Up @@ -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`)
Expand Down
18 changes: 15 additions & 3 deletions libexec/tfenv-list-remote
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

0 comments on commit a7729d8

Please sign in to comment.