Skip to content

Commit 1334488

Browse files
committed
Improve utility functions
1 parent dfdea9c commit 1334488

9 files changed

+198
-136
lines changed

pkg/src/commands/woof-get-version.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ woof-get-version() {
2020
args+=("$arg")
2121
esac done; unset -v arg
2222

23-
helper.determine_tool_pair "${args[0]}"
24-
declare -g g_tool_pair="$REPLY"
25-
declare -g g_plugin_name="$REPLY1"
26-
declare -g g_tool_name="$REPLY2"
23+
helper.determine_tool_pair_active "${args[0]}"
24+
declare -g g_tool_pair="$REPLY1"
25+
declare -g g_plugin_name="$REPLY2"
26+
declare -g g_tool_name="$REPLY3"
2727

2828
local tool_version=
2929
if [ "$flag_global" = 'yes' ]; then

pkg/src/commands/woof-install.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ woof-install() {
2727
args+=("$arg")
2828
esac done; unset -v arg
2929

30-
helper.determine_tool_pair "${args[0]}"
31-
declare -g g_tool_pair="$REPLY"
32-
declare -g g_plugin_name="$REPLY1"
33-
declare -g g_tool_name="$REPLY2"
30+
helper.determine_tool_pair_active "${args[0]}"
31+
declare -g g_tool_pair="$REPLY1"
32+
declare -g g_plugin_name="$REPLY2"
33+
declare -g g_tool_name="$REPLY3"
3434

3535
helper.create_version_table "$flag_no_cache"
3636

37-
helper.determine_tool_version --allow-latest "${args[1]}"
37+
helper.determine_tool_version_active --allow-latest "${args[1]}"
3838
declare -g g_tool_version="$REPLY"
3939

4040
local flag_interactive='no'

pkg/src/commands/woof-set-version.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ woof-set-version() {
2020
args+=("$arg")
2121
esac done; unset -v arg
2222

23-
helper.determine_tool_pair "${args[0]}"
24-
declare -g g_tool_pair="$REPLY"
25-
declare -g g_plugin_name="$REPLY1"
26-
declare -g g_tool_name="$REPLY2"
23+
helper.determine_tool_pair_active "${args[0]}"
24+
declare -g g_tool_pair="$REPLY1"
25+
declare -g g_plugin_name="$REPLY2"
26+
declare -g g_tool_name="$REPLY3"
2727

2828
helper.determine_tool_version_installed "$g_tool_pair" "${args[1]}"
2929
local g_tool_version="$REPLY"

pkg/src/commands/woof-tool.sh

+7-7
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ woof-tool() {
4747
done; unset -v var_name
4848
unset -vn var_value
4949
elif [ "$subcmd" = 'debug-table' ]; then
50-
helper.determine_tool_pair "$1"
51-
local tool_name="$REPLY2"
50+
helper.determine_tool_pair_active "$1"
51+
local tool_name="$REPLY3"
5252

5353
util.run_function "$tool_name.table"
5454
elif [ "$subcmd" = 'debug-install' ]; then
55-
helper.determine_tool_pair "$1"
56-
declare -g g_tool_pair="$REPLY"
57-
declare -g g_plugin_name="$REPLY1"
58-
declare -g g_tool_name="$REPLY2"
55+
helper.determine_tool_pair_active "$1"
56+
declare -g g_tool_pair="$REPLY1"
57+
declare -g g_plugin_name="$REPLY2"
58+
declare -g g_tool_name="$REPLY3"
5959

6060
helper.create_version_table "$g_tool_pair" 'yes'
6161

62-
helper.determine_tool_version "$2"
62+
helper.determine_tool_version_active "$2"
6363
local g_tool_version="$REPLY"
6464

6565
local flag_interactive='yes'

pkg/src/commands/woof-uninstall.sh

+11-9
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ woof-uninstall() {
1717
esac done; unset -v arg
1818

1919
helper.determine_tool_pair_installed "${args[0]}"
20-
local tool_pair="$REPLY"
20+
declare -g g_tool_pair="$REPLY1"
21+
declare -g g_plugin_name="$REPLY2"
22+
declare -g g_tool_name="$REPLY3"
2123

22-
helper.determine_tool_version_installed "$tool_pair" "${args[1]}"
23-
local tool_version="$REPLY"
24+
helper.determine_tool_version_installed "$g_tool_pair" "${args[1]}"
25+
declare -g g_tool_version="$REPLY"
2426

25-
var.get_dir 'tools' "$tool_pair"
27+
var.get_dir 'tools' "$g_tool_pair"
2628
local install_dir="$REPLY"
2729

2830
# Do uninstall
29-
printf '%s\n' "Uninstalling $tool_pair"
31+
printf '%s\n' "Uninstalling $g_tool_pair"
3032
# Note that this is a redundant check since it is done by helper.determine_tool_version_installed(), but we
3133
# do it anyways, Just in Case
32-
if [ -e "$install_dir/$tool_version" ]; then
33-
rm -rf "${install_dir:?}/$tool_version"
34-
util.print_info "Removed version '$tool_version' for plugin '$tool_pair'"
34+
if [ -e "$install_dir/$g_tool_version" ]; then
35+
rm -rf "${install_dir:?}/$g_tool_version"
36+
util.print_info "Removed version '$g_tool_version' for plugin '$g_tool_pair'"
3537
else
36-
util.print_error_die "Version '$tool_version' for plugin '$tool_pair' is not installed"
38+
util.print_error_die "Version '$g_tool_version' for plugin '$g_tool_pair' is not installed"
3739
fi
3840
}

pkg/src/util/helper-determine.sh

+18-92
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,13 @@
11
# shellcheck shell=bash
22

3-
# @description Tool names aren't required to be specified on the command line. If one
4-
# isn't specified, then start a TUI selection screen
5-
helper.determine_tool_pair() {
3+
helper.determine_tool_pair_active() {
64
unset -v REPLY; REPLY=
75
local input="$1"
86

9-
local tool_pair=
10-
local plugin_name=
11-
local tool_name=
12-
13-
if [ -z "$input" ]; then
14-
util.plugin_get_plugins --filter=active --with=name
15-
local -a all_plugins_arr=("${REPLY[@]}")
16-
local -A all_plugins_obj=()
17-
for m in "${all_plugins_arr[@]}"; do
18-
all_plugins_obj["$m"]=
19-
done; unset -v m
20-
tty.multiselect '' all_plugins_arr all_plugins_obj
21-
plugin_name="$REPLY"
22-
23-
util.plugin_get_active_tools_of_plugin "$plugin_name"
24-
local all_tools_arr=("${REPLY[@]}")
25-
local -A all_tools_obj=()
26-
for m in "${all_tools_arr[@]}"; do
27-
all_tools_obj["$m"]=
28-
done; unset -v m
29-
tty.multiselect '' all_tools_arr all_tools_obj
30-
tool_name="$REPLY"
31-
32-
tool_pair="$plugin_name/$tool_name"
33-
elif [[ "$input" != */* ]]; then
34-
util.plugin_get_active_tools --with=pair
35-
local tools=("${REPLY[@]}")
36-
37-
local tool=
38-
for tool in "${tools[@]}"; do
39-
if [ "$input" = "${tool#*/}" ]; then
40-
plugin_name=${tool%/*}
41-
tool_name=${tool#*/}
42-
43-
tool_pair="$plugin_name/$tool_name"
44-
break
45-
fi
46-
done
47-
48-
if [ -z "$tool_name" ]; then
49-
50-
util.plugin_get_active_tools_of_plugin "$input"
51-
local all_tools_arr=("${REPLY[@]}")
52-
local -A all_tools_obj=()
53-
for m in "${all_tools_arr[@]}"; do
54-
all_tools_obj["$m"]=
55-
done; unset -v m
56-
tty.multiselect '' all_tools_arr all_tools_obj
57-
local _tool_name="$REPLY"
58-
59-
plugin_name=$input
60-
tool_name="$_tool_name"
61-
62-
tool_pair="$plugin_name/$tool_name"
63-
fi
64-
else
65-
plugin_name=${input%/*}
66-
tool_name=${input#*/}
67-
68-
tool_pair=$input
69-
fi
7+
util.determine_tool_pair "$input" 'active'
8+
local tool_pair=$REPLY1
9+
local plugin_name=$REPLY2
10+
local tool_name=$REPLY3
7011

7112
var.get_tool_file "$plugin_name" "$tool_name"
7213
local tool_file="$REPLY"
@@ -80,48 +21,33 @@ helper.determine_tool_pair() {
8021
util.print_error_die "Could not successfully source plugin '$tool_pair'"
8122
fi
8223

83-
REPLY=$tool_pair
84-
REPLY1=$plugin_name
85-
REPLY2=$tool_name
24+
REPLY1=$tool_pair
25+
REPLY2=$plugin_name
26+
REPLY3=$tool_name
8627
}
8728

8829
helper.determine_tool_pair_installed() {
8930
unset -v REPLY; REPLY=
90-
local tool_pair="$1"
31+
local input="$1"
32+
33+
util.determine_tool_pair "$input" 'installed'
34+
local tool_pair=$REPLY1
35+
local plugin_name=$REPLY2
36+
local tool_name=$REPLY3
9137

9238
var.get_dir 'tools' "$tool_pair"
9339
local install_dir="$REPLY"
9440

95-
if [ -z "$tool_pair" ]; then
96-
core.shopt_push -s nullglob
97-
local -a plugin_list=("$install_dir"/*/)
98-
core.shopt_pop
99-
100-
if (( ${#plugin_list[@]} == 0 )); then
101-
util.print_error_die "Cannot uninstall as no plugins are installed"
102-
fi
103-
104-
plugin_list=("${plugin_list[@]%/}")
105-
plugin_list=("${plugin_list[@]##*/}")
106-
107-
local -A plugins_table=()
108-
local plugin=
109-
for plugin in "${plugin_list[@]}"; do
110-
plugins_table["$plugin"]=
111-
done; unset plugin
112-
113-
tty.multiselect 0 plugin_list plugins_table
114-
tool_pair=$REPLY
115-
fi
116-
11741
if [ ! -d "$install_dir" ]; then
11842
util.print_error_die "No versions of plugin '$tool_pair' are installed"
11943
fi
12044

121-
REPLY=$tool_pair
45+
REPLY1=$tool_pair
46+
REPLY2=$plugin_name
47+
REPLY3=$tool_name
12248
}
12349

124-
helper.determine_tool_version() {
50+
helper.determine_tool_version_active() {
12551
unset -v REPLY; REPLY=
12652
local flag_allow_latest='no'
12753
if [ "$1" = '--allow-latest' ]; then

pkg/src/util/helper-tool.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ util.tool_list_global_versions() {
151151
printf '%s\n' "dir: $tool_dir"
152152
done; unset -v tool_dir
153153

154-
# helper.determine_tool_pair "$1"
154+
# helper.determine_tool_pair_active "$1"
155155
# declare -g g_tool_pair="$REPLY"
156156
# declare -g g_plugin_name="$REPLY1"
157157
# declare -g g_tool_name="$REPLY2"

pkg/src/util/util-plugin.sh

+77-14
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,32 @@ util.plugin_get_plugins() {
88
--filter=*)
99
local value=${arg#--filter=}
1010
case $value in
11-
none|active)
12-
flag_with=$value
13-
;;
14-
*)
15-
util.print_error_die "Flag '$arg' could not be evaluated"
16-
;;
11+
none|active|installed)
12+
flag_with=$value
13+
;;
14+
*)
15+
util.print_error_die "Flag '$arg' could not be evaluated"
16+
;;
1717
esac
1818
;;
1919
--with=*)
2020
local value=${arg#--with=}
2121
case $value in
22-
filepath|name)
23-
flag_with=$value
24-
;;
25-
*)
26-
util.print_error_die "Flag '$arg' could not be evaluated"
27-
;;
22+
filepath|name)
23+
flag_with=$value
24+
;;
25+
*)
26+
util.print_error_die "Flag '$arg' could not be evaluated"
27+
;;
2828
esac
2929
;;
3030
*)
3131
util.print_error_die "Flag '$arg' not recognized"
3232
;;
33-
esac done
33+
esac done; unset -v arg
34+
35+
var.get_dir 'tools'
36+
local tools_dir="$REPLY"
3437

3538
var.get_dir 'plugins'
3639
local plugins_dir="$REPLY"
@@ -45,10 +48,16 @@ util.plugin_get_plugins() {
4548
plugin_name=${dir##*/}
4649
plugin_name=${plugin_name#woof-plugin-}
4750

48-
if [ "$flag_filter" = 'active' ]; then
51+
if [ "$flag_filter" = 'none' ]; then
52+
:
53+
elif [ "$flag_filter" = 'active' ]; then
4954
if ! util.plugin_is_enabled "$plugin_name"; then
5055
continue
5156
fi
57+
elif [ "$flag_filter" = 'installed' ]; then
58+
if [ ! -d "$tools_dir/$plugin_name" ]; then
59+
continue
60+
fi
5261
fi
5362

5463
if [ "$flag_with" = 'filepath' ]; then
@@ -64,6 +73,60 @@ util.plugin_get_plugins() {
6473
unset -v dir plugin_name entry
6574
}
6675

76+
util.plugin_get_plugin_tools() {
77+
local plugin_name="$1"
78+
79+
local flag_filter='none'
80+
local flag_with='none'
81+
local arg=
82+
for arg; do case $arg in
83+
--filter=*)
84+
local value=${arg#--filter=}
85+
case $value in
86+
none|active|installed)
87+
flag_with=$value
88+
;;
89+
*)
90+
util.print_error_die "Flag '$arg' could not be evaluated"
91+
;;
92+
esac
93+
;;
94+
--with=*)
95+
local value=${arg#--with=}
96+
case $value in
97+
filepath|name)
98+
flag_with=$value
99+
;;
100+
*)
101+
util.print_error_die "Flag '$arg' could not be evaluated"
102+
;;
103+
esac
104+
;;
105+
-*)
106+
util.print_error_die "Flag '$arg' not recognized"
107+
;;
108+
esac done; unset -v arg
109+
110+
var.get_dir 'plugins'
111+
local plugins_dir="$REPLY"
112+
113+
unset -v REPLY
114+
declare -ga REPLY=()
115+
116+
local tool= tool_name= entry=
117+
for tool in "$plugins_dir/woof-plugin-$plugin_name/tools/"*.sh; do
118+
if [ "$flag_with" = 'filepath' ]; then
119+
entry=$tool
120+
elif [ "$flag_with" = 'name' ]; then
121+
entry=${tool##*/}
122+
entry=${entry%.sh}
123+
fi
124+
125+
REPLY+=("$entry")
126+
done
127+
unset -v tool tool_name
128+
}
129+
67130
util.plugin_get_active_tools_of_plugin() {
68131
local plugin="$1"
69132

0 commit comments

Comments
 (0)