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