Bladeren bron

Merge pull request #18828 from albers/completion-plugins

Use computed plugin lists in bash completion
David Calavera 9 jaren geleden
bovenliggende
commit
88c4d4a943
1 gewijzigde bestanden met toevoegingen van 24 en 11 verwijderingen
  1. 24 11
      contrib/completion/bash/docker

+ 24 - 11
contrib/completion/bash/docker

@@ -153,8 +153,11 @@ __docker_networks() {
 	# Set DOCKER_COMPLETION_SHOW_NETWORK_IDS=yes to also complete network IDs.
 	# Set DOCKER_COMPLETION_SHOW_NETWORK_IDS=yes to also complete network IDs.
 	local fields='$2'
 	local fields='$2'
 	[ "${DOCKER_COMPLETION_SHOW_NETWORK_IDS}" = yes ] && fields='$1,$2'
 	[ "${DOCKER_COMPLETION_SHOW_NETWORK_IDS}" = yes ] && fields='$1,$2'
-	local networks=$(__docker_q network ls --no-trunc | awk "NR>1 {print $fields}")
-	COMPREPLY=( $(compgen -W "$networks" -- "$cur") )
+	__docker_q network ls --no-trunc | awk "NR>1 {print $fields}"
+}
+
+__docker_complete_networks() {
+	COMPREPLY=( $(compgen -W "$(__docker_complete_networks)" -- "$cur") )
 }
 }
 
 
 __docker_containers_in_network() {
 __docker_containers_in_network() {
@@ -166,6 +169,14 @@ __docker_volumes() {
 	COMPREPLY=( $(compgen -W "$(__docker_q volume ls -q)" -- "$cur") )
 	COMPREPLY=( $(compgen -W "$(__docker_q volume ls -q)" -- "$cur") )
 }
 }
 
 
+__docker_plugins() {
+	__docker_q info | sed -n "/^Plugins/,/^[^ ]/s/ $1: //p"
+}
+
+__docker_complete_plugins() {
+	COMPREPLY=( $(compgen -W "$(__docker_plugins $1)" -- "$cur") )
+}
+
 # Finds the position of the first word that is neither option nor an option's argument.
 # Finds the position of the first word that is neither option nor an option's argument.
 # If there are options that require arguments, you should pass a glob describing those
 # If there are options that require arguments, you should pass a glob describing those
 # options, e.g. "--option1|-o|--option2"
 # options, e.g. "--option1|-o|--option2"
@@ -1103,7 +1114,7 @@ _docker_network_connect() {
 		*)
 		*)
 			local counter=$(__docker_pos_first_nonflag)
 			local counter=$(__docker_pos_first_nonflag)
 			if [ $cword -eq $counter ]; then
 			if [ $cword -eq $counter ]; then
-				__docker_networks
+				__docker_complete_networks
 			elif [ $cword -eq $(($counter + 1)) ]; then
 			elif [ $cword -eq $(($counter + 1)) ]; then
 				__docker_containers_running
 				__docker_containers_running
 			fi
 			fi
@@ -1121,9 +1132,11 @@ _docker_network_create() {
 			return
 			return
 			;;
 			;;
 		--driver|-d)
 		--driver|-d)
-			# no need to suggest drivers that allow one instance only
-			# (host, null)
-			COMPREPLY=( $( compgen -W "bridge overlay" -- "$cur" ) )
+			local plugins=" $(__docker_plugins Network) "
+			# remove drivers that allow one instance only
+			plugins=${plugins/ host / }
+			plugins=${plugins/ null / }
+			COMPREPLY=( $(compgen -W "$plugins" -- "$cur") )
 			return
 			return
 			;;
 			;;
 	esac
 	esac
@@ -1143,7 +1156,7 @@ _docker_network_disconnect() {
 		*)
 		*)
 			local counter=$(__docker_pos_first_nonflag)
 			local counter=$(__docker_pos_first_nonflag)
 			if [ $cword -eq $counter ]; then
 			if [ $cword -eq $counter ]; then
-				__docker_networks
+				__docker_complete_networks
 			elif [ $cword -eq $(($counter + 1)) ]; then
 			elif [ $cword -eq $(($counter + 1)) ]; then
 				__docker_containers_in_network "$prev"
 				__docker_containers_in_network "$prev"
 			fi
 			fi
@@ -1163,7 +1176,7 @@ _docker_network_inspect() {
 			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
 			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
 			;;
 			;;
 		*)
 		*)
-			__docker_networks
+			__docker_complete_networks
 	esac
 	esac
 }
 }
 
 
@@ -1535,7 +1548,7 @@ _docker_run() {
 					__docker_containers_all
 					__docker_containers_all
 					;;
 					;;
 				*)
 				*)
-					COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
+					COMPREPLY=( $( compgen -W "$(__docker_plugins Network) $(__docker_networks) container:" -- "$cur") )
 					if [ "${COMPREPLY[*]}" = "container:" ] ; then
 					if [ "${COMPREPLY[*]}" = "container:" ] ; then
 						__docker_nospace
 						__docker_nospace
 					fi
 					fi
@@ -1572,7 +1585,7 @@ _docker_run() {
 			return
 			return
 			;;
 			;;
 		--volume-driver)
 		--volume-driver)
-			COMPREPLY=( $( compgen -W "local" -- "$cur" ) )
+			__docker_complete_plugins Volume
 			return
 			return
 			;;
 			;;
 		--volumes-from)
 		--volumes-from)
@@ -1731,7 +1744,7 @@ _docker_version() {
 _docker_volume_create() {
 _docker_volume_create() {
 	case "$prev" in
 	case "$prev" in
 		--driver|-d)
 		--driver|-d)
-			COMPREPLY=( $( compgen -W "local" -- "$cur" ) )
+			__docker_complete_plugins Volume
 			return
 			return
 			;;
 			;;
 		--name|--opt|-o)
 		--name|--opt|-o)