Merge pull request #717 from albers/completion-images-2

Improve and fix bash completion for images
This commit is contained in:
Sebastiaan van Stijn 2018-01-27 17:48:49 -08:00 committed by GitHub
commit 4586609f71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 80 deletions

View File

@ -178,52 +178,78 @@ __docker_complete_container_ids() {
COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") ) COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") )
} }
# __docker_images returns a list of images. For each image, up to three representations
# can be generated: the repository (e.g. busybox), repository:tag (e.g. busybox:latest)
# and the ID (e.g. sha256:ee22cbbd4ea3dff63c86ba60c7691287c321e93adfc1009604eb1dde7ec88645).
#
# The optional arguments `--repo`, `--tag` and `--id` select the representations that
# may be returned. Whether or not a particular representation is actually returned
# depends on the user's customization through several environment variables:
# - image IDs are only shown if DOCKER_COMPLETION_SHOW_IMAGE_IDS=all|non-intermediate.
# - tags can be excluded by setting DOCKER_COMPLETION_SHOW_TAGS=no.
# - repositories are always shown.
#
# In cases where an exact image specification is needed, `--force-tag` can be used.
# It ignores DOCKER_COMPLETION_SHOW_TAGS and only lists valid repository:tag combinations,
# avoiding repository names that would default to a potentially missing default tag.
#
# Additional arguments to `docker image ls` may be specified in order to filter the list,
# e.g. `__docker_images --filter dangling=true`.
#
__docker_images() { __docker_images() {
local images_args="" local repo_format='{{.Repository}}'
local tag_format='{{.Repository}}:{{.Tag}}'
local id_format='{{.ID}}'
local all
local format
case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in if [ "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" = "all" ] ; then
all) all='--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 fi
local awk_script while true ; do
case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in case "$1" in
all|non-intermediate) --repo)
awk_script='NR>1 { print $3; if ($1 != "<none>") { '"$repo_print_command"' } }' format+="$repo_format\n"
;; shift
none|*) ;;
awk_script='NR>1 && $1 != "<none>" { '"$repo_print_command"' }' --tag)
;; if [ "${DOCKER_COMPLETION_SHOW_TAGS:-yes}" = "yes" ]; then
esac format+="$tag_format\n"
fi
shift
;;
--id)
if [[ $DOCKER_COMPLETION_SHOW_IMAGE_IDS =~ ^(all|non-intermediate)$ ]] ; then
format+="$id_format\n"
fi
shift
;;
--force-tag)
# like `--tag` but ignores environment setting
format+="$tag_format\n"
shift
;;
*)
break
;;
esac
done
__docker_q images $images_args | awk "$awk_script" | grep -v '<none>$' __docker_q image ls --no-trunc --format "${format%\\n}" $all "$@" | grep -v '<none>$'
} }
# __docker_complete_images applies completion of images based on the current value of `$cur` or
# the value of the optional first option `--cur`, if given.
# See __docker_images for customization of the returned items.
__docker_complete_images() { __docker_complete_images() {
COMPREPLY=( $(compgen -W "$(__docker_images)" -- "$cur") ) local current="$cur"
__ltrim_colon_completions "$cur" if [ "$1" = "--cur" ] ; then
} current="$2"
shift 2
__docker_complete_image_repos() { fi
local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')" COMPREPLY=( $(compgen -W "$(__docker_images "$@")" -- "$current") )
COMPREPLY=( $(compgen -W "$repos" -- "$cur") ) __ltrim_colon_completions "$current"
}
__docker_complete_image_repos_and_tags() {
local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
__ltrim_colon_completions "$cur"
} }
# __docker_networks returns a list of all networks. Additional options to # __docker_networks returns a list of all networks. Additional options to
@ -1354,11 +1380,8 @@ _docker_container_commit() {
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_containers_all __docker_complete_containers_all
return return
fi elif [ "$cword" -eq "$((counter + 1))" ]; then
(( counter++ )) __docker_complete_images --repo --tag
if [ "$cword" -eq "$counter" ]; then
__docker_complete_image_repos_and_tags
return return
fi fi
;; ;;
@ -1529,8 +1552,7 @@ _docker_container_ls() {
local key=$(__docker_map_key_of_current_option '--filter|-f') local key=$(__docker_map_key_of_current_option '--filter|-f')
case "$key" in case "$key" in
ancestor) ancestor)
cur="${cur##*=}" __docker_complete_images --cur "${cur##*=}" --repo --tag --id
__docker_complete_images
return return
;; ;;
before) before)
@ -1998,7 +2020,7 @@ _docker_container_run_and_create() {
*) *)
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_complete_images __docker_complete_images --repo --tag --id
fi fi
;; ;;
esac esac
@ -2513,7 +2535,7 @@ _docker_image_build() {
return return
;; ;;
--cache-from) --cache-from)
__docker_complete_image_repos_and_tags __docker_complete_images --repo --tag --id
return return
;; ;;
--file|-f|--iidfile) --file|-f|--iidfile)
@ -2541,7 +2563,7 @@ _docker_image_build() {
return return
;; ;;
--tag|-t) --tag|-t)
__docker_complete_image_repos_and_tags __docker_complete_images --repo --tag
return return
;; ;;
--target) --target)
@ -2587,9 +2609,9 @@ _docker_image_history() {
COMPREPLY=( $( compgen -W "--format --help --human=false -H=false --no-trunc --quiet -q" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--format --help --human=false -H=false --no-trunc --quiet -q" -- "$cur" ) )
;; ;;
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag '--format')
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_images __docker_complete_images --force-tag --id
fi fi
;; ;;
esac esac
@ -2613,12 +2635,10 @@ _docker_image_import() {
*) *)
local counter=$(__docker_pos_first_nonflag '--change|-c|--message|-m') local counter=$(__docker_pos_first_nonflag '--change|-c|--message|-m')
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
_filedir
return return
fi elif [ "$cword" -eq "$((counter + 1))" ]; then
(( counter++ )) __docker_complete_images --repo --tag
if [ "$cword" -eq "$counter" ]; then
__docker_complete_image_repos_and_tags
return return
fi fi
;; ;;
@ -2651,9 +2671,8 @@ _docker_image_list() {
_docker_image_ls() { _docker_image_ls() {
local key=$(__docker_map_key_of_current_option '--filter|-f') local key=$(__docker_map_key_of_current_option '--filter|-f')
case "$key" in case "$key" in
before|since|reference) before|since)
cur="${cur##*=}" __docker_complete_images --cur "${cur##*=}" --force-tag --id
__docker_complete_images
return return
;; ;;
dangling) dangling)
@ -2663,6 +2682,10 @@ _docker_image_ls() {
label) label)
return return
;; ;;
reference)
__docker_complete_images --cur "${cur##*=}" --repo --tag
return
;;
esac esac
case "$prev" in case "$prev" in
@ -2684,7 +2707,7 @@ _docker_image_ls() {
return return
;; ;;
*) *)
__docker_complete_image_repos __docker_complete_images --repo --tag
;; ;;
esac esac
} }
@ -2725,12 +2748,12 @@ _docker_image_pull() {
for arg in "${COMP_WORDS[@]}"; do for arg in "${COMP_WORDS[@]}"; do
case "$arg" in case "$arg" in
--all-tags|-a) --all-tags|-a)
__docker_complete_image_repos __docker_complete_images --repo
return return
;; ;;
esac esac
done done
__docker_complete_image_repos_and_tags __docker_complete_images --repo --tag
fi fi
;; ;;
esac esac
@ -2744,7 +2767,7 @@ _docker_image_push() {
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_image_repos_and_tags __docker_complete_images --repo --tag
fi fi
;; ;;
esac esac
@ -2760,7 +2783,7 @@ _docker_image_rm() {
COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
;; ;;
*) *)
__docker_complete_images __docker_complete_images --force-tag --id
;; ;;
esac esac
} }
@ -2782,7 +2805,7 @@ _docker_image_save() {
COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
;; ;;
*) *)
__docker_complete_images __docker_complete_images --repo --tag --id
;; ;;
esac esac
} }
@ -2796,13 +2819,10 @@ _docker_image_tag() {
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_image_repos_and_tags __docker_complete_images --force-tag --id
return return
fi elif [ "$cword" -eq "$((counter + 1))" ]; then
(( counter++ )) __docker_complete_images --repo --tag
if [ "$cword" -eq "$counter" ]; then
__docker_complete_image_repos_and_tags
return return
fi fi
;; ;;
@ -2858,7 +2878,7 @@ _docker_inspect() {
'') '')
COMPREPLY=( $( compgen -W " COMPREPLY=( $( compgen -W "
$(__docker_containers --all) $(__docker_containers --all)
$(__docker_images) $(__docker_images --force-tag --id)
$(__docker_networks) $(__docker_networks)
$(__docker_nodes) $(__docker_nodes)
$(__docker_plugins_installed) $(__docker_plugins_installed)
@ -2872,7 +2892,7 @@ _docker_inspect() {
__docker_complete_containers_all __docker_complete_containers_all
;; ;;
image) image)
__docker_complete_images __docker_complete_images --force-tag --id
;; ;;
network) network)
__docker_complete_networks __docker_complete_networks
@ -3495,7 +3515,7 @@ _docker_service_update_and_create() {
esac esac
;; ;;
--image) --image)
__docker_complete_image_repos_and_tags __docker_complete_images --repo --tag --id
return return
;; ;;
--network-add|--network-rm) --network-add|--network-rm)
@ -3591,7 +3611,7 @@ _docker_service_update_and_create() {
fi fi
else else
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_images __docker_complete_images --repo --tag --id
fi fi
fi fi
;; ;;
@ -4639,8 +4659,7 @@ _docker_system_events() {
return return
;; ;;
image) image)
cur="${cur##*=}" __docker_complete_images --cur "${cur##*=}" --repo --tag
__docker_complete_images
return return
;; ;;
network) network)
@ -4737,7 +4756,7 @@ _docker_trust_revoke() {
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_images __docker_complete_images --repo --tag
fi fi
;; ;;
esac esac
@ -4751,7 +4770,7 @@ _docker_trust_sign() {
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_images __docker_complete_images --force-tag --id
fi fi
;; ;;
esac esac
@ -4765,7 +4784,7 @@ _docker_trust_view() {
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then if [ "$cword" -eq "$counter" ]; then
__docker_complete_images __docker_complete_images --repo --tag
fi fi
;; ;;
esac esac

View File

@ -179,7 +179,7 @@ The currently supported filters are:
* container (`container=<name or id>`) * container (`container=<name or id>`)
* daemon (`daemon=<name or id>`) * daemon (`daemon=<name or id>`)
* event (`event=<event action>`) * event (`event=<event action>`)
* image (`image=<tag or id>`) * image (`image=<repository or tag>`)
* label (`label=<key>` or `label=<key>=<value>`) * label (`label=<key>` or `label=<key>=<value>`)
* network (`network=<name or id>`) * network (`network=<name or id>`)
* node (`node=<id>`) * node (`node=<id>`)