asdf.bash 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env bash
  2. _asdf () {
  3. local cur
  4. cur=${COMP_WORDS[COMP_CWORD]}
  5. local cmd
  6. cmd=${COMP_WORDS[1]}
  7. local prev
  8. prev=${COMP_WORDS[COMP_CWORD-1]}
  9. local plugins
  10. plugins=$(asdf plugin-list 2> /dev/null | tr '\n' ' ')
  11. # We can safely ignore warning SC2207 since it warns that it will uses the
  12. # shell's sloppy word splitting and globbing. The possible commands here are
  13. # all single words, and most likely won't contain special chars the shell will
  14. # expand.
  15. COMPREPLY=()
  16. case "$cmd" in
  17. plugin-update)
  18. # shellcheck disable=SC2207
  19. COMPREPLY=($(compgen -W "$plugins --all" -- "$cur"))
  20. ;;
  21. plugin-remove|current|list|list-all)
  22. # shellcheck disable=SC2207
  23. COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
  24. ;;
  25. plugin-add)
  26. local available_plugins
  27. available_plugins=$( (asdf plugin-list 2> /dev/null && asdf plugin-list-all 2> /dev/null) | sort | uniq -u)
  28. # shellcheck disable=SC2207
  29. COMPREPLY=($(compgen -W "$available_plugins" -- "$cur"))
  30. ;;
  31. install)
  32. if [[ "$plugins" == *"$prev"* ]] ; then
  33. local versions
  34. versions=$(asdf list-all "$prev" 2> /dev/null)
  35. # shellcheck disable=SC2207
  36. COMPREPLY=($(compgen -W "$versions" -- "$cur"))
  37. else
  38. # shellcheck disable=SC2207
  39. COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
  40. fi
  41. ;;
  42. update)
  43. # shellcheck disable=SC2207
  44. COMPREPLY=($(compgen -W "--head" -- "$cur"))
  45. ;;
  46. uninstall|where|reshim|local|global)
  47. if [[ "$plugins" == *"$prev"* ]] ; then
  48. local versions
  49. versions=$(asdf list "$prev" 2> /dev/null)
  50. # shellcheck disable=SC2207
  51. COMPREPLY=($(compgen -W "$versions" -- "$cur"))
  52. else
  53. # shellcheck disable=SC2207
  54. COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
  55. fi
  56. ;;
  57. *)
  58. local cmds='current global help install list list-all local plugin-add plugin-list plugin-list-all plugin-remove plugin-update reshim uninstall update where which '
  59. # shellcheck disable=SC2207
  60. COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
  61. ;;
  62. esac
  63. return 0
  64. }
  65. complete -F _asdf asdf