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

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