asdf/lib/commands/version_commands.bash

55 lines
1.3 KiB
Bash
Raw Normal View History

# -*- sh -*-
version_command() {
local cmd=$1
local plugin_name=$2
if [ "$#" -lt "3" ]; then
2018-10-19 17:29:31 -07:00
if [ "$cmd" = "global" ]; then
2020-09-21 15:27:52 -07:00
printf "Usage: asdf global <name> <version>\\n"
2018-10-19 17:29:31 -07:00
else
2020-09-21 15:27:52 -07:00
printf "Usage: asdf local <name> <version>\\n"
2018-10-19 17:29:31 -07:00
fi
exit 1
fi
shift 2
2017-09-04 10:04:56 -07:00
local versions=("$@")
local file
if [ "$cmd" = "global" ]; then
file=${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME:-$HOME/.tool-versions}
2018-10-19 17:29:31 -07:00
elif [ "$cmd" = "local-tree" ]; then
file=$(find_tool_versions)
else # cmd = local
2017-09-04 10:04:56 -07:00
file="$(pwd)/.tool-versions"
2016-05-13 19:11:16 -07:00
fi
if [ -L "$file" ]; then
2019-11-29 09:15:38 -07:00
# Resolve file path if symlink
file="$(resolve_symlink "$file")"
fi
check_if_plugin_exists "$plugin_name"
2017-09-04 10:04:56 -07:00
declare -a resolved_versions
2017-09-04 10:04:56 -07:00
local version
for version in "${versions[@]}"; do
if [ "$version" = "latest" ]; then
version=$(asdf latest "$plugin_name")
fi
if ! (check_if_version_exists "$plugin_name" "$version"); then
version_not_installed_text "$plugin_name" "$version" 1>&2
exit 1
fi
resolved_versions+=("$version")
done
if [ -f "$file" ] && grep "^$plugin_name " "$file" >/dev/null; then
sed -i.bak -e "s|^$plugin_name .*$|$plugin_name ${resolved_versions[*]}|" "$file"
rm "$file".bak
else
2020-09-21 15:27:52 -07:00
printf "%s %s\\n" "$plugin_name" "${resolved_versions[*]}" >>"$file"
fi
}