/
command-help.bash
112 lines (94 loc) · 2.94 KB
/
command-help.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- sh -*-
asdf_help() {
printf "version: %s\\n\\n" "$(asdf_version)"
cat "$(asdf_dir)/help.txt"
}
asdf_moto() {
cat <<EOF
"Late but latest"
-- Rajinikanth
EOF
}
asdf_extension_cmds() {
local plugins_path ext_cmds plugin
plugins_path="$(get_plugin_path)"
# use find instead of ls -1
# shellcheck disable=SC2012
ext_cmds="$(ls -1 "$plugins_path"/*/lib/commands/command*.bash 2>/dev/null |
sed "s#^$plugins_path/##;s#lib/commands/command##;s/.bash//;s/^-//;s/-/ /g")"
if test -n "$ext_cmds"; then
cut -d'/' -f 1 <<<"$ext_cmds" | uniq | while read -r plugin; do
printf "\\nPLUGIN %s\\n" "$plugin"
grep "$plugin/" <<<"$ext_cmds" | sed "s#^$plugin/# asdf $plugin#" | sort
done
fi
}
help_command() {
local plugin_name="$1"
local tool_version="$2"
local plugin_path
# If plugin name is present as first argument output plugin help info
if [ -n "$plugin_name" ]; then
plugin_path=$(get_plugin_path "$plugin_name")
if [ -d "$plugin_path" ]; then
if [ -f "${plugin_path}/bin/help.overview" ]; then
if [ -n "$tool_version" ]; then
# TODO: Refactor this code out into helper functions in utils.bash
IFS=':' read -r -a version_info <<<"$tool_version"
if [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
if [ "${version_info[0]}" = "latest" ]; then
local version
version=$(asdf latest "$plugin_name" "${version_info[1]}")
else
local version="${version_info[0]}"
fi
fi
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
(
# shellcheck disable=SC2031
export ASDF_INSTALL_TYPE=$install_type
# shellcheck disable=SC2031
export ASDF_INSTALL_VERSION=$version
# shellcheck disable=SC2031
export ASDF_INSTALL_PATH=$install_path
print_plugin_help "$plugin_path"
)
else
(print_plugin_help "$plugin_path")
fi
else
printf "No documentation for plugin %s\\n" "$plugin_name" >&2
exit 1
fi
else
printf "No plugin named %s\\n" "$plugin_name" >&2
exit 1
fi
else
# Otherwise output general asdf help
asdf_help
asdf_extension_cmds
asdf_moto
fi
}
print_plugin_help() {
local plugin_path=$1
# Eventually @jthegedus or someone else will format the output from these
# scripts in a certain way.
"${plugin_path}"/bin/help.overview
if [ -f "${plugin_path}"/bin/help.deps ]; then
"${plugin_path}"/bin/help.deps
fi
if [ -f "${plugin_path}"/bin/help.config ]; then
"${plugin_path}"/bin/help.config
fi
if [ -f "${plugin_path}"/bin/help.links ]; then
"${plugin_path}"/bin/help.links
fi
}
help_command "$@"