Files
sdkman-cli/contrib/completion/bash/sdk
Oliver Weiler f473322276 Add tests for Bash programmable completion (#998)
* Add tests for Bash completion
* Fix broken uname stub setup
* Let test task depend on prepareContrib
* Quote array expansion
2021-10-16 16:41:58 +01:00

86 lines
2.1 KiB
Bash

#!/usr/bin/bash
_sdk() {
local -r previous_word="${COMP_WORDS[COMP_CWORD - 1]}"
local -r current_word="${COMP_WORDS[COMP_CWORD]}"
if ((COMP_CWORD == 3)); then
local -r before_previous_word="${COMP_WORDS[COMP_CWORD - 2]}"
__sdkman_complete_candidate_version "$before_previous_word" "$previous_word" "$current_word"
return
fi
__sdkman_complete_command "$previous_word" "$current_word"
}
__sdkman_complete_command() {
local -r command=$1
local -r current_word=$2
local -a candidates
case $command in
sdk)
candidates=("install" "uninstall" "list" "use" "config" "default" "home" "env" "current" "upgrade" "version" "broadcast" "help" "offline" "selfupdate" "update" "flush")
;;
current|default|home|uninstall|upgrade|use)
local -r candidate_paths=("${SDKMAN_CANDIDATES_DIR}"/*)
for candidate_path in "${candidate_paths[@]}"; do
candidates+=(${candidate_path##*/})
done
;;
install|list)
local -r all_candidates=$(curl --silent "${SDKMAN_CANDIDATES_API}/candidates/all")
IFS=',' read -r -a candidates <<< "$all_candidates"
;;
env)
candidates=("init install clear")
;;
offline)
candidates=("enable" "disable")
;;
selfupdate)
candidates=("force")
;;
flush)
candidates=("archives" "temp" "broadcast" "version")
;;
esac
COMPREPLY=($(compgen -W "${candidates[*]}" -- "$current_word"))
}
__sdkman_complete_candidate_version() {
local -r command=$1
local -r candidate=$2
local -r candidate_version=$3
local -a candidates
case $command in
use|default|home|uninstall)
local -r version_paths=("${SDKMAN_CANDIDATES_DIR}/${candidate}"/*)
for version_path in "${version_paths[@]}"; do
[[ $version_path = *current ]] && continue
candidates+=(${version_path##*/})
done
;;
install)
local -r all_candidate_versions=$(curl --silent "${SDKMAN_CANDIDATES_API}/candidates/$candidate/${SDKMAN_PLATFORM}/versions/all")
IFS=',' read -r -a candidates <<< "$all_candidate_versions"
;;
esac
COMPREPLY=($(compgen -W "${candidates[*]}" -- "$candidate_version"))
}
complete -o default -F _sdk sdk
# Set 'sdkman_auto_complete' to 'true' in .sdkman/etc/config to enable completion