_docker-compose 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 <s.durrheimer@gmail.com>
  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. '(--output -o)'{--output,-o}'[Path to write the bundle file to. Defaults to "<project name>.dab".]:file:_files' && ret=0
  181. ;;
  182. (config)
  183. _arguments \
  184. $opts_help \
  185. '(--quiet -q)'{--quiet,-q}"[Only validate the configuration, don't print anything.]" \
  186. '--services[Print the service names, one per line.]' && ret=0
  187. ;;
  188. (create)
  189. _arguments \
  190. $opts_help \
  191. $opts_force_recreate \
  192. $opts_no_recreate \
  193. $opts_no_build \
  194. "(--no-build)--build[Build images before creating containers.]" \
  195. '*:services:__docker-compose_services_all' && ret=0
  196. ;;
  197. (down)
  198. _arguments \
  199. $opts_help \
  200. "--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)" \
  201. '(-v --volumes)'{-v,--volumes}"[Remove named volumes declared in the \`volumes\` section of the Compose file and anonymous volumes attached to containers.]" \
  202. $opts_remove_orphans && ret=0
  203. ;;
  204. (events)
  205. _arguments \
  206. $opts_help \
  207. '--json[Output events as a stream of json objects]' \
  208. '*:services:__docker-compose_services_all' && ret=0
  209. ;;
  210. (exec)
  211. _arguments \
  212. $opts_help \
  213. '-d[Detached mode: Run command in the background.]' \
  214. '--privileged[Give extended privileges to the process.]' \
  215. '--user=[Run the command as this user.]:username:_users' \
  216. '-T[Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY.]' \
  217. '--index=[Index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
  218. '(-):running services:__docker-compose_runningservices' \
  219. '(-):command: _command_names -e' \
  220. '*::arguments: _normal' && ret=0
  221. ;;
  222. (help)
  223. _arguments ':subcommand:__docker-compose_commands' && ret=0
  224. ;;
  225. (kill)
  226. _arguments \
  227. $opts_help \
  228. '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
  229. '*:running services:__docker-compose_runningservices' && ret=0
  230. ;;
  231. (logs)
  232. _arguments \
  233. $opts_help \
  234. '(-f --follow)'{-f,--follow}'[Follow log output]' \
  235. $opts_no_color \
  236. '--tail=[Number of lines to show from the end of the logs for each container.]:number of lines: ' \
  237. '(-t --timestamps)'{-t,--timestamps}'[Show timestamps]' \
  238. '*:services:__docker-compose_services_all' && ret=0
  239. ;;
  240. (pause)
  241. _arguments \
  242. $opts_help \
  243. '*:running services:__docker-compose_runningservices' && ret=0
  244. ;;
  245. (port)
  246. _arguments \
  247. $opts_help \
  248. '--protocol=[tcp or udp \[default: tcp\]]:protocol:(tcp udp)' \
  249. '--index=[index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
  250. '1:running services:__docker-compose_runningservices' \
  251. '2:port:_ports' && ret=0
  252. ;;
  253. (ps)
  254. _arguments \
  255. $opts_help \
  256. '-q[Only display IDs]' \
  257. '*:services:__docker-compose_services_all' && ret=0
  258. ;;
  259. (pull)
  260. _arguments \
  261. $opts_help \
  262. '--ignore-pull-failures[Pull what it can and ignores images with pull failures.]' \
  263. '*:services:__docker-compose_services_from_image' && ret=0
  264. ;;
  265. (push)
  266. _arguments \
  267. $opts_help \
  268. '--ignore-push-failures[Push what it can and ignores images with push failures.]' \
  269. '*:services:__docker-compose_services' && ret=0
  270. ;;
  271. (rm)
  272. _arguments \
  273. $opts_help \
  274. '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
  275. '-v[Remove any anonymous volumes attached to containers]' \
  276. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  277. ;;
  278. (run)
  279. _arguments \
  280. $opts_help \
  281. '-d[Detached mode: Run container in the background, print new container name.]' \
  282. '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  283. '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
  284. '--name=[Assign a name to the container]:name: ' \
  285. $opts_no_deps \
  286. '(-p --publish)'{-p,--publish=}"[Publish a container's port(s) to the host]" \
  287. '--rm[Remove container after run. Ignored in detached mode.]' \
  288. "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
  289. '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
  290. '(-u --user)'{-u,--user=}'[Run as specified username or uid]:username or uid:_users' \
  291. '(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
  292. '(-):services:__docker-compose_services' \
  293. '(-):command: _command_names -e' \
  294. '*::arguments: _normal' && ret=0
  295. ;;
  296. (scale)
  297. _arguments \
  298. $opts_help \
  299. $opts_timeout \
  300. '*:running services:__docker-compose_runningservices' && ret=0
  301. ;;
  302. (start)
  303. _arguments \
  304. $opts_help \
  305. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  306. ;;
  307. (stop|restart)
  308. _arguments \
  309. $opts_help \
  310. $opts_timeout \
  311. '*:running services:__docker-compose_runningservices' && ret=0
  312. ;;
  313. (unpause)
  314. _arguments \
  315. $opts_help \
  316. '*:paused services:__docker-compose_pausedservices' && ret=0
  317. ;;
  318. (up)
  319. _arguments \
  320. $opts_help \
  321. '(--abort-on-container-exit)-d[Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit.]' \
  322. $opts_no_color \
  323. $opts_no_deps \
  324. $opts_force_recreate \
  325. $opts_no_recreate \
  326. $opts_no_build \
  327. "(--no-build)--build[Build images before starting containers.]" \
  328. "(-d)--abort-on-container-exit[Stops all containers if any container was stopped. Incompatible with -d.]" \
  329. '(-t --timeout)'{-t,--timeout}"[Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)]:seconds: " \
  330. $opts_remove_orphans \
  331. '*:services:__docker-compose_services_all' && ret=0
  332. ;;
  333. (version)
  334. _arguments \
  335. $opts_help \
  336. "--short[Shows only Compose's version number.]" && ret=0
  337. ;;
  338. (*)
  339. _message 'Unknown sub command' && ret=1
  340. ;;
  341. esac
  342. return ret
  343. }
  344. _docker-compose() {
  345. # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
  346. # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
  347. if [[ $service != docker-compose ]]; then
  348. _call_function - _$service
  349. return
  350. fi
  351. local curcontext="$curcontext" state line
  352. integer ret=1
  353. typeset -A opt_args
  354. _arguments -C \
  355. '(- :)'{-h,--help}'[Get help]' \
  356. '(-f --file)'{-f,--file}'[Specify an alternate docker-compose file (default: docker-compose.yml)]:file:_files -g "*.yml"' \
  357. '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
  358. '--verbose[Show more output]' \
  359. '(- :)'{-v,--version}'[Print version and exit]' \
  360. '(-H --host)'{-H,--host}'[Daemon socket to connect to]:host:' \
  361. '--tls[Use TLS; implied by --tlsverify]' \
  362. '--tlscacert=[Trust certs signed only by this CA]:ca path:' \
  363. '--tlscert=[Path to TLS certificate file]:client cert path:' \
  364. '--tlskey=[Path to TLS key file]:tls key path:' \
  365. '--tlsverify[Use TLS and verify the remote]' \
  366. "--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)]" \
  367. '(-): :->command' \
  368. '(-)*:: :->option-or-argument' && ret=0
  369. local -a relevant_compose_flags relevant_docker_flags compose_options docker_options
  370. relevant_compose_flags=(
  371. "--file" "-f"
  372. "--host" "-H"
  373. "--project-name" "-p"
  374. "--tls"
  375. "--tlscacert"
  376. "--tlscert"
  377. "--tlskey"
  378. "--tlsverify"
  379. "--skip-hostname-check"
  380. )
  381. relevant_docker_flags=(
  382. "--host" "-H"
  383. "--tls"
  384. "--tlscacert"
  385. "--tlscert"
  386. "--tlskey"
  387. "--tlsverify"
  388. )
  389. for k in "${(@k)opt_args}"; do
  390. if [[ -n "${relevant_docker_flags[(r)$k]}" ]]; then
  391. docker_options+=$k
  392. if [[ -n "$opt_args[$k]" ]]; then
  393. docker_options+=$opt_args[$k]
  394. fi
  395. fi
  396. if [[ -n "${relevant_compose_flags[(r)$k]}" ]]; then
  397. compose_options+=$k
  398. if [[ -n "$opt_args[$k]" ]]; then
  399. compose_options+=$opt_args[$k]
  400. fi
  401. fi
  402. done
  403. case $state in
  404. (command)
  405. __docker-compose_commands && ret=0
  406. ;;
  407. (option-or-argument)
  408. curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
  409. __docker-compose_subcommand && ret=0
  410. ;;
  411. esac
  412. return ret
  413. }
  414. _docker-compose "$@"