2015-06-28 02:16:17 -07:00
#compdef docker-compose
# Description
# -----------
# zsh completion for docker-compose
# -------------------------------------------------------------------------
# Authors
# -------
# * Steve Durrheimer <s.durrheimer@gmail.com>
# -------------------------------------------------------------------------
# Inspiration
# -----------
# * @albers docker-compose bash completion script
# * @felixr docker zsh completion script : https://github.com/felixr/docker-zsh-completion
# -------------------------------------------------------------------------
2016-07-18 15:24:12 -07:00
__docker-compose_q() {
docker-compose 2>/dev/null $compose_options "$@"
2015-06-28 02:16:17 -07:00
}
2016-07-18 15:24:12 -07:00
# All services defined in docker-compose.yml
__docker-compose_all_services_in_compose_file() {
2015-06-28 02:16:17 -07:00
local already_selected
local -a services
2016-07-18 15:24:12 -07:00
already_selected=$(echo $words | tr " " "|")
2019-04-09 04:45:43 -07:00
__docker-compose_q ps --services "$@" \
2016-07-18 15:24:12 -07:00
| grep -Ev "^(${already_selected})$"
2015-06-28 02:16:17 -07:00
}
# All services, even those without an existing container
__docker-compose_services_all() {
2016-07-18 15:24:12 -07:00
[[ $PREFIX = -* ]] && return 1
integer ret=1
2019-04-09 04:45:43 -07:00
services=$(__docker-compose_all_services_in_compose_file "$@")
2016-07-18 15:24:12 -07:00
_alternative "args:services:($services)" && ret=0
return ret
2015-06-28 02:16:17 -07:00
}
# All services that are defined by a Dockerfile reference
__docker-compose_services_from_build() {
2016-07-18 15:24:12 -07:00
[[ $PREFIX = -* ]] && return 1
2019-04-09 04:45:43 -07:00
__docker-compose_services_all --filter source=build
2015-06-28 02:16:17 -07:00
}
# All services that are defined by an image
__docker-compose_services_from_image() {
2016-07-18 15:24:12 -07:00
[[ $PREFIX = -* ]] && return 1
2019-04-09 04:45:43 -07:00
__docker-compose_services_all --filter source=image
2016-07-18 15:24:12 -07:00
}
__docker-compose_pausedservices() {
[[ $PREFIX = -* ]] && return 1
2019-04-09 04:45:43 -07:00
__docker-compose_services_all --filter status=paused
2015-06-28 02:16:17 -07:00
}
__docker-compose_stoppedservices() {
2016-07-18 15:24:12 -07:00
[[ $PREFIX = -* ]] && return 1
2019-04-09 04:45:43 -07:00
__docker-compose_services_all --filter status=stopped
2015-06-28 02:16:17 -07:00
}
__docker-compose_runningservices() {
2016-07-18 15:24:12 -07:00
[[ $PREFIX = -* ]] && return 1
2019-04-09 04:45:43 -07:00
__docker-compose_services_all --filter status=running
2015-06-28 02:16:17 -07:00
}
2016-07-18 15:24:12 -07:00
__docker-compose_services() {
[[ $PREFIX = -* ]] && return 1
2019-04-09 04:45:43 -07:00
__docker-compose_services_all
2015-06-28 02:16:17 -07:00
}
__docker-compose_caching_policy() {
2016-07-18 15:24:12 -07:00
oldp=( "$1"(Nmh+1) ) # 1 hour
2015-06-28 02:16:17 -07:00
(( $#oldp ))
}
2016-07-18 15:24:12 -07:00
__docker-compose_commands() {
2015-06-28 02:16:17 -07:00
local cache_policy
zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
if [[ -z "$cache_policy" ]]; then
zstyle ":completion:${curcontext}:" cache-policy __docker-compose_caching_policy
fi
if ( [[ ${+_docker_compose_subcommands} -eq 0 ]] || _cache_invalid docker_compose_subcommands) \
&& ! _retrieve_cache docker_compose_subcommands;
then
local -a lines
lines=(${(f)"$(_call_program commands docker-compose 2>&1)"})
_docker_compose_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I) *]}]}## #}/ ##/:})
2016-07-18 15:24:12 -07:00
(( $#_docker_compose_subcommands > 0 )) && _store_cache docker_compose_subcommands _docker_compose_subcommands
2015-06-28 02:16:17 -07:00
fi
_describe -t docker-compose-commands "docker-compose command" _docker_compose_subcommands
}
2016-07-18 15:24:12 -07:00
__docker-compose_subcommand() {
local opts_help opts_force_recreate opts_no_recreate opts_no_build opts_remove_orphans opts_timeout opts_no_color opts_no_deps
opts_help='(: -)--help[Print usage]'
opts_force_recreate="(--no-recreate)--force-recreate[Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.]"
opts_no_recreate="(--force-recreate)--no-recreate[If containers already exist, don't recreate them. Incompatible with --force-recreate.]"
opts_no_build="(--build)--no-build[Don't build an image, even if it's missing.]"
opts_remove_orphans="--remove-orphans[Remove containers for services not defined in the Compose file]"
opts_timeout=('(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: ")
opts_no_color='--no-color[Produce monochrome output.]'
opts_no_deps="--no-deps[Don't start linked services.]"
2015-06-28 02:16:17 -07:00
integer ret=1
2016-07-18 15:24:12 -07:00
2015-06-28 02:16:17 -07:00
case "$words[1]" in
(build)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
2018-03-09 09:11:52 -07:00
"*--build-arg=[Set build-time variables for one service.]:<varname>=<value>: " \
2016-07-18 15:24:12 -07:00
'--force-rm[Always remove intermediate containers.]' \
2019-04-09 04:45:43 -07:00
'(--quiet -q)'{--quiet,-q}'[Curb build output]' \
'(--memory -m)'{--memory,-m}'[Memory limit for the build container.]' \
2016-07-18 15:24:12 -07:00
'--no-cache[Do not use cache when building the image.]' \
'--pull[Always attempt to pull a newer version of the image.]' \
2019-04-09 04:45:43 -07:00
'--compress[Compress the build context using gzip.]' \
'--parallel[Build images in parallel.]' \
2015-06-28 02:16:17 -07:00
'*:services:__docker-compose_services_from_build' && ret=0
;;
2016-07-18 15:24:12 -07:00
(config)
_arguments \
$opts_help \
'(--quiet -q)'{--quiet,-q}"[Only validate the configuration, don't print anything.]" \
2018-03-09 09:11:52 -07:00
'--resolve-image-digests[Pin image tags to digests.]' \
'--services[Print the service names, one per line.]' \
2019-04-09 04:45:43 -07:00
'--volumes[Print the volume names, one per line.]' \
2024-03-15 21:26:48 -07:00
'--hash[Print the service config hash, one per line. Set "service1,service2" for a list of specified services.]' && ret=0
2016-07-18 15:24:12 -07:00
;;
(create)
_arguments \
$opts_help \
$opts_force_recreate \
$opts_no_recreate \
$opts_no_build \
"(--no-build)--build[Build images before creating containers.]" \
2019-04-09 04:45:43 -07:00
'*:services:__docker-compose_services' && ret=0
2016-07-18 15:24:12 -07:00
;;
(down)
_arguments \
$opts_help \
2019-04-09 04:45:43 -07:00
$opts_timeout \
2016-07-18 15:24:12 -07:00
"--rmi[Remove images. Type must be one of: 'all': Remove all images used by any service. 'local': Remove only images that don't have a custom tag set by the \`image\` field.]:type:(all local)" \
'(-v --volumes)'{-v,--volumes}"[Remove named volumes declared in the \`volumes\` section of the Compose file and anonymous volumes attached to containers.]" \
$opts_remove_orphans && ret=0
;;
(events)
_arguments \
$opts_help \
'--json[Output events as a stream of json objects]' \
2019-04-09 04:45:43 -07:00
'*:services:__docker-compose_services' && ret=0
2016-07-18 15:24:12 -07:00
;;
(exec)
_arguments \
$opts_help \
'-d[Detached mode: Run command in the background.]' \
'--privileged[Give extended privileges to the process.]' \
2019-04-09 04:45:43 -07:00
'(-u --user)'{-u,--user=}'[Run the command as this user.]:username:_users' \
2016-07-18 15:24:12 -07:00
'-T[Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY.]' \
'--index=[Index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
2019-04-09 04:45:43 -07:00
'*'{-e,--env}'[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
'(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
2016-07-18 15:24:12 -07:00
'(-):running services:__docker-compose_runningservices' \
'(-):command: _command_names -e' \
'*::arguments: _normal' && ret=0
;;
2015-06-28 02:16:17 -07:00
(help)
_arguments ':subcommand:__docker-compose_commands' && ret=0
;;
2019-04-09 04:45:43 -07:00
(images)
_arguments \
$opts_help \
'-q[Only display IDs]' \
'*:services:__docker-compose_services' && ret=0
;;
2015-06-28 02:16:17 -07:00
(kill)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
2015-06-28 02:16:17 -07:00
'-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
'*:running services:__docker-compose_runningservices' && ret=0
;;
(logs)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
'(-f --follow)'{-f,--follow}'[Follow log output]' \
$opts_no_color \
'--tail=[Number of lines to show from the end of the logs for each container.]:number of lines: ' \
'(-t --timestamps)'{-t,--timestamps}'[Show timestamps]' \
2019-04-09 04:45:43 -07:00
'*:services:__docker-compose_services' && ret=0
2015-06-28 02:16:17 -07:00
;;
2016-07-18 15:24:12 -07:00
(pause)
2015-06-28 02:16:17 -07:00
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
'*:running services:__docker-compose_runningservices' && ret=0
2015-06-28 02:16:17 -07:00
;;
(port)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
'--protocol=[tcp or udp \[default: tcp\]]:protocol:(tcp udp)' \
'--index=[index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
2015-06-28 02:16:17 -07:00
'1:running services:__docker-compose_runningservices' \
'2:port:_ports' && ret=0
;;
(ps)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
2015-06-28 02:16:17 -07:00
'-q[Only display IDs]' \
2019-04-09 04:45:43 -07:00
'--filter KEY=VAL[Filter services by a property]:<filtername>=<value>:' \
'*:services:__docker-compose_services' && ret=0
2015-06-28 02:16:17 -07:00
;;
(pull)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
'--ignore-pull-failures[Pull what it can and ignores images with pull failures.]' \
2019-04-09 04:45:43 -07:00
'--no-parallel[Disable parallel pulling]' \
'(-q --quiet)'{-q,--quiet}'[Pull without printing progress information]' \
'--include-deps[Also pull services declared as dependencies]' \
2015-06-28 02:16:17 -07:00
'*:services:__docker-compose_services_from_image' && ret=0
;;
2016-07-18 15:24:12 -07:00
(push)
_arguments \
$opts_help \
'--ignore-push-failures[Push what it can and ignores images with push failures.]' \
'*:services:__docker-compose_services' && ret=0
;;
2015-06-28 02:16:17 -07:00
(rm)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
2015-06-28 02:16:17 -07:00
'(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
2016-07-18 15:24:12 -07:00
'-v[Remove any anonymous volumes attached to containers]' \
2015-06-28 02:16:17 -07:00
'*:stopped services:__docker-compose_stoppedservices' && ret=0
;;
(run)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
2018-03-09 09:11:52 -07:00
$opts_no_deps \
2015-06-28 02:16:17 -07:00
'-d[Detached mode: Run container in the background, print new container name.]' \
'*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
2019-04-09 04:45:43 -07:00
'*'{-l,--label}'[KEY=VAL Add or override a label (can be used multiple times)]:label KEY=VAL: ' \
2016-07-18 15:24:12 -07:00
'--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
'--name=[Assign a name to the container]:name: ' \
'(-p --publish)'{-p,--publish=}"[Publish a container's port(s) to the host]" \
2015-06-28 02:16:17 -07:00
'--rm[Remove container after run. Ignored in detached mode.]' \
"--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
'-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
2016-07-18 15:24:12 -07:00
'(-u --user)'{-u,--user=}'[Run as specified username or uid]:username or uid:_users' \
2018-03-09 09:11:52 -07:00
'(-v --volume)*'{-v,--volume=}'[Bind mount a volume]:volume: ' \
2016-07-18 15:24:12 -07:00
'(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
2019-04-09 04:45:43 -07:00
"--use-aliases[Use the services network aliases in the network(s) the container connects to]" \
2015-06-28 02:16:17 -07:00
'(-):services:__docker-compose_services' \
'(-):command: _command_names -e' \
'*::arguments: _normal' && ret=0
;;
(scale)
2016-07-18 15:24:12 -07:00
_arguments \
$opts_help \
$opts_timeout \
'*:running services:__docker-compose_runningservices' && ret=0
2015-06-28 02:16:17 -07:00
;;
(start)
2016-07-18 15:24:12 -07:00
_arguments \
$opts_help \
'*:stopped services:__docker-compose_stoppedservices' && ret=0
2015-06-28 02:16:17 -07:00
;;
(stop|restart)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
$opts_timeout \
2015-06-28 02:16:17 -07:00
'*:running services:__docker-compose_runningservices' && ret=0
;;
2018-03-09 09:11:52 -07:00
(top)
_arguments \
$opts_help \
'*:running services:__docker-compose_runningservices' && ret=0
;;
2016-07-18 15:24:12 -07:00
(unpause)
_arguments \
$opts_help \
'*:paused services:__docker-compose_pausedservices' && ret=0
;;
2015-06-28 02:16:17 -07:00
(up)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
2021-10-04 03:02:08 -07:00
'(--abort-on-container-exit)-d[Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit and --attach-dependencies.]' \
2016-07-18 15:24:12 -07:00
$opts_no_color \
$opts_no_deps \
$opts_force_recreate \
$opts_no_recreate \
$opts_no_build \
"(--no-build)--build[Build images before starting containers.]" \
"(-d)--abort-on-container-exit[Stops all containers if any container was stopped. Incompatible with -d.]" \
2021-10-04 03:02:08 -07:00
"(-d)--attach-dependencies[Attach to dependent containers. Incompatible with -d.]" \
2016-07-18 15:24:12 -07:00
'(-t --timeout)'{-t,--timeout}"[Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)]:seconds: " \
2019-04-09 04:45:43 -07:00
'--scale[SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.]:service scale SERVICE=NUM: ' \
'--exit-code-from=[Return the exit code of the selected service container. Implies --abort-on-container-exit]:service:__docker-compose_services' \
2016-07-18 15:24:12 -07:00
$opts_remove_orphans \
2019-04-09 04:45:43 -07:00
'*:services:__docker-compose_services' && ret=0
2015-06-28 02:16:17 -07:00
;;
(version)
_arguments \
2016-07-18 15:24:12 -07:00
$opts_help \
2015-06-28 02:16:17 -07:00
"--short[Shows only Compose's version number.]" && ret=0
;;
(*)
2016-07-18 15:24:12 -07:00
_message 'Unknown sub command' && ret=1
;;
2015-06-28 02:16:17 -07:00
esac
return ret
}
2016-07-18 15:24:12 -07:00
_docker-compose() {
2015-06-28 02:16:17 -07:00
# Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
# Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
if [[ $service != docker-compose ]]; then
_call_function - _$service
return
fi
2016-07-18 15:24:12 -07:00
local curcontext="$curcontext" state line
integer ret=1
2015-06-28 02:16:17 -07:00
typeset -A opt_args
2018-03-09 09:11:52 -07:00
local file_description
if [[ -n ${words[(r)-f]} || -n ${words[(r)--file]} ]] ; then
file_description="Specify an override docker-compose file (default: docker-compose.override.yml)"
else
file_description="Specify an alternate docker-compose file (default: docker-compose.yml)"
fi
2015-06-28 02:16:17 -07:00
_arguments -C \
'(- :)'{-h,--help}'[Get help]' \
2018-03-09 09:11:52 -07:00
'*'{-f,--file}"[${file_description}]:file:_files -g '*.yml'" \
2015-06-28 02:16:17 -07:00
'(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
2021-10-04 03:02:08 -07:00
'--env-file[Specify an alternate environment file (default: .env)]:env-file:_files' \
2019-04-09 04:45:43 -07:00
"--compatibility[If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent]" \
2016-07-18 15:24:12 -07:00
'(- :)'{-v,--version}'[Print version and exit]' \
2019-04-09 04:45:43 -07:00
'--verbose[Show more output]' \
'--log-level=[Set log level]:level:(DEBUG INFO WARNING ERROR CRITICAL)' \
'--no-ansi[Do not print ANSI control characters]' \
2021-10-04 03:02:08 -07:00
'--ansi=[Control when to print ANSI control characters]:when:(never always auto)' \
2016-07-18 15:24:12 -07:00
'(-H --host)'{-H,--host}'[Daemon socket to connect to]:host:' \
'--tls[Use TLS; implied by --tlsverify]' \
'--tlscacert=[Trust certs signed only by this CA]:ca path:' \
'--tlscert=[Path to TLS certificate file]:client cert path:' \
'--tlskey=[Path to TLS key file]:tls key path:' \
'--tlsverify[Use TLS and verify the remote]' \
"--skip-hostname-check[Don't check the daemon's hostname against the name specified in the client certificate (for example if your docker host is an IP address)]" \
2015-06-28 02:16:17 -07:00
'(-): :->command' \
'(-)*:: :->option-or-argument' && ret=0
2019-04-09 04:45:43 -07:00
local -a relevant_compose_flags relevant_compose_repeatable_flags relevant_docker_flags compose_options docker_options
2016-07-18 15:24:12 -07:00
relevant_compose_flags=(
2021-10-04 03:02:08 -07:00
"--env-file"
2016-07-18 15:24:12 -07:00
"--file" "-f"
"--host" "-H"
"--project-name" "-p"
"--tls"
"--tlscacert"
"--tlscert"
"--tlskey"
"--tlsverify"
"--skip-hostname-check"
)
2019-04-09 04:45:43 -07:00
relevant_compose_repeatable_flags=(
"--file" "-f"
)
2016-07-18 15:24:12 -07:00
relevant_docker_flags=(
"--host" "-H"
"--tls"
"--tlscacert"
"--tlscert"
"--tlskey"
"--tlsverify"
)
for k in "${(@k)opt_args}"; do
if [[ -n "${relevant_docker_flags[(r)$k]}" ]]; then
docker_options+=$k
if [[ -n "$opt_args[$k]" ]]; then
docker_options+=$opt_args[$k]
fi
fi
if [[ -n "${relevant_compose_flags[(r)$k]}" ]]; then
2019-04-09 04:45:43 -07:00
if [[ -n "${relevant_compose_repeatable_flags[(r)$k]}" ]]; then
values=("${(@s/:/)opt_args[$k]}")
for value in $values
do
compose_options+=$k
compose_options+=$value
done
else
compose_options+=$k
if [[ -n "$opt_args[$k]" ]]; then
compose_options+=$opt_args[$k]
fi
2016-07-18 15:24:12 -07:00
fi
fi
2015-06-28 02:16:17 -07:00
done
case $state in
(command)
__docker-compose_commands && ret=0
;;
(option-or-argument)
curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
__docker-compose_subcommand && ret=0
;;
esac
return ret
}
_docker-compose "$@"