_docker-compose 16 KB

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