Skip to content

Commit

Permalink
refactor - deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
fistach committed Apr 24, 2024
1 parent 3e8fa1c commit f90d907
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/main/bash/sdkman-use.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,7 @@ function __sdk_use() {
elif [[ ${count} -eq 1 ]]
then
version=$(basename $(ls -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${major_version}"*))
if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then
local matched_version

if [[ "$zsh_shell" == "true" ]]; then
matched_version=${match[1]}
else
matched_version=${BASH_REMATCH[1]}
fi

export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}}
fi
__sdkman_change_candidate_in_path "$candidate"
__sdkman_echo_green "Using ${candidate} version ${version} in this shell."
else
echo ""
Expand All @@ -74,18 +64,7 @@ function __sdk_use() {
# Just update the *_HOME and PATH for this shell.
__sdkman_set_candidate_home "$candidate" "$version"

if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then
local matched_version

if [[ "$zsh_shell" == "true" ]]; then
matched_version=${match[1]}
else
matched_version=${BASH_REMATCH[1]}
fi

export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}}
fi

__sdkman_change_candidate_in_path "$candidate"
if [[ ! (-L "${SDKMAN_CANDIDATES_DIR}/${candidate}/current" || -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/current") ]]; then
__sdkman_echo_green "Setting ${candidate} version ${version} as default."
__sdkman_link_candidate_version "$candidate" "$version"
Expand All @@ -95,3 +74,17 @@ function __sdk_use() {
__sdkman_echo_green "Using ${candidate} version ${version} in this shell."
fi
}

function __sdkman_change_candidate_in_path() {
if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${1}/([^/]+) ]]; then
local matched_version

if [[ "$zsh_shell" == "true" ]]; then
matched_version=${match[1]}
else
matched_version=${BASH_REMATCH[1]}
fi

export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}}
fi
}

0 comments on commit f90d907

Please sign in to comment.