bash-completion 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # Copyright (c) 2010, Aneurin Price <aneurin.price@gmail.com>
  2. # Permission is hereby granted, free of charge, to any person
  3. # obtaining a copy of this software and associated documentation
  4. # files (the "Software"), to deal in the Software without
  5. # restriction, including without limitation the rights to use,
  6. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the
  8. # Software is furnished to do so, subject to the following
  9. # conditions:
  10. # The above copyright notice and this permission notice shall be
  11. # included in all copies or substantial portions of the Software.
  12. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  14. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  16. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  19. # OTHER DEALINGS IN THE SOFTWARE.
  20. __zfs_get_commands()
  21. {
  22. zfs 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
  23. }
  24. __zfs_get_properties()
  25. {
  26. zfs get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
  27. }
  28. __zfs_get_editable_properties()
  29. {
  30. zfs get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
  31. }
  32. __zfs_get_inheritable_properties()
  33. {
  34. zfs get 2>&1 | awk '$3 == "YES" {print $1}'
  35. }
  36. __zfs_list_datasets()
  37. {
  38. zfs list -H -o name
  39. }
  40. __zfs_list_filesystems()
  41. {
  42. zfs list -H -o name -t filesystem
  43. }
  44. __zfs_list_snapshots()
  45. {
  46. zfs list -H -o name -t snapshot
  47. }
  48. __zfs_list_volumes()
  49. {
  50. zfs list -H -o name -t volume
  51. }
  52. __zfs_argument_chosen()
  53. {
  54. for word in $(seq $((COMP_CWORD-1)) -1 2)
  55. do
  56. local prev="${COMP_WORDS[$word]}"
  57. for property in $@
  58. do
  59. if [ "x$prev" = "x$property" ]
  60. then
  61. return 0
  62. fi
  63. done
  64. done
  65. return 1
  66. }
  67. __zfs_complete_ordered_arguments()
  68. {
  69. local list1=$1
  70. local list2=$2
  71. local cur=$3
  72. local extra=$4
  73. if __zfs_argument_chosen $list1
  74. then
  75. COMPREPLY=($(compgen -W "$list2 $extra" -- "$cur"))
  76. else
  77. COMPREPLY=($(compgen -W "$list1 $extra" -- "$cur"))
  78. fi
  79. }
  80. __zfs_complete()
  81. {
  82. local cur prev cmd cmds
  83. COMPREPLY=()
  84. cur="${COMP_WORDS[COMP_CWORD]}"
  85. prev="${COMP_WORDS[COMP_CWORD-1]}"
  86. cmd="${COMP_WORDS[1]}"
  87. cmds=$(__zfs_get_commands)
  88. if [ "${prev##*/}" = "zfs" ]
  89. then
  90. COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
  91. return 0
  92. fi
  93. case "${cmd}" in
  94. clone)
  95. __zfs_complete_ordered_arguments "$(__zfs_list_snapshots)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
  96. return 0
  97. ;;
  98. get)
  99. __zfs_complete_ordered_arguments "$(__zfs_get_properties)" "$(__zfs_list_datasets)" "$cur" "-H -r -p"
  100. return 0
  101. ;;
  102. inherit)
  103. __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_list_datasets)" $cur
  104. return 0
  105. ;;
  106. list)
  107. if [ "x$prev" = "x-o" ]
  108. then
  109. COMPREPLY=($(compgen -W "$(__zfs_get_properties)" -- "${cur##*,}"))
  110. local existing_opts=$(expr "$cur" : '\(.*,\)')
  111. if [ ! "x$existing_opts" = "x" ]
  112. then
  113. COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
  114. fi
  115. else
  116. COMPREPLY=($(compgen -W "$(__zfs_list_datasets) -H -r -o" -- "$cur"))
  117. fi
  118. return 0
  119. ;;
  120. promote)
  121. COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
  122. return 0
  123. ;;
  124. rollback|send)
  125. COMPREPLY=($(compgen -W "$(__zfs_list_snapshots)" -- "$cur"))
  126. return 0
  127. ;;
  128. snapshot)
  129. COMPREPLY=($(compgen -W "$(__zfs_list_filesystems) $(__zfs_list_volumes)" -- "$cur"))
  130. return 0
  131. ;;
  132. set)
  133. __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
  134. return 0
  135. ;;
  136. *)
  137. COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
  138. return 0
  139. ;;
  140. esac
  141. }
  142. __zpool_get_commands()
  143. {
  144. zpool 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
  145. }
  146. __zpool_get_properties()
  147. {
  148. zpool get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
  149. }
  150. __zpool_get_editable_properties()
  151. {
  152. zpool get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
  153. }
  154. __zpool_list_pools()
  155. {
  156. zpool list -H -o name
  157. }
  158. __zpool_complete()
  159. {
  160. local cur prev cmd cmds
  161. COMPREPLY=()
  162. cur="${COMP_WORDS[COMP_CWORD]}"
  163. prev="${COMP_WORDS[COMP_CWORD-1]}"
  164. cmd="${COMP_WORDS[1]}"
  165. cmds=$(__zpool_get_commands)
  166. if [ "${prev##*/}" = "zpool" ]
  167. then
  168. COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
  169. return 0
  170. fi
  171. case "${cmd}" in
  172. get)
  173. __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" $cur
  174. return 0
  175. ;;
  176. import)
  177. if [ "x$prev" = "x-d" ]
  178. then
  179. _filedir -d
  180. else
  181. COMPREPLY=($(compgen -W "$(__zpool_list_pools) -d" -- "$cur"))
  182. fi
  183. return 0
  184. ;;
  185. set)
  186. __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" $cur
  187. return 0
  188. ;;
  189. add|attach|clear|create|detach|offline|online|remove|replace)
  190. local pools="$(__zpool_list_pools)"
  191. if __zfs_argument_chosen $pools
  192. then
  193. _filedir
  194. else
  195. COMPREPLY=($(compgen -W "$pools" -- "$cur"))
  196. fi
  197. return 0
  198. ;;
  199. *)
  200. COMPREPLY=($(compgen -W "$(__zpool_list_pools)" -- "$cur"))
  201. return 0
  202. ;;
  203. esac
  204. }
  205. complete -F __zfs_complete zfs
  206. complete -o filenames -F __zpool_complete zpool