Blame SOURCES/0045-cxl-cli-add-bash-completion.patch

2eb93d
From 02c40b971bd4d092b3612fcb5e9ddd57548e6dbb Mon Sep 17 00:00:00 2001
2eb93d
From: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
Date: Thu, 7 Oct 2021 02:21:38 -0600
2eb93d
Subject: [PATCH 045/217] cxl-cli: add bash completion
2eb93d
2eb93d
Add bash completion for the cxl-cli commands implemented so far:
2eb93d
  cxl-list
2eb93d
  cxl-read-labels
2eb93d
  cxl-write-labels
2eb93d
  cxl-zero-labels
2eb93d
2eb93d
Acked-by: Dan Williams <dan.j.williams@intel.com>
2eb93d
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
---
2eb93d
 contrib/ndctl | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++
2eb93d
 1 file changed, 109 insertions(+)
2eb93d
2eb93d
diff --git a/contrib/ndctl b/contrib/ndctl
2eb93d
index 680fe6a..cae4b1b 100755
2eb93d
--- a/contrib/ndctl
2eb93d
+++ b/contrib/ndctl
2eb93d
@@ -647,5 +647,114 @@ _daxctl()
2eb93d
 	__daxctl_main
2eb93d
 }
2eb93d
 
2eb93d
+### cxl-cli ###
2eb93d
+
2eb93d
+__cxl_get_devs()
2eb93d
+{
2eb93d
+	local opts=("--memdevs" "$*")
2eb93d
+	cxl list "${opts[@]}" | grep -E "^\s*\"memdev\":" | cut -d'"' -f4
2eb93d
+}
2eb93d
+
2eb93d
+__cxlcomp()
2eb93d
+{
2eb93d
+	local i=0
2eb93d
+
2eb93d
+	COMPREPLY=( $( compgen -W "$1" -- "$2" ) )
2eb93d
+	for cword in "${COMPREPLY[@]}"; do
2eb93d
+		if [[ "$cword" == @(--memdev|--offset|--size|--input|--output) ]]; then
2eb93d
+			COMPREPLY[$i]="${cword}="
2eb93d
+		else
2eb93d
+			COMPREPLY[$i]="${cword} "
2eb93d
+		fi
2eb93d
+		((i++))
2eb93d
+	done
2eb93d
+}
2eb93d
+
2eb93d
+__cxl_comp_options()
2eb93d
+{
2eb93d
+
2eb93d
+	local cur=$1
2eb93d
+	local opts
2eb93d
+
2eb93d
+	if [[ "$cur" == *=* ]]; then
2eb93d
+		local cur_subopt=${cur%%=*}
2eb93d
+		local cur_arg=${cur##*=}
2eb93d
+		case $cur_subopt in
2eb93d
+		--memdev)
2eb93d
+			opts="$(__cxl_get_devs -i)"
2eb93d
+			;;
2eb93d
+		*)
2eb93d
+			return
2eb93d
+			;;
2eb93d
+		esac
2eb93d
+		__cxlcomp "$opts" "$cur_arg"
2eb93d
+	fi
2eb93d
+}
2eb93d
+
2eb93d
+__cxl_comp_non_option_args()
2eb93d
+{
2eb93d
+	local subcmd=$1
2eb93d
+	local cur=$2
2eb93d
+	local opts
2eb93d
+
2eb93d
+	case $subcmd in
2eb93d
+	read-labels)
2eb93d
+		;&
2eb93d
+	write-labels)
2eb93d
+		;&
2eb93d
+	zero-labels)
2eb93d
+		opts="$(__cxl_get_devs -i) all"
2eb93d
+		;;
2eb93d
+	*)
2eb93d
+		return
2eb93d
+		;;
2eb93d
+	esac
2eb93d
+	__cxlcomp "$opts" "$cur"
2eb93d
+}
2eb93d
+
2eb93d
+__cxl_main()
2eb93d
+{
2eb93d
+	local cmd subcmd
2eb93d
+
2eb93d
+	cmd=${words[0]}
2eb93d
+	COMPREPLY=()
2eb93d
+
2eb93d
+	# Skip options backward and find the last cxl command
2eb93d
+	__nd_common_prev_skip_opts
2eb93d
+	subcmd=$prev_skip_opts
2eb93d
+	# List cxl subcommands or long options
2eb93d
+	if [ -z $subcmd ]; then
2eb93d
+		if [[ $cur == --* ]]; then
2eb93d
+			cmds="--version --help --list-cmds"
2eb93d
+		else
2eb93d
+			cmds=$($cmd --list-cmds)
2eb93d
+		fi
2eb93d
+		__cxlcomp "$cmds" "$cur"
2eb93d
+	else
2eb93d
+		# List long option names
2eb93d
+		if [[ $cur == --* ]];  then
2eb93d
+			opts=$($cmd $subcmd --list-opts)
2eb93d
+			__cxlcomp "$opts" "$cur"
2eb93d
+			__cxl_comp_options "$cur"
2eb93d
+		else
2eb93d
+			[ -z "$subcmd" ] && return
2eb93d
+			__cxl_comp_non_option_args "$subcmd" "$cur"
2eb93d
+		fi
2eb93d
+	fi
2eb93d
+}
2eb93d
+
2eb93d
+type cxl &>/dev/null &&
2eb93d
+_cxl()
2eb93d
+{
2eb93d
+	local cur words cword prev
2eb93d
+	if [ $preload_get_comp_words_by_ref = "true" ]; then
2eb93d
+		_get_comp_words_by_ref -n =: cur words cword prev
2eb93d
+	else
2eb93d
+		__nd_common_get_comp_words_by_ref -n =: cur words cword prev
2eb93d
+	fi
2eb93d
+	__cxl_main
2eb93d
+}
2eb93d
+
2eb93d
 complete -o nospace -F _ndctl ndctl
2eb93d
 complete -o nospace -F _daxctl daxctl
2eb93d
+complete -o nospace -F _cxl cxl
2eb93d
-- 
2eb93d
2.27.0
2eb93d