Improve bash completion of containers

Signed-off-by: Harald Albers <github@albersweb.de>
master
Harald Albers 2016-10-13 04:09:53 -07:00 committed by Tibor Vass
parent f1c1bbcbea
commit 9ef7847f74
1 changed files with 49 additions and 31 deletions

View File

@ -20,6 +20,7 @@
# For several commands, the amount of completions can be configured by # For several commands, the amount of completions can be configured by
# setting environment variables. # setting environment variables.
# #
# DOCKER_COMPLETION_SHOW_CONTAINER_IDS
# DOCKER_COMPLETION_SHOW_NETWORK_IDS # DOCKER_COMPLETION_SHOW_NETWORK_IDS
# DOCKER_COMPLETION_SHOW_NODE_IDS # DOCKER_COMPLETION_SHOW_NODE_IDS
# DOCKER_COMPLETION_SHOW_SERVICE_IDS # DOCKER_COMPLETION_SHOW_SERVICE_IDS
@ -58,32 +59,56 @@ __docker_q() {
docker ${host:+-H "$host"} ${config:+--config "$config"} 2>/dev/null "$@" docker ${host:+-H "$host"} ${config:+--config "$config"} 2>/dev/null "$@"
} }
__docker_complete_containers_all() { # Returns a list of containers. Additional arguments to `docker ps`
local IFS=$'\n' # may be specified in order to filter the list, e.g.
local containers=( $(__docker_q ps -aq --no-trunc) ) # `__docker_containers --filter status=running`
if [ "$1" ]; then # By default, only names are returned.
containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") ) # Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs.
# An optional first option `--id|--name` may be used to limit the
# output to the IDs or names of matching items. This setting takes
# precedence over the environment setting.
__docker_containers() {
local format
if [ "$1" = "--id" ] ; then
format='{{.ID}}'
shift
elif [ "$1" = "--name" ] ; then
format='{{.Names}}'
shift
elif [ "${DOCKER_COMPLETION_SHOW_CONTAINER_IDS}" = yes ] ; then
format='{{.ID}} {{.Names}}'
else
format='{{.Names}}'
fi fi
local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") ) __docker_q ps --format "$format" "$@"
names=( "${names[@]#/}" ) # trim off the leading "/" from the container names }
unset IFS
COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") ) # Applies completion of containers based on the current value of `$cur` or
# the value of the optional first option `--cur`, if given.
# Additional filters may be appended, see `__docker_containers`.
__docker_complete_containers() {
local current="$cur"
if [ "$1" = "--cur" ] ; then
current="$2"
shift 2
fi
COMPREPLY=( $(compgen -W "$(__docker_containers "$@")" -- "$current") )
}
__docker_complete_containers_all() {
__docker_complete_containers "$@" --all
} }
__docker_complete_containers_running() { __docker_complete_containers_running() {
__docker_complete_containers_all '.State.Running' __docker_complete_containers "$@" --filter status=running
} }
__docker_complete_containers_stopped() { __docker_complete_containers_stopped() {
__docker_complete_containers_all 'not .State.Running' __docker_complete_containers "$@" --filter status=exited
}
__docker_complete_containers_pauseable() {
__docker_complete_containers_all 'and .State.Running (not .State.Paused)'
} }
__docker_complete_containers_unpauseable() { __docker_complete_containers_unpauseable() {
__docker_complete_containers_all '.State.Paused' __docker_complete_containers "$@" --filter status=paused
} }
__docker_complete_container_names() { __docker_complete_container_names() {
@ -1114,8 +1139,7 @@ _docker_events() {
local key=$(__docker_map_key_of_current_option '-f|--filter') local key=$(__docker_map_key_of_current_option '-f|--filter')
case "$key" in case "$key" in
container) container)
cur="${cur##*=}" __docker_complete_containers_all --cur "${cur##*=}"
__docker_complete_containers_all
return return
;; ;;
daemon) daemon)
@ -2172,7 +2196,7 @@ _docker_pause() {
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_complete_containers_pauseable __docker_complete_containers_running
fi fi
;; ;;
esac esac
@ -2201,13 +2225,11 @@ _docker_ps() {
return return
;; ;;
before) before)
cur="${cur##*=}" __docker_complete_containers_all --cur "${cur##*=}"
__docker_complete_containers_all
return return
;; ;;
id) id)
cur="${cur##*=}" __docker_complete_containers_all --cur "${cur##*=}" --id
__docker_complete_container_ids
return return
;; ;;
is-task) is-task)
@ -2215,8 +2237,7 @@ _docker_ps() {
return return
;; ;;
name) name)
cur="${cur##*=}" __docker_complete_containers_all --cur "${cur##*=}" --name
__docker_complete_container_names
return return
;; ;;
network) network)
@ -2224,8 +2245,7 @@ _docker_ps() {
return return
;; ;;
since) since)
cur="${cur##*=}" __docker_complete_containers_all --cur "${cur##*=}"
__docker_complete_containers_all
return return
;; ;;
status) status)
@ -2560,8 +2580,7 @@ _docker_run() {
--network) --network)
case "$cur" in case "$cur" in
container:*) container:*)
local cur=${cur#*:} __docker_complete_containers_all --cur "${cur#*:}"
__docker_complete_containers_all
;; ;;
*) *)
COMPREPLY=( $( compgen -W "$(__docker_plugins Network) $(__docker_networks) container:" -- "$cur") ) COMPREPLY=( $( compgen -W "$(__docker_plugins Network) $(__docker_networks) container:" -- "$cur") )
@ -2575,8 +2594,7 @@ _docker_run() {
--pid) --pid)
case "$cur" in case "$cur" in
*:*) *:*)
cur="${cur#*:}" __docker_complete_containers_running --cur "${cur#*:}"
__docker_complete_containers_running
;; ;;
*) *)
COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) ) COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )