| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- #!bash
- #
- # bash completion for docker-compose
- #
- # This work is based on the completion for the docker command.
- #
- # This script provides completion of:
- # - commands and their options
- # - service names
- # - filepaths
- #
- # To enable the completions either:
- # - place this file in /etc/bash_completion.d
- # or
- # - copy this file to e.g. ~/.docker-compose-completion.sh and add the line
- # below to your .bashrc after bash completion features are loaded
- # . ~/.docker-compose-completion.sh
- # For compatibility reasons, Compose and therefore its completion supports several
- # stack compositon files as listed here, in descending priority.
- # Support for these filenames might be dropped in some future version.
- __docker_compose_compose_file() {
- local file
- for file in docker-compose.y{,a}ml fig.y{,a}ml ; do
- [ -e $file ] && {
- echo $file
- return
- }
- done
- echo docker-compose.yml
- }
- # Extracts all service names from the compose file.
- ___docker_compose_all_services_in_compose_file() {
- awk -F: '/^[a-zA-Z0-9]/{print $1}' "${compose_file:-$(__docker_compose_compose_file)}" 2>/dev/null
- }
- # All services, even those without an existing container
- __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 to one line, then filter lines containing the key and return section name.
- awk '/^[a-zA-Z0-9]/{printf "\n"};{printf $0;next;}' "${compose_file:-$(__docker_compose_compose_file)}" 2>/dev/null | 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") )
- }
- # 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 2>/dev/null ${compose_file:+-f $compose_file} ${compose_project:+-p $compose_project} ps -q)"
- names=( $(docker 2>/dev/null inspect --format "{{if ${1:-true}}} {{ .Name }} {{end}}" $containers) )
- names=( ${names[@]%_*} ) # strip trailing numbers
- names=( ${names[@]#*_} ) # strip project name
- COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") )
- }
- # The services for which at least one paused container exists
- __docker_compose_services_paused() {
- __docker_compose_services_with '.State.Paused'
- }
- # The services for which at least one running container exists
- __docker_compose_services_running() {
- __docker_compose_services_with '.State.Running'
- }
- # The services for which at least one stopped container exists
- __docker_compose_services_stopped() {
- __docker_compose_services_with 'not .State.Running'
- }
- _docker_compose_build() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help --no-cache --pull" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_from_build
- ;;
- esac
- }
- _docker_compose_docker_compose() {
- case "$prev" in
- --file|-f)
- _filedir "y?(a)ml"
- return
- ;;
- --project-name|-p)
- return
- ;;
- --x-network-driver)
- COMPREPLY=( $( compgen -W "bridge host none overlay" -- "$cur" ) )
- return
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--file -f --help -h --project-name -p --verbose --version -v --x-networking --x-network-driver" -- "$cur" ) )
- ;;
- *)
- COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
- ;;
- esac
- }
- _docker_compose_help() {
- COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
- }
- _docker_compose_kill() {
- case "$prev" in
- -s)
- COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) )
- return
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help -s" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_running
- ;;
- esac
- }
- _docker_compose_logs() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help --no-color" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_all
- ;;
- esac
- }
- _docker_compose_migrate_to_labels() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
- ;;
- esac
- }
- _docker_compose_pause() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_running
- ;;
- esac
- }
- _docker_compose_port() {
- case "$prev" in
- --protocol)
- COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) )
- return;
- ;;
- --index)
- return;
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help --index --protocol" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_all
- ;;
- esac
- }
- _docker_compose_ps() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help -q" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_all
- ;;
- esac
- }
- _docker_compose_pull() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help --ignore-pull-failures" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_from_image
- ;;
- esac
- }
- _docker_compose_restart() {
- case "$prev" in
- --timeout|-t)
- return
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_running
- ;;
- esac
- }
- _docker_compose_rm() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--force -f --help -v" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_stopped
- ;;
- esac
- }
- _docker_compose_run() {
- case "$prev" in
- -e)
- COMPREPLY=( $( compgen -e -- "$cur" ) )
- compopt -o nospace
- return
- ;;
- --entrypoint|--name|--user|-u)
- return
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "-d --entrypoint -e --help --name --no-deps --publish -p --rm --service-ports -T --user -u" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_all
- ;;
- esac
- }
- _docker_compose_scale() {
- case "$prev" in
- =)
- COMPREPLY=("$cur")
- return
- ;;
- --timeout|-t)
- return
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
- ;;
- *)
- COMPREPLY=( $(compgen -S "=" -W "$(___docker_compose_all_services_in_compose_file)" -- "$cur") )
- compopt -o nospace
- ;;
- esac
- }
- _docker_compose_start() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_stopped
- ;;
- esac
- }
- _docker_compose_stop() {
- case "$prev" in
- --timeout|-t)
- return
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_running
- ;;
- esac
- }
- _docker_compose_unpause() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_paused
- ;;
- esac
- }
- _docker_compose_up() {
- case "$prev" in
- --timeout|-t)
- return
- ;;
- esac
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "-d --help --no-build --no-color --no-deps --no-recreate --force-recreate --timeout -t" -- "$cur" ) )
- ;;
- *)
- __docker_compose_services_all
- ;;
- esac
- }
- _docker_compose_version() {
- case "$cur" in
- -*)
- COMPREPLY=( $( compgen -W "--short" -- "$cur" ) )
- ;;
- esac
- }
- _docker_compose() {
- local previous_extglob_setting=$(shopt -p extglob)
- shopt -s extglob
- local commands=(
- build
- help
- kill
- logs
- migrate-to-labels
- pause
- port
- ps
- pull
- restart
- rm
- run
- scale
- start
- stop
- unpause
- up
- version
- )
- COMPREPLY=()
- local cur prev words cword
- _get_comp_words_by_ref -n : cur prev words cword
- # search subcommand and invoke its handler.
- # special treatment of some top-level options
- local command='docker_compose'
- local counter=1
- local compose_file compose_project
- while [ $counter -lt $cword ]; do
- case "${words[$counter]}" in
- --file|-f)
- (( counter++ ))
- compose_file="${words[$counter]}"
- ;;
- --project-name|p)
- (( counter++ ))
- compose_project="${words[$counter]}"
- ;;
- --x-network-driver)
- (( counter++ ))
- ;;
- -*)
- ;;
- *)
- command="${words[$counter]}"
- break
- ;;
- esac
- (( counter++ ))
- done
- local completions_func=_docker_compose_${command//-/_}
- declare -F $completions_func >/dev/null && $completions_func
- eval "$previous_extglob_setting"
- return 0
- }
- complete -F _docker_compose docker-compose
|