_docker-compose 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #compdef docker-compose
  2. # Description
  3. # -----------
  4. # zsh completion for docker-compose
  5. # -------------------------------------------------------------------------
  6. # Authors
  7. # -------
  8. # * Steve Durrheimer <s.durrheimer@gmail.com>
  9. # -------------------------------------------------------------------------
  10. # Inspiration
  11. # -----------
  12. # * @albers docker-compose bash completion script
  13. # * @felixr docker zsh completion script : https://github.com/felixr/docker-zsh-completion
  14. # -------------------------------------------------------------------------
  15. __docker-compose_q() {
  16. docker-compose 2>/dev/null $compose_options "$@"
  17. }
  18. # All services defined in docker-compose.yml
  19. __docker-compose_all_services_in_compose_file() {
  20. local already_selected
  21. local -a services
  22. already_selected=$(echo $words | tr " " "|")
  23. __docker-compose_q ps --services "$@" \
  24. | grep -Ev "^(${already_selected})$"
  25. }
  26. # All services, even those without an existing container
  27. __docker-compose_services_all() {
  28. [[ $PREFIX = -* ]] && return 1
  29. integer ret=1
  30. services=$(__docker-compose_all_services_in_compose_file "$@")
  31. _alternative "args:services:($services)" && ret=0
  32. return ret
  33. }
  34. # All services that are defined by a Dockerfile reference
  35. __docker-compose_services_from_build() {
  36. [[ $PREFIX = -* ]] && return 1
  37. __docker-compose_services_all --filter source=build
  38. }
  39. # All services that are defined by an image
  40. __docker-compose_services_from_image() {
  41. [[ $PREFIX = -* ]] && return 1
  42. __docker-compose_services_all --filter source=image
  43. }
  44. __docker-compose_pausedservices() {
  45. [[ $PREFIX = -* ]] && return 1
  46. __docker-compose_services_all --filter status=paused
  47. }
  48. __docker-compose_stoppedservices() {
  49. [[ $PREFIX = -* ]] && return 1
  50. __docker-compose_services_all --filter status=stopped
  51. }
  52. __docker-compose_runningservices() {
  53. [[ $PREFIX = -* ]] && return 1
  54. __docker-compose_services_all --filter status=running
  55. }
  56. __docker-compose_services() {
  57. [[ $PREFIX = -* ]] && return 1
  58. __docker-compose_services_all
  59. }
  60. __docker-compose_caching_policy() {
  61. oldp=( "$1"(Nmh+1) ) # 1 hour
  62. (( $#oldp ))
  63. }
  64. __docker-compose_commands() {
  65. local cache_policy
  66. zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
  67. if [[ -z "$cache_policy" ]]; then
  68. zstyle ":completion:${curcontext}:" cache-policy __docker-compose_caching_policy
  69. fi
  70. if ( [[ ${+_docker_compose_subcommands} -eq 0 ]] || _cache_invalid docker_compose_subcommands) \
  71. && ! _retrieve_cache docker_compose_subcommands;
  72. then
  73. local -a lines
  74. lines=(${(f)"$(_call_program commands docker-compose 2>&1)"})
  75. _docker_compose_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I) *]}]}## #}/ ##/:})
  76. (( $#_docker_compose_subcommands > 0 )) && _store_cache docker_compose_subcommands _docker_compose_subcommands
  77. fi
  78. _describe -t docker-compose-commands "docker-compose command" _docker_compose_subcommands
  79. }
  80. __docker-compose_subcommand() {
  81. local opts_help opts_force_recreate opts_no_recreate opts_no_build opts_remove_orphans opts_timeout opts_no_color opts_no_deps
  82. opts_help='(: -)--help[Print usage]'
  83. opts_force_recreate="(--no-recreate)--force-recreate[Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.]"
  84. opts_no_recreate="(--force-recreate)--no-recreate[If containers already exist, don't recreate them. Incompatible with --force-recreate.]"
  85. opts_no_build="(--build)--no-build[Don't build an image, even if it's missing.]"
  86. opts_remove_orphans="--remove-orphans[Remove containers for services not defined in the Compose file]"
  87. opts_timeout=('(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: ")
  88. opts_no_color='--no-color[Produce monochrome output.]'
  89. opts_no_deps="--no-deps[Don't start linked services.]"
  90. integer ret=1
  91. case "$words[1]" in
  92. (build)
  93. _arguments \
  94. $opts_help \
  95. "*--build-arg=[Set build-time variables for one service.]:<varname>=<value>: " \
  96. '--force-rm[Always remove intermediate containers.]' \
  97. '(--quiet -q)'{--quiet,-q}'[Curb build output]' \
  98. '(--memory -m)'{--memory,-m}'[Memory limit for the build container.]' \
  99. '--no-cache[Do not use cache when building the image.]' \
  100. '--pull[Always attempt to pull a newer version of the image.]' \
  101. '--compress[Compress the build context using gzip.]' \
  102. '--parallel[Build images in parallel.]' \
  103. '*:services:__docker-compose_services_from_build' && ret=0
  104. ;;
  105. (config)
  106. _arguments \
  107. $opts_help \
  108. '(--quiet -q)'{--quiet,-q}"[Only validate the configuration, don't print anything.]" \
  109. '--resolve-image-digests[Pin image tags to digests.]' \
  110. '--services[Print the service names, one per line.]' \
  111. '--volumes[Print the volume names, one per line.]' \
  112. '--hash[Print the service config hash, one per line. Set "service1,service2" for a list of specified services.]' \ && ret=0
  113. ;;
  114. (create)
  115. _arguments \
  116. $opts_help \
  117. $opts_force_recreate \
  118. $opts_no_recreate \
  119. $opts_no_build \
  120. "(--no-build)--build[Build images before creating containers.]" \
  121. '*:services:__docker-compose_services' && ret=0
  122. ;;
  123. (down)
  124. _arguments \
  125. $opts_help \
  126. $opts_timeout \
  127. "--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)" \
  128. '(-v --volumes)'{-v,--volumes}"[Remove named volumes declared in the \`volumes\` section of the Compose file and anonymous volumes attached to containers.]" \
  129. $opts_remove_orphans && ret=0
  130. ;;
  131. (events)
  132. _arguments \
  133. $opts_help \
  134. '--json[Output events as a stream of json objects]' \
  135. '*:services:__docker-compose_services' && ret=0
  136. ;;
  137. (exec)
  138. _arguments \
  139. $opts_help \
  140. '-d[Detached mode: Run command in the background.]' \
  141. '--privileged[Give extended privileges to the process.]' \
  142. '(-u --user)'{-u,--user=}'[Run the command as this user.]:username:_users' \
  143. '-T[Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY.]' \
  144. '--index=[Index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
  145. '*'{-e,--env}'[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  146. '(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
  147. '(-):running services:__docker-compose_runningservices' \
  148. '(-):command: _command_names -e' \
  149. '*::arguments: _normal' && ret=0
  150. ;;
  151. (help)
  152. _arguments ':subcommand:__docker-compose_commands' && ret=0
  153. ;;
  154. (images)
  155. _arguments \
  156. $opts_help \
  157. '-q[Only display IDs]' \
  158. '*:services:__docker-compose_services' && ret=0
  159. ;;
  160. (kill)
  161. _arguments \
  162. $opts_help \
  163. '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
  164. '*:running services:__docker-compose_runningservices' && ret=0
  165. ;;
  166. (logs)
  167. _arguments \
  168. $opts_help \
  169. '(-f --follow)'{-f,--follow}'[Follow log output]' \
  170. $opts_no_color \
  171. '--tail=[Number of lines to show from the end of the logs for each container.]:number of lines: ' \
  172. '(-t --timestamps)'{-t,--timestamps}'[Show timestamps]' \
  173. '*:services:__docker-compose_services' && ret=0
  174. ;;
  175. (pause)
  176. _arguments \
  177. $opts_help \
  178. '*:running services:__docker-compose_runningservices' && ret=0
  179. ;;
  180. (port)
  181. _arguments \
  182. $opts_help \
  183. '--protocol=[tcp or udp \[default: tcp\]]:protocol:(tcp udp)' \
  184. '--index=[index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
  185. '1:running services:__docker-compose_runningservices' \
  186. '2:port:_ports' && ret=0
  187. ;;
  188. (ps)
  189. _arguments \
  190. $opts_help \
  191. '-q[Only display IDs]' \
  192. '--filter KEY=VAL[Filter services by a property]:<filtername>=<value>:' \
  193. '*:services:__docker-compose_services' && ret=0
  194. ;;
  195. (pull)
  196. _arguments \
  197. $opts_help \
  198. '--ignore-pull-failures[Pull what it can and ignores images with pull failures.]' \
  199. '--no-parallel[Disable parallel pulling]' \
  200. '(-q --quiet)'{-q,--quiet}'[Pull without printing progress information]' \
  201. '--include-deps[Also pull services declared as dependencies]' \
  202. '*:services:__docker-compose_services_from_image' && ret=0
  203. ;;
  204. (push)
  205. _arguments \
  206. $opts_help \
  207. '--ignore-push-failures[Push what it can and ignores images with push failures.]' \
  208. '*:services:__docker-compose_services' && ret=0
  209. ;;
  210. (rm)
  211. _arguments \
  212. $opts_help \
  213. '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
  214. '-v[Remove any anonymous volumes attached to containers]' \
  215. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  216. ;;
  217. (run)
  218. _arguments \
  219. $opts_help \
  220. $opts_no_deps \
  221. '-d[Detached mode: Run container in the background, print new container name.]' \
  222. '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  223. '*'{-l,--label}'[KEY=VAL Add or override a label (can be used multiple times)]:label KEY=VAL: ' \
  224. '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
  225. '--name=[Assign a name to the container]:name: ' \
  226. '(-p --publish)'{-p,--publish=}"[Publish a container's port(s) to the host]" \
  227. '--rm[Remove container after run. Ignored in detached mode.]' \
  228. "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
  229. '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
  230. '(-u --user)'{-u,--user=}'[Run as specified username or uid]:username or uid:_users' \
  231. '(-v --volume)*'{-v,--volume=}'[Bind mount a volume]:volume: ' \
  232. '(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
  233. "--use-aliases[Use the services network aliases in the network(s) the container connects to]" \
  234. '(-):services:__docker-compose_services' \
  235. '(-):command: _command_names -e' \
  236. '*::arguments: _normal' && ret=0
  237. ;;
  238. (scale)
  239. _arguments \
  240. $opts_help \
  241. $opts_timeout \
  242. '*:running services:__docker-compose_runningservices' && ret=0
  243. ;;
  244. (start)
  245. _arguments \
  246. $opts_help \
  247. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  248. ;;
  249. (stop|restart)
  250. _arguments \
  251. $opts_help \
  252. $opts_timeout \
  253. '*:running services:__docker-compose_runningservices' && ret=0
  254. ;;
  255. (top)
  256. _arguments \
  257. $opts_help \
  258. '*:running services:__docker-compose_runningservices' && ret=0
  259. ;;
  260. (unpause)
  261. _arguments \
  262. $opts_help \
  263. '*:paused services:__docker-compose_pausedservices' && ret=0
  264. ;;
  265. (up)
  266. _arguments \
  267. $opts_help \
  268. '(--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.]' \
  269. $opts_no_color \
  270. $opts_no_deps \
  271. $opts_force_recreate \
  272. $opts_no_recreate \
  273. $opts_no_build \
  274. "(--no-build)--build[Build images before starting containers.]" \
  275. "(-d)--abort-on-container-exit[Stops all containers if any container was stopped. Incompatible with -d.]" \
  276. "(-d)--attach-dependencies[Attach to dependent containers. Incompatible with -d.]" \
  277. '(-t --timeout)'{-t,--timeout}"[Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)]:seconds: " \
  278. '--scale[SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.]:service scale SERVICE=NUM: ' \
  279. '--exit-code-from=[Return the exit code of the selected service container. Implies --abort-on-container-exit]:service:__docker-compose_services' \
  280. $opts_remove_orphans \
  281. '*:services:__docker-compose_services' && ret=0
  282. ;;
  283. (version)
  284. _arguments \
  285. $opts_help \
  286. "--short[Shows only Compose's version number.]" && ret=0
  287. ;;
  288. (*)
  289. _message 'Unknown sub command' && ret=1
  290. ;;
  291. esac
  292. return ret
  293. }
  294. _docker-compose() {
  295. # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
  296. # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
  297. if [[ $service != docker-compose ]]; then
  298. _call_function - _$service
  299. return
  300. fi
  301. local curcontext="$curcontext" state line
  302. integer ret=1
  303. typeset -A opt_args
  304. local file_description
  305. if [[ -n ${words[(r)-f]} || -n ${words[(r)--file]} ]] ; then
  306. file_description="Specify an override docker-compose file (default: docker-compose.override.yml)"
  307. else
  308. file_description="Specify an alternate docker-compose file (default: docker-compose.yml)"
  309. fi
  310. _arguments -C \
  311. '(- :)'{-h,--help}'[Get help]' \
  312. '*'{-f,--file}"[${file_description}]:file:_files -g '*.yml'" \
  313. '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
  314. '--env-file[Specify an alternate environment file (default: .env)]:env-file:_files' \
  315. "--compatibility[If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent]" \
  316. '(- :)'{-v,--version}'[Print version and exit]' \
  317. '--verbose[Show more output]' \
  318. '--log-level=[Set log level]:level:(DEBUG INFO WARNING ERROR CRITICAL)' \
  319. '--no-ansi[Do not print ANSI control characters]' \
  320. '--ansi=[Control when to print ANSI control characters]:when:(never always auto)' \
  321. '(-H --host)'{-H,--host}'[Daemon socket to connect to]:host:' \
  322. '--tls[Use TLS; implied by --tlsverify]' \
  323. '--tlscacert=[Trust certs signed only by this CA]:ca path:' \
  324. '--tlscert=[Path to TLS certificate file]:client cert path:' \
  325. '--tlskey=[Path to TLS key file]:tls key path:' \
  326. '--tlsverify[Use TLS and verify the remote]' \
  327. "--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)]" \
  328. '(-): :->command' \
  329. '(-)*:: :->option-or-argument' && ret=0
  330. local -a relevant_compose_flags relevant_compose_repeatable_flags relevant_docker_flags compose_options docker_options
  331. relevant_compose_flags=(
  332. "--env-file"
  333. "--file" "-f"
  334. "--host" "-H"
  335. "--project-name" "-p"
  336. "--tls"
  337. "--tlscacert"
  338. "--tlscert"
  339. "--tlskey"
  340. "--tlsverify"
  341. "--skip-hostname-check"
  342. )
  343. relevant_compose_repeatable_flags=(
  344. "--file" "-f"
  345. )
  346. relevant_docker_flags=(
  347. "--host" "-H"
  348. "--tls"
  349. "--tlscacert"
  350. "--tlscert"
  351. "--tlskey"
  352. "--tlsverify"
  353. )
  354. for k in "${(@k)opt_args}"; do
  355. if [[ -n "${relevant_docker_flags[(r)$k]}" ]]; then
  356. docker_options+=$k
  357. if [[ -n "$opt_args[$k]" ]]; then
  358. docker_options+=$opt_args[$k]
  359. fi
  360. fi
  361. if [[ -n "${relevant_compose_flags[(r)$k]}" ]]; then
  362. if [[ -n "${relevant_compose_repeatable_flags[(r)$k]}" ]]; then
  363. values=("${(@s/:/)opt_args[$k]}")
  364. for value in $values
  365. do
  366. compose_options+=$k
  367. compose_options+=$value
  368. done
  369. else
  370. compose_options+=$k
  371. if [[ -n "$opt_args[$k]" ]]; then
  372. compose_options+=$opt_args[$k]
  373. fi
  374. fi
  375. fi
  376. done
  377. case $state in
  378. (command)
  379. __docker-compose_commands && ret=0
  380. ;;
  381. (option-or-argument)
  382. curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
  383. __docker-compose_subcommand && ret=0
  384. ;;
  385. esac
  386. return ret
  387. }
  388. _docker-compose "$@"