_docker-compose 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. (bundle)
  173. _arguments \
  174. $opts_help \
  175. '(--output -o)'{--output,-o}'[Path to write the bundle file to. Defaults to "<project name>.dab".]:file:_files' && ret=0
  176. ;;
  177. (config)
  178. _arguments \
  179. $opts_help \
  180. '(--quiet -q)'{--quiet,-q}"[Only validate the configuration, don't print anything.]" \
  181. '--services[Print the service names, one per line.]' && ret=0
  182. ;;
  183. (create)
  184. _arguments \
  185. $opts_help \
  186. "(--no-recreate --no-build)--force-recreate[Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.]" \
  187. "(--force-recreate)--no-build[If containers already exist, don't recreate them. Incompatible with --force-recreate.]" \
  188. "(--force-recreate)--no-recreate[Don't build an image, even if it's missing]" \
  189. '*:services:__docker-compose_services_all' && ret=0
  190. ;;
  191. (down)
  192. _arguments \
  193. $opts_help \
  194. "--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)" \
  195. '(-v --volumes)'{-v,--volumes}"[Remove data volumes]" && ret=0
  196. ;;
  197. (events)
  198. _arguments \
  199. $opts_help \
  200. '--json[Output events as a stream of json objects.]' \
  201. '*:services:__docker-compose_services_all' && ret=0
  202. ;;
  203. (exec)
  204. _arguments \
  205. $opts_help \
  206. '-d[Detached mode: Run command in the background.]' \
  207. '--privileged[Give extended privileges to the process.]' \
  208. '--user=[Run the command as this user.]:username:_users' \
  209. '-T[Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY.]' \
  210. '--index=[Index of the container if there are multiple instances of a service (default: 1)]:index: ' \
  211. '(-):running services:__docker-compose_runningservices' \
  212. '(-):command: _command_names -e' \
  213. '*::arguments: _normal' && ret=0
  214. ;;
  215. (help)
  216. _arguments ':subcommand:__docker-compose_commands' && ret=0
  217. ;;
  218. (kill)
  219. _arguments \
  220. $opts_help \
  221. '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
  222. '*:running services:__docker-compose_runningservices' && ret=0
  223. ;;
  224. (logs)
  225. _arguments \
  226. $opts_help \
  227. '(-f --follow)'{-f,--follow}'[Follow log output]' \
  228. '--no-color[Produce monochrome output.]' \
  229. '--tail=[Number of lines to show from the end of the logs for each container.]:number of lines: ' \
  230. '(-t --timestamps)'{-t,--timestamps}'[Show timestamps]' \
  231. '*:services:__docker-compose_services_all' && ret=0
  232. ;;
  233. (pause)
  234. _arguments \
  235. $opts_help \
  236. '*:running services:__docker-compose_runningservices' && ret=0
  237. ;;
  238. (port)
  239. _arguments \
  240. $opts_help \
  241. '--protocol=-[tcp or udap (defaults to tcp)]:protocol:(tcp udp)' \
  242. '--index=-[index of the container if there are mutiple instances of a service (defaults to 1)]:index: ' \
  243. '1:running services:__docker-compose_runningservices' \
  244. '2:port:_ports' && ret=0
  245. ;;
  246. (ps)
  247. _arguments \
  248. $opts_help \
  249. '-q[Only display IDs]' \
  250. '*:services:__docker-compose_services_all' && ret=0
  251. ;;
  252. (pull)
  253. _arguments \
  254. $opts_help \
  255. '--ignore-pull-failures[Pull what it can and ignores images with pull failures.]' \
  256. '*:services:__docker-compose_services_from_image' && ret=0
  257. ;;
  258. (rm)
  259. _arguments \
  260. $opts_help \
  261. '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
  262. '-v[Remove volumes associated with containers]' \
  263. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  264. ;;
  265. (run)
  266. _arguments \
  267. $opts_help \
  268. '-d[Detached mode: Run container in the background, print new container name.]' \
  269. '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  270. '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
  271. '--name[Assign a name to the container]:name: ' \
  272. "--no-deps[Don't start linked services.]" \
  273. '(-p --publish)'{-p,--publish=-}"[Run command with manually mapped container's port(s) to the host.]" \
  274. '--rm[Remove container after run. Ignored in detached mode.]' \
  275. "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
  276. '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
  277. '(-u --user)'{-u,--user=-}'[Run as specified username or uid]:username or uid:_users' \
  278. '(-w --workdir)'{-w=,--workdir=}'[Working directory inside the container]:workdir: ' \
  279. '(-):services:__docker-compose_services' \
  280. '(-):command: _command_names -e' \
  281. '*::arguments: _normal' && ret=0
  282. ;;
  283. (scale)
  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. (start)
  290. _arguments \
  291. $opts_help \
  292. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  293. ;;
  294. (stop|restart)
  295. _arguments \
  296. $opts_help \
  297. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  298. '*:running services:__docker-compose_runningservices' && ret=0
  299. ;;
  300. (unpause)
  301. _arguments \
  302. $opts_help \
  303. '*:paused services:__docker-compose_pausedservices' && ret=0
  304. ;;
  305. (up)
  306. _arguments \
  307. $opts_help \
  308. '(--abort-on-container-exit)-d[Detached mode: Run containers in the background, print new container names.]' \
  309. '--build[Build images before starting containers.]' \
  310. '--no-color[Produce monochrome output.]' \
  311. "--no-deps[Don't start linked services.]" \
  312. "--force-recreate[Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.]" \
  313. "--no-recreate[If containers already exist, don't recreate them.]" \
  314. "--no-build[Don't build an image, even if it's missing]" \
  315. "(-d)--abort-on-container-exit[Stops all containers if any container was stopped. Incompatible with -d.]" \
  316. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  317. '*:services:__docker-compose_services_all' && ret=0
  318. ;;
  319. (version)
  320. _arguments \
  321. $opts_help \
  322. "--short[Shows only Compose's version number.]" && ret=0
  323. ;;
  324. (*)
  325. _message 'Unknown sub command' && ret=1
  326. ;;
  327. esac
  328. return ret
  329. }
  330. _docker-compose() {
  331. # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
  332. # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
  333. if [[ $service != docker-compose ]]; then
  334. _call_function - _$service
  335. return
  336. fi
  337. local curcontext="$curcontext" state line
  338. integer ret=1
  339. typeset -A opt_args
  340. _arguments -C \
  341. '(- :)'{-h,--help}'[Get help]' \
  342. '--verbose[Show more output]' \
  343. '(- :)'{-v,--version}'[Print version and exit]' \
  344. '(-f --file)'{-f,--file}'[Specify an alternate docker-compose file (default: docker-compose.yml)]:file:_files -g "*.yml"' \
  345. '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
  346. '(-): :->command' \
  347. '(-)*:: :->option-or-argument' && ret=0
  348. local compose_file=${opt_args[-f]}${opt_args[--file]}
  349. local compose_project=${opt_args[-p]}${opt_args[--project-name]}
  350. local compose_options="${compose_file:+--file $compose_file} ${compose_project:+--project-name $compose_project}"
  351. case $state in
  352. (command)
  353. __docker-compose_commands && ret=0
  354. ;;
  355. (option-or-argument)
  356. curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
  357. __docker-compose_subcommand && ret=0
  358. ;;
  359. esac
  360. return ret
  361. }
  362. _docker-compose "$@"