asdf/bin/asdf
2022-05-17 08:43:21 -04:00

89 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -o nounset
# shellcheck source=lib/utils.bash
. "$(dirname "$(dirname "$0")")/lib/utils.bash"
find_cmd() {
local cmd_dir="$1"
shift
local cmd_name
local args_offset="$#"
cmd_name="command-$(tr ' ' '-' <<<"${@:1:${args_offset}}").bash"
while [ ! -f "$cmd_dir/$cmd_name" ] && [ "$args_offset" -gt 0 ]; do
args_offset=$((args_offset - 1))
cmd_name="command-$(tr ' ' '-' <<<"${@:1:${args_offset}}").bash"
done
if [ -f "$cmd_dir/$cmd_name" ]; then
printf "%s %s\\n" "$cmd_dir/$cmd_name" "$((args_offset + 1))"
elif [ -f "$cmd_dir/command.bash" ]; then
printf "%s %s\\n" "$cmd_dir/command.bash" 1
fi
}
find_asdf_cmd() {
local asdf_cmd_dir
asdf_cmd_dir="$(asdf_dir)/lib/commands"
case "$1" in
'exec' | 'current' | 'env' | 'global' | 'install' | 'latest' | 'local' | \
'reshim' | 'uninstall' | 'update' | 'where' | 'which' | \
'export-shell-version')
printf "%s %s\\n" "$asdf_cmd_dir/command-$1.bash" 2
;;
'' | '--help' | '-h' | 'help')
printf "%s %s\\n" "$asdf_cmd_dir/command-help.bash" 2
;;
'--version' | 'version')
printf "%s %s\\n" "$asdf_cmd_dir/command-version.bash" 2
;;
*)
find_cmd "$asdf_cmd_dir" "$@"
;;
esac
}
find_plugin_cmd() {
local ASDF_CMD_FILE args_offset
if [ -d "$(get_plugin_path "$1")/bin" ]; then
IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_cmd "$(get_plugin_path "$1")/lib/commands" "${@:2}")"
if [ -n "$ASDF_CMD_FILE" ]; then
args_offset=$((args_offset + 1)) # since the first argument is the plugin name
printf "%s %s\\n" "$ASDF_CMD_FILE" "$args_offset"
fi
fi
}
asdf_cmd() {
local ASDF_CMD_FILE args_offset
if [ "shell" == "$1" ]; then
printf "Shell integration is not enabled. Please ensure you source asdf in your shell setup." >&2
exit 1
fi
IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_asdf_cmd "$@")"
if [ -z "$ASDF_CMD_FILE" ]; then
IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_plugin_cmd "$@")"
fi
if [ -x "$ASDF_CMD_FILE" ]; then
exec "$ASDF_CMD_FILE" "${@:${args_offset}}"
elif [ -f "$ASDF_CMD_FILE" ]; then
set -- "${@:${args_offset}}"
. "$ASDF_CMD_FILE"
else
local asdf_cmd_dir
asdf_cmd_dir="$(asdf_dir)/lib/commands"
printf "%s\\n" "Unknown command: \`asdf ${*}\`" >&2
. "$asdf_cmd_dir/command-help.bash" >&2
return 127
fi
}
asdf_cmd "$@"