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