Implement 'asdf local -p'.

This commit is contained in:
Trevor Brown 2018-10-19 20:29:31 -04:00
parent e8905b42bc
commit fc078c5e72
2 changed files with 36 additions and 5 deletions

View File

@ -3,7 +3,11 @@ version_command() {
local plugin=$2
if [ "$#" -lt "3" ]; then
echo "Usage: asdf $cmd <name> <version>"
if [ "$cmd" = "global" ]; then
echo "Usage: asdf global <name> <version>"
else
echo "Usage: asdf local <name> <version>"
fi
exit 1
fi
@ -13,7 +17,9 @@ version_command() {
local file
if [ "$cmd" = "global" ]; then
file=${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME:-$HOME/.tool-versions}
else
elif [ "$cmd" = "local-tree" ]; then
file=$(find_tool_versions)
else # cmd = local
file="$(pwd)/.tool-versions"
fi
@ -39,8 +45,32 @@ version_command() {
}
local_command() {
# shellcheck disable=2068
version_command "local" $@
local parent=false
local positional=()
while [[ $# -gt 0 ]]
do
case $1 in
-p|--parent)
parent="true"
shift # past value
;;
*)
positional+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${positional[@]}" # restore positional parameters
if [ $parent = true ]; then
# shellcheck disable=2068
version_command "local-tree" $@
else
# shellcheck disable=2068
version_command "local" $@
fi
}
global_command() {

View File

@ -114,6 +114,7 @@ teardown() {
@test "local -p/--parent should allow multiple versions" {
cd $CHILD_DIR
touch $PROJECT_DIR/.tool-versions
run local_command -p "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0 1.0.0" ]
@ -129,7 +130,7 @@ teardown() {
@test "local -p/--parent should set the version if it's unset" {
cd $CHILD_DIR
echo 'dummy 1.0.0' >> $PROJECT_DIR/.tool-versions
touch $PROJECT_DIR/.tool-versions
run local_command -p "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]