Your IP : 13.58.213.43


Current Path : /etc/bash_completion.d/
Upload File :
Current File : //etc/bash_completion.d/mgrctl_completion

_has_funcname() {
	local prev_arg=""
	for arg_ind in "${!COMP_WORDS[@]}"; do
		#Skip command itself
		if [ "${arg_ind}" == "0" ] ; then
			continue
		fi
		
		#Skip keys
		if [[ "${COMP_WORDS[${arg_ind}]}" == -* ]] ; then
			continue
		fi
		
		#Skip values for keys
		prev_arg="${COMP_WORDS[${arg_ind} - 1]}"
		if [[ ("${prev_arg}" == "-m") || ("${prev_arg}" == "--manager") || ("${prev_arg}" == "-o") || ("${prev_arg}" == "--format") ]]; then
			continue
		fi
		
		#if we just autocompliting last and current parameter - its the name of function itself
		if [ "${arg_ind}" == "${COMP_CWORD}" ] ; then
			return 0
		fi
		
		#only here we autocomplete something else
		return 1
	done
	
	return 0
}

_mgrctl() {
	#Just skip mgrctl call errors
	if [[ $EUID -ne 0 ]]; then
		COMPREPLY=()
		return 0
	fi

	local BASE_PATH='/usr/local/mgr5'
	local cur_word="${COMP_WORDS[COMP_CWORD]}"
	local prev_word="${COMP_WORDS[COMP_CWORD-1]}"

	local key_list="--manager --format --restart --lock --unlock --info --noformatted"
	local short_key_list="-m -o -R -l -u -i -n"
	local format_list="json devel html text xml"
	local odd_list="elid= plid= name= sok=ok"
	
	local cur_mgr=""
	local mgr_arr=()

	local mgrlist_conf="${BASE_PATH}/etc/mgrlist.conf"
	local manager_list=""
	if [ -f ${mgrlist_conf} ]; then
		#Better and faster not to have core in manager list. Who really needs this?
		#manager_list='core'$'\n'
		manager_list=$(cut -s -d' ' -f2 ${mgrlist_conf})
	else
		manager_list="core"
	fi

	#1. Fill values for concrete flags
	case "${prev_word}" in
	-m|--manager)
		# shellcheck disable=SC2086
		COMPREPLY=( $(compgen -W "${manager_list}" -- ${cur_word}) )
		return 0
		;;
	-o|--format)
		# shellcheck disable=SC2086
		COMPREPLY=( $(compgen -W "${format_list}" -- ${cur_word}) )
		return 0
		;;
	esac

	#2. Fill values for completing '-' or '--' flags
	if [[ ${cur_word} == --* ]] ; then
		# shellcheck disable=SC2086
		COMPREPLY=( $(compgen -W "${key_list}" -- ${cur_word}) )
		return 0
	elif [[ ${cur_word} == -* ]] ; then
		# shellcheck disable=SC2086
		COMPREPLY=( $(compgen -W "${short_key_list}" -- ${cur_word}) )
		return 0
	fi
	
	#3. If there is name of function, we just can autocomplete additional parameters
	_has_funcname
	if [ $? == 1 ] ; then
		# shellcheck disable=SC2086
		COMPREPLY=( $(compgen -W "${odd_list}" -- ${cur_word}) )
		compopt -o nospace
		return 0
	fi

	#4. Fill actionlist
	for mgr_ind in "${!COMP_WORDS[@]}"; do
		if [[ ("${COMP_WORDS[${mgr_ind}]}" == "-m") || ("${COMP_WORDS[${mgr_ind}]}" == "--manager") ]]; then
			cur_mgr="${COMP_WORDS[${mgr_ind}+1]}"
			break
		fi
	done

	if [ -n "${cur_mgr}" ] ; then
		mapfile -t mgr_arr <<< "${manager_list}"

		for mgr_ind in "${!mgr_arr[@]}"; do
			if [[ "${mgr_arr[${mgr_ind}]}" == "${cur_mgr}" ]]; then
				local mgrctl_result
				mgrctl_result=$(${BASE_PATH}/sbin/mgrctl -m "${cur_mgr}" actionlist 2>/dev/null)
				# shellcheck disable=SC2181
				if [ $? == 0 ] ; then
					mgrctl_result=$(cut -s -d'=' -f2 <<< "${mgrctl_result}")
					# shellcheck disable=SC2086
					COMPREPLY=( $(compgen -W "${mgrctl_result}" -- ${cur_word}) )
					return 0
				else
					COMPREPLY=()
					return 0
				fi
			fi
		done
	fi

	#5. Fill common case
	if [ "${COMP_CWORD}" == "1" ] ; then
		COMPREPLY=('-m')
	else
		COMPREPLY=()
	fi

	return 0
}

complete -F _mgrctl /usr/local/mgr5/sbin/mgrctl sbin/mgrctl mgrctl