asdf/bin/asdf

109 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# 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
local result=
result="$(find_cmd "$(get_plugin_path "$1")/lib/commands" "${@:2}")"
ASDF_CMD_FILE=${result% *}
args_offset=${result##* }
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
# Internal Variables
ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=$(asdf_tool_versions_filename)
export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
ASDF_CONFIG_FILE=$(asdf_config_file)
export ASDF_CONFIG_FILE
ASDF_DATA_DIR=$(asdf_data_dir)
export ASDF_DATA_DIR
ASDF_DIR=$(asdf_dir)
export ASDF_DIR
local result=
result="$(find_asdf_cmd "$@")"
ASDF_CMD_FILE=${result% *}
args_offset=${result##* }
if [ -z "$ASDF_CMD_FILE" ]; then
result="$(find_plugin_cmd "$@")"
ASDF_CMD_FILE=${result% *}
args_offset=${result##* }
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 "$@"