From 1414c1f1fa92db256c83083cb2107740957dff05 Mon Sep 17 00:00:00 2001 From: Svyatoslav Ilinskiy Date: Fri, 17 Nov 2017 17:43:51 -0600 Subject: [PATCH] Use docker-compose config --services in bash completion Signed-off-by: Svyatoslav Ilinskiy --- contrib/completion/bash/docker-compose | 32 +++++++------------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/contrib/completion/bash/docker-compose b/contrib/completion/bash/docker-compose index 1fdb27705..5885e686b 100644 --- a/contrib/completion/bash/docker-compose +++ b/contrib/completion/bash/docker-compose @@ -64,48 +64,32 @@ __docker_compose_services_all() { COMPREPLY=( $(compgen -W "$(___docker_compose_all_services_in_compose_file)" -- "$cur") ) } -# All services that have an entry with the given key in their compose_file section -___docker_compose_services_with_key() { - # flatten sections under "services" to one line, then filter lines containing the key and return section name - __docker_compose_q config \ - | sed -n -e '/^services:/,/^[^ ]/p' \ - | sed -n 's/^ //p' \ - | awk '/^[a-zA-Z0-9]/{printf "\n"};{printf $0;next;}' \ - | awk -F: -v key=": +$1:" '$0 ~ key {print $1}' -} - # All services that are defined by a Dockerfile reference __docker_compose_services_from_build() { - COMPREPLY=( $(compgen -W "$(___docker_compose_services_with_key build)" -- "$cur") ) + COMPREPLY=( $(compgen -W "$(__docker_compose_q config --services --filter "option=build")" -- "$cur") ) } # All services that are defined by an image __docker_compose_services_from_image() { - COMPREPLY=( $(compgen -W "$(___docker_compose_services_with_key image)" -- "$cur") ) -} - -# The services for which containers have been created, optionally filtered -# by a boolean expression passed in as argument. -__docker_compose_services_with() { - local containers names - containers="$(__docker_compose_q ps -q)" - names=$(docker 2>/dev/null inspect -f "{{if ${1:-true}}}{{range \$k, \$v := .Config.Labels}}{{if eq \$k \"com.docker.compose.service\"}}{{\$v}}{{end}}{{end}}{{end}}" $containers) - COMPREPLY=( $(compgen -W "$names" -- "$cur") ) + COMPREPLY=( $(compgen -W "$(__docker_compose_q config --services --filter "option=image")" -- "$cur") ) } # The services for which at least one paused container exists __docker_compose_services_paused() { - __docker_compose_services_with '.State.Paused' + names=$(__docker_compose_q config --services --filter "status=paused") + COMPREPLY=( $(compgen -W "$names" -- "$cur") ) } # The services for which at least one running container exists __docker_compose_services_running() { - __docker_compose_services_with '.State.Running' + names=$(__docker_compose_q config --services --filter "status=running") + COMPREPLY=( $(compgen -W "$names" -- "$cur") ) } # The services for which at least one stopped container exists __docker_compose_services_stopped() { - __docker_compose_services_with 'not .State.Running' + names=$(__docker_compose_q config --services --filter "status=stopped") + COMPREPLY=( $(compgen -W "$names" -- "$cur") ) }