Skip to content

Commit

Permalink
Add version filtering support
Browse files Browse the repository at this point in the history
There are some plugins that return prerelease versions.
Some other plugins don't return a unique list.

This change allows for ensuring lists are unique before passing it to
fzf.
It also allows for passing in custom filter commands.
  • Loading branch information
reegnz committed Dec 14, 2021
1 parent c54bc35 commit e2f6a11
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 73 deletions.
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,49 @@

[asdf](https://github.com/asdf-vm/asdf) + [fzf](https://github.com/junegunn/fzf) = asdfzf!


## Installation

Put the script on your path, eg.:

```
```sh
mkdir -p ~/bin
cp asdfzf ~/bin
chmod +x ~/bin/asdfzf
cd ~/bin
wget https://raw.githubusercontent.com/reegnz/asdfzf/master/asdfzf
chmod +x asdfzf
```

## Usage

The asdfzf command hides asdf commands behind an fzf interactive selection interface.

There are no arguments. Just run `asdfzf` and that's it!

### Custom version filters per tool

You can influence versions that display during install.

Precedence of environment variables is:
* ASDFZF_VERSION_FILTER_COMMAND_pluginname
* ASDFZF_VERSION_KEEP_pluginname
* ASDFZF_VERSION_FILTER_COMMAND
* ASDFZF_VERSION_KEEP

#### ASDFZF_VERSION_KEEP

`ASDFZF_VERSION_KEEP=semver-no-prerelease` will keep versions that are
not semver prerelease versions.

`ASDFZF_VERSION_KEEP_kubectl=semver-only-prerelease` will only show semver
prerelease versions

#### ASDFZF_VERSION_KEEP_pluginname

Same as `ASDFZF_VERSION_KEEP` but limited to a given plugin only.

#### ASDFZF_VERSION_FILTER_COMMAND

Allows for providing a custom shell command to filter the version strings.

#### ASDFZF_VERSION_FILTER_COMMAND_pluginname

Same as `ASDFZF_VERSION_FILTER_COMMAND` but limited to a given plugin only.
197 changes: 128 additions & 69 deletions asdfzf
Original file line number Diff line number Diff line change
@@ -1,140 +1,192 @@
#!/usr/bin/env bash

set -euo pipefail

__list_installed_plugins() {
asdf plugin-list 2>/dev/null | sort
asdf plugin-list 2>/dev/null | sort
}

__select_installed_plugin() {
__list_installed_plugins | fzf
}

__color_installed() {
awk '
BEGIN {
green="\033[32m";
red="\033[31m";
reset="\033[0m";
}
/(installed)/{
print green $0 reset;
next;
}
{
print red $0 reset;
}'
}

__list_all_plugins() {
join -t ' ' -a 1 \
<(asdf plugin-list-all 2>/dev/null | awk '{print $1}' | sort) \
<(__list_installed_plugins | awk '{print $1 " (installed)"}' | sort) |\
awk '
BEGIN {
green="\033[32m";
red="\033[31m";
reset="\033[0m";
}
/(installed)/{
print green $0 reset;
next;
}
{
print red $0 reset
}'
<( asdf plugin-list-all 2>/dev/null | awk '{print $1}' | sort ) \
<( __list_installed_plugins | awk '{print $1 " (installed)"}' | sort ) |
__color_installed
}


__list_current() {
asdf current 2>&1 | awk '{ print $1,$2 }' | column -t
}


__select_available_plugins() {
plugins=`__list_all_plugins`
cat <(echo "$plugins" | grep "installed") \
<(echo "$plugins" | grep -v "installed") | \
fzf -m --tac --ansi | awk '{print $1}'
plugins=$( __list_all_plugins )
cat <( echo "$plugins" | grep "installed" ) \
<( echo "$plugins" | grep -v "installed" ) |
fzf -m --tac --ansi | awk '{ print $1 }'
}

__select_installed_plugins() {
plugins=`__list_all_plugins`
cat <(echo "$plugins" | grep -v "installed") \
<(echo "$plugins" | grep "installed") | \
fzf -m --tac --ansi | awk '{print $1}'
plugins=$( __list_all_plugins )
cat <( echo "$plugins" | grep -v "installed" ) \
<( echo "$plugins" | grep "installed" ) |
fzf -m --tac --ansi | awk '{ print $1 }'
}

__version_filter() {
# Allow user-defined command to filter versions.
# Custom commands are either per tool with ASDFZF_VERSION_FILTER_COMMAND_plugin
# where plugin is the name of the plugin that installs the tool
# or defined globally with ASDFZF_VERSION_FILTER_COMMAND
#
# builtin filters are also selectable with ASDFZF_VERSION_FILTER_plugin
# and ASDFZF_VERSION_FILTER for global selection

varname="ASDFZF_VERSION_FILTER_COMMAND_$1"
if [[ -n "${!varname:-}" ]]; then
echo "${!varname}"
return
fi
varname="ASDFZF_VERSION_KEEP_$1"
if [[ -n "${!varname:-}" ]]; then
filter="${!varname}"
fi
if [[ -z "${filter:-}" ]]; then
if [[ -n "${ASDFZF_VERSION_FILTER_COMMAND:-}" ]]; then
echo "$ASDFZF_VERSION_FILTER_COMMAND"
return
fi
filter="${ASDFZF_VERSION_KEEP:-all}"
fi
case "$filter" in
all)
echo "cat"
;;
semver-no-prerelease)
echo "sed -E -n '/^[0-9]+(\.[0-9]+)*(\+[0-9A-Za-z\.-]+)*$/p'"
;;
semver-only-prerelease)
echo "sed -E -n '/^[0-9]+(\.[0-9]+)*-[0-9A-Za-z\.-]+(\+[0-9A-Za-z\.-]+)*/p'"
;;
*)
echo "Unknown version filter: $filter" >&2
exit 1
;;
esac
}

__list_all_versions() {
version_filter=$( __version_filter "$1" )
join -t ' ' -a 1 \
<(asdf list-all $1 2>/dev/null | awk '{print $1}' | sort) \
<(asdf list $1 2>/dev/null | awk '{print $1 " (installed)"}' | sort) | \
sort -V | \
awk '
BEGIN {
green="\033[32m";
red="\033[31m";
reset="\033[0m";
}
/(installed)/{
print green $0 reset;
next;
}
{
print red $0 reset
}'
<( asdf list-all "$1" 2>/dev/null | awk '{print $1}' | eval "$version_filter" | sort ) \
<( asdf list "$1" 2>/dev/null | awk '{print $1}' | eval "$version_filter" | awk '{print $1 " (installed)"}' | sort ) | \
sort -V |
__color_installed
}

__list_installed_versions() {
__list_all_versions $1 | grep 'installed'
__list_all_versions "$1" | grep 'installed'
}

__select_installed_version() {
__list_installed_versions $1 | \
fzf --tac --ansi --no-sort | \
awk '{print $1}'
__list_installed_versions "$1" |
fzf --tac --ansi --no-sort |
awk '{ print $1 }'

}

__select_available_versions() {
versions=`__list_all_versions $1`
versions=$( __list_all_versions "$1" )
cat <(echo "$versions" | grep "installed") \
<(echo "$versions" | grep -v "installed") |\
fzf -m --tac --ansi --no-sort | awk '{print $1}'
fzf -m --tac --ansi --no-sort | awk '{ print $1 }'
}

__select_installed_versions() {
versions=`__list_all_versions $1`
versions=$( __list_all_versions "$1" )
cat <(echo "$versions" | grep -v "installed") \
<(echo "$versions" | grep "installed") |\
fzf -m --tac --ansi --no-sort | awk '{print $1}'
fzf -m --tac --ansi --no-sort | awk '{ print $1 }'
}

__get_current_version() {
set +e
asdf current terraform | awk '{print $1}'
asdf current terraform | awk '{ print $1 }'
set -e
}

__multi() {
xargs -t -r -n 1 "$@"
}

_install() {
plugin=`__list_installed_plugins | fzf`
__select_available_versions $plugin | xargs -t -r -n 1 asdf install $plugin
plugin=$( __select_installed_plugin )
__select_available_versions "$plugin" | __multi asdf install "$plugin"
}

_uninstall() {
plugin=`__list_installed_plugins | fzf`
__select_installed_versions $plugin | xargs -t -r -n 1 asdf uninstall $plugin
plugin=$( __select_installed_plugin )
__select_installed_versions "$plugin" | __multi asdf uninstall "$plugin"
}

_plugin-add() {
__select_available_plugins | xargs -t -r -n 1 asdf plugin-add
__select_available_plugins | __multi asdf plugin-add
}

_plugin-remove() {
__select_installed_plugins | xargs -t -r -n 1 asdf plugin-remove
__select_installed_plugins | __multi asdf plugin-remove
}


_list() {
plugin=`__list_installed_plugins | fzf`
__list_all_versions $plugin | grep installed
plugin=$( __select_installed_plugin )
__list_all_versions "$plugin" | grep installed
}

_list-all() {
plugin=`__list_installed_plugins | fzf`
__list_all_versions $plugin
plugin=$( __select_installed_plugin )
__list_all_versions "$plugin"
}

_global() {
plugin=`__list_installed_plugins | fzf`
__select_installed_version $plugin | xargs -t -r -n 1 asdf global $plugin
plugin=$( __select_installed_plugin )
version=$( __select_installed_version "$plugin" )
asdf global "$plugin" "$version"
}

_local() {
plugin=`__list_installed_plugins | fzf`
__select_installed_version $plugin | xargs -t -r -n 1 asdf local $plugin
plugin=$( __select_installed_plugin )
version=$( __select_installed_version "$plugin" )
asdf local "$plugin" "$version"
}

_current() {
asdf current 2>&1 | awk '{print $1,$2}' | column -t | fzf
__list_current | fzf
}


main() {
operation=$(cat <<-EOF | fzf

__list_operations() {
cat <<-EOF
install
uninstall
plugin-add
Expand All @@ -145,8 +197,15 @@ local
global
current
EOF
)
_$operation
}

__select_operation() {
__list_operations | fzf
}

main() {
operation=$(__select_operation)
"_$operation"
}

main

0 comments on commit e2f6a11

Please sign in to comment.