Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / bash_completion / rbd
1 #
2 # Ceph - scalable distributed file system
3 #
4 # Copyright (C) 2011 Wido den Hollander <wido@widodh.nl>
5 #
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.
10 #
11 _rbd_commands="" # lazy init
12 _rbd_global_options="" # lazy init
13
14 _rbd()
15 {
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')"
20         fi
21
22         COMPREPLY=()
23
24         local arg_count=${#COMP_WORDS[@]}
25         local args=()
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
31                         help_request="true"
32                         continue
33                 elif [[ $(echo ${_rbd_global_options} | grep "|${word}|") ]]; then
34                         # skip flag option
35                         continue
36                 elif [[ "x${word:0:1}" == "x-" ]]; then
37                         # skip option with argument
38                         let i=$i+1
39                         continue
40                 elif [[ "x${word}" == "x" ]]; then
41                         # skip blank arguments
42                         continue
43                 fi
44
45                 args+=("${word}")
46         done
47
48         local cur="${COMP_WORDS[COMP_CWORD]}"
49         local prev="${COMP_WORDS[COMP_CWORD-1]}"
50
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}"
59                 valid_command="true"
60         fi
61
62         if [[ "x${cur}" == "xhelp" ]]; then
63                 COMPREPLY=()
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)}'))
80         fi
81 }
82
83 complete -F _rbd rbd