asdf/lib/commands/command-export-shell-version.bash
Trevor Brown 27f7ef7852
fix: don't invoke asdf inside asdf commands (#1208)
* fix: don't invoke asdf inside asdf commands

Recursive calls have a number of disadvantages:

* Poorer performance since each invocation spawns and new process and re-executes all the code in bin/asdf
* Makes debugging more difficult
* More likely to introduce subtle bugs and the possibility for infinite loops
2022-04-25 08:45:19 -04:00

66 lines
1.7 KiB
Bash

# -*- sh -*-
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"
# Output from this command must be executable shell code
shell_command() {
local asdf_shell="$1"
shift
if [ "$#" -lt "2" ]; then
printf "Usage: asdf shell <name> {<version>|--unset}\\n" >&2
printf "false\\n"
exit 1
fi
local plugin=$1
local version=$2
local upcase_name
upcase_name=$(tr '[:lower:]-' '[:upper:]_' <<<"$plugin")
local version_env_var="ASDF_${upcase_name}_VERSION"
if [ "$version" = "--unset" ]; then
case "$asdf_shell" in
fish)
printf "set -e %s\\n" "$version_env_var"
;;
elvish)
# Elvish doesn't have a `source` command, and eval is banned, so the
# var name and value are printed on separate lines for asdf.elv to parse
# and pass to unset-env.
printf "unset-env\n%s" "$version_env_var"
;;
*)
printf "unset %s\\n" "$version_env_var"
;;
esac
exit 0
fi
if [ "$version" = "latest" ]; then
version=$(latest_command "$plugin")
fi
if ! (check_if_version_exists "$plugin" "$version"); then
version_not_installed_text "$plugin" "$version" 1>&2
printf "false\\n"
exit 1
fi
case "$asdf_shell" in
fish)
printf "set -gx %s \"%s\"\\n" "$version_env_var" "$version"
;;
elvish)
# Elvish doesn't have a `source` command, and eval is banned, so the
# var name and value are printed on separate lines for asdf.elv to parse
# and pass to set-env.
printf "set-env\n%s\n%s" "$version_env_var" "$version"
;;
*)
printf "export %s=\"%s\"\\n" "$version_env_var" "$version"
;;
esac
}
shell_command "$@"