Use docker-compose config --services in bash completion

Signed-off-by: Svyatoslav Ilinskiy <ilinskiy.sv@gmail.com>
This commit is contained in:
Svyatoslav Ilinskiy 2017-11-17 17:43:51 -06:00
parent 21e312e402
commit 1414c1f1fa
1 changed files with 8 additions and 24 deletions

View File

@ -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") )
}