_docker-compose 18 KB

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