Browse Source

Make run and rmi bash completions configurable

Allow the user to configure how Docker's bash completion works for the
"events", "history", "inspect", "run", "rmi" and "save" commands through the
following environment variables:

DOCKER_COMPLETION_SHOW_IMAGE_IDS
  "none" - Show names only (default)
  "non-intermediate" - Show names and ids, but omit intermediate image IDs
  "all" - Show names and ids, including intermediate image IDs

DOCKER_COMPLETION_SHOW_TAGS
  "yes" - include tags in completion options (default)
  "no"  - don't include tags in completion options

Fixes #9474.

Signed-off-by: Rory Hunter <roryhunter2@gmail.com>
Rory Hunter 10 years ago
parent
commit
60d97a4a63
1 changed files with 57 additions and 14 deletions
  1. 57 14
      contrib/completion/bash/docker

+ 57 - 14
contrib/completion/bash/docker

@@ -14,6 +14,22 @@
 #  - copy this file to e.g. ~/.docker-completion.sh and add the line
 #  - copy this file to e.g. ~/.docker-completion.sh and add the line
 #    below to your .bashrc after bash completion features are loaded
 #    below to your .bashrc after bash completion features are loaded
 #    . ~/.docker-completion.sh
 #    . ~/.docker-completion.sh
+#
+# Configuration:
+#
+# You can tailor completion for the "events", "history", "inspect", "run",
+# "rmi" and "save" commands by settings the following environment
+# variables:
+#   
+# DOCKER_COMPLETION_SHOW_IMAGE_IDS
+#   "none" - Show names only (default)
+#   "non-intermediate" - Show names and ids, but omit intermediate image IDs
+#   "all" - Show names and ids, including intermediate image IDs
+# 
+# DOCKER_COMPLETION_SHOW_TAGS
+#   "yes" - include tags in completion options (default)
+#   "no"  - don't include tags in completion options
+
 #
 #
 # Note:
 # Note:
 # Currently, the completions will not work if the docker daemon is not
 # Currently, the completions will not work if the docker daemon is not
@@ -70,6 +86,40 @@ __docker_container_ids() {
 	COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") )
 	COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") )
 }
 }
 
 
+__docker_images() {
+	local images_args=""
+
+	case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in
+		all)
+			images_args="--no-trunc -a"
+			;;
+		non-intermediate)
+			images_args="--no-trunc"
+			;;
+	esac
+
+	local repo_print_command
+	if [ "${DOCKER_COMPLETION_SHOW_TAGS:-yes}" = "yes" ]; then
+		repo_print_command='print $1; print $1":"$2'
+	else
+		repo_print_command='print $1'
+	fi
+
+	local awk_script
+	case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in
+		all|non-intermediate)
+			awk_script='NR>1 { print $3; if ($1 != "<none>") { '"$repo_print_command"' } }'
+			;;
+		none|*)
+			awk_script='NR>1 && $1 != "<none>" { '"$repo_print_command"' }'
+			;;
+	esac
+
+	local images=$(__docker_q images $images_args | awk "$awk_script")
+	COMPREPLY=( $(compgen -W "$images" -- "$cur") )
+	__ltrim_colon_completions "$cur"
+}
+
 __docker_image_repos() {
 __docker_image_repos() {
 	local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
 	local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
 	COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
 	COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
@@ -81,16 +131,10 @@ __docker_image_repos_and_tags() {
 	__ltrim_colon_completions "$cur"
 	__ltrim_colon_completions "$cur"
 }
 }
 
 
-__docker_image_repos_and_tags_and_ids() {
-	local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')"
-	COMPREPLY=( $(compgen -W "$images" -- "$cur") )
-	__ltrim_colon_completions "$cur"
-}
-
 __docker_containers_and_images() {
 __docker_containers_and_images() {
 	__docker_containers_all
 	__docker_containers_all
 	local containers=( "${COMPREPLY[@]}" )
 	local containers=( "${COMPREPLY[@]}" )
-	__docker_image_repos_and_tags_and_ids
+	__docker_images
 	COMPREPLY+=( "${containers[@]}" )
 	COMPREPLY+=( "${containers[@]}" )
 }
 }
 
 
@@ -599,7 +643,7 @@ _docker_events() {
 			;;
 			;;
 		*image=*)
 		*image=*)
 			cur="${cur#=}"
 			cur="${cur#=}"
-			__docker_image_repos_and_tags_and_ids
+			__docker_images
 			return
 			return
 			;;
 			;;
 	esac
 	esac
@@ -657,7 +701,7 @@ _docker_history() {
 		*)
 		*)
 			local counter=$(__docker_pos_first_nonflag)
 			local counter=$(__docker_pos_first_nonflag)
 			if [ $cword -eq $counter ]; then
 			if [ $cword -eq $counter ]; then
-				__docker_image_repos_and_tags_and_ids
+				__docker_images
 			fi
 			fi
 			;;
 			;;
 	esac
 	esac
@@ -750,7 +794,7 @@ _docker_inspect() {
 					__docker_containers_all
 					__docker_containers_all
 					;;
 					;;
 				image)
 				image)
-					__docker_image_repos_and_tags_and_ids
+					__docker_images
 					;;
 					;;
 			esac
 			esac
 	esac
 	esac
@@ -990,7 +1034,7 @@ _docker_rmi() {
 			COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
 			COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
 			;;
 			;;
 		*)
 		*)
-			__docker_image_repos_and_tags_and_ids
+			__docker_images
 			;;
 			;;
 	esac
 	esac
 }
 }
@@ -1195,9 +1239,8 @@ _docker_run() {
 			;;
 			;;
 		*)
 		*)
 			local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
 			local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
-
 			if [ $cword -eq $counter ]; then
 			if [ $cword -eq $counter ]; then
-				__docker_image_repos_and_tags_and_ids
+				__docker_images
 			fi
 			fi
 			;;
 			;;
 	esac
 	esac
@@ -1216,7 +1259,7 @@ _docker_save() {
 			COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
 			COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
 			;;
 			;;
 		*)
 		*)
-			__docker_image_repos_and_tags_and_ids
+			__docker_images
 			;;
 			;;
 	esac
 	esac
 }
 }