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

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