summaryrefslogtreecommitdiffstats
path: root/src/bash_completion/rbd
blob: bef1670e9a28b815f829af95fc034b8dc646dfa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# Ceph - scalable distributed file system
#
# Copyright (C) 2011 Wido den Hollander <wido@widodh.nl>
#
# This is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1, as published by the Free Software
# Foundation.  See file COPYING.
#
_rbd_commands="" # lazy init
_rbd_global_options="" # lazy init

_rbd()
{
	if [ "x${_rbd_commands}" == "x" ]; then
		local rbc="$(rbd bash-completion 2>/dev/null)"
		_rbd_commands="$(echo ${rbc} | sed -e 's/|-[^|]*//g' -e 's/||*/|/g')"
		_rbd_global_options="$(echo ${rbc} | sed -e 's/|[^-][^\|]*//g' -e 's/||*/|/g')"
	fi

        COMPREPLY=()

	local arg_count=${#COMP_WORDS[@]}
	local args=()
	local help_request="false"
	for (( i=1; i<arg_count; i++ )); do
		word="${COMP_WORDS[i]}"
		if [[ "x${word}" == "xhelp" && ${#args} == 0 ]]; then
			# treat help request as a special case
			help_request="true"
			continue
		elif [[ $(echo ${_rbd_global_options} | grep "|${word}|") ]]; then
			# skip flag option
			continue
		elif [[ "x${word:0:1}" == "x-" ]]; then
			# skip option with argument
			let i=$i+1
			continue
		elif [[ "x${word}" == "x" ]]; then
			# skip blank arguments
			continue
		fi

		args+=("${word}")
	done

        local cur="${COMP_WORDS[COMP_CWORD]}"
        local prev="${COMP_WORDS[COMP_CWORD-1]}"

	local options_exp=${_rbd_global_options}
	local command="${args[@]}"
	local valid_command="false"
	if [[ ${#args} != 0 && "${args[-1]}" != "${cur}" &&
	      $(echo "${_rbd_commands}" | grep -c "|${command}|") == 1 ]]; then
		# combine global and command-specific options
                local rbd_command_options="$(rbd bash-completion ${args[@]} 2>/dev/null)"
                options_exp="${options_exp} ${rbd_command_options}"
		valid_command="true"
	fi

	if [[ "x${cur}" == "xhelp" ]]; then
		COMPREPLY=()
	elif [[ "${options_exp}}" =~ "|${prev} path|" ]];  then
		# perform path completion for path argument
		COMPREPLY=($(compgen -f ${cur}))
	elif [[ "${options_exp}}" =~ "|${prev} host|" ]];  then
		# perform host completion for host argument
		COMPREPLY=($(compgen -A hostname ${cur}))
	elif [[ "${help_request}" == "false" && ( "x${cur:0:1}" == "x-" ||
		( "x${cur}" == "x" && "${valid_command}" == "true" ) ) ]]; then
		# all valid options for current command
		options="$(echo ${options_exp} | sed -e 's/||*/ /g' -r -e 's/ (arg|path|host)//g')"
		COMPREPLY=($(compgen -W "${options}" -- ${cur}))
	elif [[ "${valid_command}" == "false" ]]; then
		# search for valid command
		[[ "x${command}" != "x" && "x${cur}" == "x" ]] && command="${command} "
		COMPREPLY=($(echo ${_rbd_commands} | grep -o "|${command}[^ |]*" | \
			uniq | sed -e 's/|//g' | awk -F' ' '{print $(NF)}'))
	fi
}

complete -F _rbd rbd