2 # Ceph - scalable distributed file system
4 # Copyright (C) 2011 Wido den Hollander <wido@widodh.nl>
6 # This is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License version 2.1, as published by the Free Software
9 # Foundation. See file COPYING.
11 _rbd_commands="" # lazy init
12 _rbd_global_options="" # lazy init
16 if [ "x${_rbd_commands}" == "x" ]; then
17 local rbc="$(rbd bash-completion 2>/dev/null)"
18 _rbd_commands="$(echo ${rbc} | sed -e 's/|-[^|]*//g' -e 's/||*/|/g')"
19 _rbd_global_options="$(echo ${rbc} | sed -e 's/|[^-][^\|]*//g' -e 's/||*/|/g')"
24 local arg_count=${#COMP_WORDS[@]}
26 local help_request="false"
27 for (( i=1; i<arg_count; i++ )); do
28 word="${COMP_WORDS[i]}"
29 if [[ "x${word}" == "xhelp" && ${#args} == 0 ]]; then
30 # treat help request as a special case
33 elif [[ $(echo ${_rbd_global_options} | grep "|${word}|") ]]; then
36 elif [[ "x${word:0:1}" == "x-" ]]; then
37 # skip option with argument
40 elif [[ "x${word}" == "x" ]]; then
41 # skip blank arguments
48 local cur="${COMP_WORDS[COMP_CWORD]}"
49 local prev="${COMP_WORDS[COMP_CWORD-1]}"
51 local options_exp=${_rbd_global_options}
52 local command="${args[@]}"
53 local valid_command="false"
54 if [[ ${#args} != 0 && "${args[-1]}" != "${cur}" &&
55 $(echo "${_rbd_commands}" | grep -c "|${command}|") == 1 ]]; then
56 # combine global and command-specific options
57 local rbd_command_options="$(rbd bash-completion ${args[@]} 2>/dev/null)"
58 options_exp="${options_exp} ${rbd_command_options}"
62 if [[ "x${cur}" == "xhelp" ]]; then
64 elif [[ "${options_exp}}" =~ "|${prev} path|" ]]; then
65 # perform path completion for path argument
66 COMPREPLY=($(compgen -f ${cur}))
67 elif [[ "${options_exp}}" =~ "|${prev} host|" ]]; then
68 # perform host completion for host argument
69 COMPREPLY=($(compgen -A hostname ${cur}))
70 elif [[ "${help_request}" == "false" && ( "x${cur:0:1}" == "x-" ||
71 ( "x${cur}" == "x" && "${valid_command}" == "true" ) ) ]]; then
72 # all valid options for current command
73 options="$(echo ${options_exp} | sed -e 's/||*/ /g' -r -e 's/ (arg|path|host)//g')"
74 COMPREPLY=($(compgen -W "${options}" -- ${cur}))
75 elif [[ "${valid_command}" == "false" ]]; then
76 # search for valid command
77 [[ "x${command}" != "x" && "x${cur}" == "x" ]] && command="${command} "
78 COMPREPLY=($(echo ${_rbd_commands} | grep -o "|${command}[^ |]*" | \
79 uniq | sed -e 's/|//g' | awk -F' ' '{print $(NF)}'))