_docker-compose 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. (bundle)
  106. _arguments \
  107. $opts_help \
  108. '--push-images[Automatically push images for any services which have a `build` option specified.]' \
  109. '(--output -o)'{--output,-o}'[Path to write the bundle file to. Defaults to "<project name>.dab".]:file:_files' && ret=0
  110. ;;
  111. (config)
  112. _arguments \
  113. $opts_help \
  114. '(--quiet -q)'{--quiet,-q}"[Only validate the configuration, don't print anything.]" \
  115. '--resolve-image-digests[Pin image tags to digests.]' \
  116. '--services[Print the service names, one per line.]' \
  117. '--volumes[Print the volume names, one per line.]' \
  118. '--hash[Print the service config hash, one per line. Set "service1,service2" for a list of specified services.]' \ && ret=0
  119. ;;
  120. (create)
  121. _arguments \
  122. $opts_help \
  123. $opts_force_recreate \
  124. $opts_no_recreate \
  125. $opts_no_build \
  126. "(--no-build)--build[Build images before creating containers.]" \
  127. '*:services:__docker-compose_services' && ret=0
  128. ;;
  129. (down)
  130. _arguments \
  131. $opts_help \
  132. $opts_timeout \
  133. "--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)" \
  134. '(-v --volumes)'{-v,--volumes}"[Remove named volumes declared in the \`volumes\` section of the Compose file and anonymous volumes attached to containers.]" \
  135. $opts_remove_orphans && ret=0
  136. ;;
  137. (events)
  138. _arguments \
  139. $opts_help \
  140. '--json[Output events as a stream of json objects]' \
  141. '*:services:__docker-compose_services' && ret=0
  142. ;;
  143. (exec)
  144. _arguments \
  145. $opts_help \
  146. '-d[Detached mode: Run command in the background.]' \
  147. '--privileged[Give extended privileges to the process.]' \
  148. '(-u --user)'{-u,--user=}'[Run the command as this user.]:username:_users' \
  149. '-T[Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY.]' \
  150. '--index=[Index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
  151. '*'{-e,--env}'[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  152. '(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
  153. '(-):running services:__docker-compose_runningservices' \
  154. '(-):command: _command_names -e' \
  155. '*::arguments: _normal' && ret=0
  156. ;;
  157. (help)
  158. _arguments ':subcommand:__docker-compose_commands' && ret=0
  159. ;;
  160. (images)
  161. _arguments \
  162. $opts_help \
  163. '-q[Only display IDs]' \
  164. '*:services:__docker-compose_services' && ret=0
  165. ;;
  166. (kill)
  167. _arguments \
  168. $opts_help \
  169. '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
  170. '*:running services:__docker-compose_runningservices' && ret=0
  171. ;;
  172. (logs)
  173. _arguments \
  174. $opts_help \
  175. '(-f --follow)'{-f,--follow}'[Follow log output]' \
  176. $opts_no_color \
  177. '--tail=[Number of lines to show from the end of the logs for each container.]:number of lines: ' \
  178. '(-t --timestamps)'{-t,--timestamps}'[Show timestamps]' \
  179. '*:services:__docker-compose_services' && ret=0
  180. ;;
  181. (pause)
  182. _arguments \
  183. $opts_help \
  184. '*:running services:__docker-compose_runningservices' && ret=0
  185. ;;
  186. (port)
  187. _arguments \
  188. $opts_help \
  189. '--protocol=[tcp or udp \[default: tcp\]]:protocol:(tcp udp)' \
  190. '--index=[index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
  191. '1:running services:__docker-compose_runningservices' \
  192. '2:port:_ports' && ret=0
  193. ;;
  194. (ps)
  195. _arguments \
  196. $opts_help \
  197. '-q[Only display IDs]' \
  198. '--filter KEY=VAL[Filter services by a property]:<filtername>=<value>:' \
  199. '*:services:__docker-compose_services' && ret=0
  200. ;;
  201. (pull)
  202. _arguments \
  203. $opts_help \
  204. '--ignore-pull-failures[Pull what it can and ignores images with pull failures.]' \
  205. '--no-parallel[Disable parallel pulling]' \
  206. '(-q --quiet)'{-q,--quiet}'[Pull without printing progress information]' \
  207. '--include-deps[Also pull services declared as dependencies]' \
  208. '*:services:__docker-compose_services_from_image' && ret=0
  209. ;;
  210. (push)
  211. _arguments \
  212. $opts_help \
  213. '--ignore-push-failures[Push what it can and ignores images with push failures.]' \
  214. '*:services:__docker-compose_services' && ret=0
  215. ;;
  216. (rm)
  217. _arguments \
  218. $opts_help \
  219. '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
  220. '-v[Remove any anonymous volumes attached to containers]' \
  221. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  222. ;;
  223. (run)
  224. _arguments \
  225. $opts_help \
  226. $opts_no_deps \
  227. '-d[Detached mode: Run container in the background, print new container name.]' \
  228. '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  229. '*'{-l,--label}'[KEY=VAL Add or override a label (can be used multiple times)]:label KEY=VAL: ' \
  230. '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
  231. '--name=[Assign a name to the container]:name: ' \
  232. '(-p --publish)'{-p,--publish=}"[Publish a container's port(s) to the host]" \
  233. '--rm[Remove container after run. Ignored in detached mode.]' \
  234. "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
  235. '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
  236. '(-u --user)'{-u,--user=}'[Run as specified username or uid]:username or uid:_users' \
  237. '(-v --volume)*'{-v,--volume=}'[Bind mount a volume]:volume: ' \
  238. '(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
  239. "--use-aliases[Use the services network aliases in the network(s) the container connects to]" \
  240. '(-):services:__docker-compose_services' \
  241. '(-):command: _command_names -e' \
  242. '*::arguments: _normal' && ret=0
  243. ;;
  244. (scale)
  245. _arguments \
  246. $opts_help \
  247. $opts_timeout \
  248. '*:running services:__docker-compose_runningservices' && ret=0
  249. ;;
  250. (start)
  251. _arguments \
  252. $opts_help \
  253. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  254. ;;
  255. (stop|restart)
  256. _arguments \
  257. $opts_help \
  258. $opts_timeout \
  259. '*:running services:__docker-compose_runningservices' && ret=0
  260. ;;
  261. (top)
  262. _arguments \
  263. $opts_help \
  264. '*:running services:__docker-compose_runningservices' && ret=0
  265. ;;
  266. (unpause)
  267. _arguments \
  268. $opts_help \
  269. '*:paused services:__docker-compose_pausedservices' && ret=0
  270. ;;
  271. (up)
  272. _arguments \
  273. $opts_help \
  274. '(--abort-on-container-exit)-d[Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit.]' \
  275. $opts_no_color \
  276. $opts_no_deps \
  277. $opts_force_recreate \
  278. $opts_no_recreate \
  279. $opts_no_build \
  280. "(--no-build)--build[Build images before starting containers.]" \
  281. "(-d)--abort-on-container-exit[Stops all containers if any container was stopped. Incompatible with -d.]" \
  282. '(-t --timeout)'{-t,--timeout}"[Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)]:seconds: " \
  283. '--scale[SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.]:service scale SERVICE=NUM: ' \
  284. '--exit-code-from=[Return the exit code of the selected service container. Implies --abort-on-container-exit]:service:__docker-compose_services' \
  285. $opts_remove_orphans \
  286. '*:services:__docker-compose_services' && ret=0
  287. ;;
  288. (version)
  289. _arguments \
  290. $opts_help \
  291. "--short[Shows only Compose's version number.]" && ret=0
  292. ;;
  293. (*)
  294. _message 'Unknown sub command' && ret=1
  295. ;;
  296. esac
  297. return ret
  298. }
  299. _docker-compose() {
  300. # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
  301. # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
  302. if [[ $service != docker-compose ]]; then
  303. _call_function - _$service
  304. return
  305. fi
  306. local curcontext="$curcontext" state line
  307. integer ret=1
  308. typeset -A opt_args
  309. local file_description
  310. if [[ -n ${words[(r)-f]} || -n ${words[(r)--file]} ]] ; then
  311. file_description="Specify an override docker-compose file (default: docker-compose.override.yml)"
  312. else
  313. file_description="Specify an alternate docker-compose file (default: docker-compose.yml)"
  314. fi
  315. _arguments -C \
  316. '(- :)'{-h,--help}'[Get help]' \
  317. '*'{-f,--file}"[${file_description}]:file:_files -g '*.yml'" \
  318. '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
  319. "--compatibility[If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent]" \
  320. '(- :)'{-v,--version}'[Print version and exit]' \
  321. '--verbose[Show more output]' \
  322. '--log-level=[Set log level]:level:(DEBUG INFO WARNING ERROR CRITICAL)' \
  323. '--no-ansi[Do not print ANSI control characters]' \
  324. '(-H --host)'{-H,--host}'[Daemon socket to connect to]:host:' \
  325. '--tls[Use TLS; implied by --tlsverify]' \
  326. '--tlscacert=[Trust certs signed only by this CA]:ca path:' \
  327. '--tlscert=[Path to TLS certificate file]:client cert path:' \
  328. '--tlskey=[Path to TLS key file]:tls key path:' \
  329. '--tlsverify[Use TLS and verify the remote]' \
  330. "--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)]" \
  331. '(-): :->command' \
  332. '(-)*:: :->option-or-argument' && ret=0
  333. local -a relevant_compose_flags relevant_compose_repeatable_flags relevant_docker_flags compose_options docker_options
  334. relevant_compose_flags=(
  335. "--file" "-f"
  336. "--host" "-H"
  337. "--project-name" "-p"
  338. "--tls"
  339. "--tlscacert"
  340. "--tlscert"
  341. "--tlskey"
  342. "--tlsverify"
  343. "--skip-hostname-check"
  344. )
  345. relevant_compose_repeatable_flags=(
  346. "--file" "-f"
  347. )
  348. relevant_docker_flags=(
  349. "--host" "-H"
  350. "--tls"
  351. "--tlscacert"
  352. "--tlscert"
  353. "--tlskey"
  354. "--tlsverify"
  355. )
  356. for k in "${(@k)opt_args}"; do
  357. if [[ -n "${relevant_docker_flags[(r)$k]}" ]]; then
  358. docker_options+=$k
  359. if [[ -n "$opt_args[$k]" ]]; then
  360. docker_options+=$opt_args[$k]
  361. fi
  362. fi
  363. if [[ -n "${relevant_compose_flags[(r)$k]}" ]]; then
  364. if [[ -n "${relevant_compose_repeatable_flags[(r)$k]}" ]]; then
  365. values=("${(@s/:/)opt_args[$k]}")
  366. for value in $values
  367. do
  368. compose_options+=$k
  369. compose_options+=$value
  370. done
  371. else
  372. compose_options+=$k
  373. if [[ -n "$opt_args[$k]" ]]; then
  374. compose_options+=$opt_args[$k]
  375. fi
  376. fi
  377. fi
  378. done
  379. case $state in
  380. (command)
  381. __docker-compose_commands && ret=0
  382. ;;
  383. (option-or-argument)
  384. curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
  385. __docker-compose_subcommand && ret=0
  386. ;;
  387. esac
  388. return ret
  389. }
  390. _docker-compose "$@"