_docker-compose 18 KB

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