_docker-compose 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. _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. (help)
  179. _arguments ':subcommand:__docker-compose_commands' && ret=0
  180. ;;
  181. (kill)
  182. _arguments \
  183. $opts_help \
  184. '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
  185. '*:running services:__docker-compose_runningservices' && ret=0
  186. ;;
  187. (logs)
  188. _arguments \
  189. $opts_help \
  190. '--no-color[Produce monochrome output.]' \
  191. '*:services:__docker-compose_services_all' && ret=0
  192. ;;
  193. (pause)
  194. _arguments \
  195. $opts_help \
  196. '*:running services:__docker-compose_runningservices' && ret=0
  197. ;;
  198. (port)
  199. _arguments \
  200. $opts_help \
  201. '--protocol=-[tcp or udap (defaults to tcp)]:protocol:(tcp udp)' \
  202. '--index=-[index of the container if there are mutiple instances of a service (defaults to 1)]:index: ' \
  203. '1:running services:__docker-compose_runningservices' \
  204. '2:port:_ports' && ret=0
  205. ;;
  206. (ps)
  207. _arguments \
  208. $opts_help \
  209. '-q[Only display IDs]' \
  210. '*:services:__docker-compose_services_all' && ret=0
  211. ;;
  212. (pull)
  213. _arguments \
  214. $opts_help \
  215. '--ignore-pull-failures[Pull what it can and ignores images with pull failures.]' \
  216. '*:services:__docker-compose_services_from_image' && ret=0
  217. ;;
  218. (rm)
  219. _arguments \
  220. $opts_help \
  221. '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
  222. '-v[Remove volumes associated with containers]' \
  223. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  224. ;;
  225. (run)
  226. _arguments \
  227. $opts_help \
  228. '-d[Detached mode: Run container in the background, print new container name.]' \
  229. '--name[Assign a name to the container]:name: ' \
  230. '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
  231. '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  232. '(-u --user)'{-u,--user=-}'[Run as specified username or uid]:username or uid:_users' \
  233. "--no-deps[Don't start linked services.]" \
  234. '--rm[Remove container after run. Ignored in detached mode.]' \
  235. "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
  236. '(-p --publish)'{-p,--publish=-}"[Run command with manually mapped container's port(s) to the host.]" \
  237. '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
  238. '(-):services:__docker-compose_services' \
  239. '(-):command: _command_names -e' \
  240. '*::arguments: _normal' && ret=0
  241. ;;
  242. (scale)
  243. _arguments \
  244. $opts_help \
  245. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  246. '*:running services:__docker-compose_runningservices' && ret=0
  247. ;;
  248. (start)
  249. _arguments \
  250. $opts_help \
  251. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  252. ;;
  253. (stop|restart)
  254. _arguments \
  255. $opts_help \
  256. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  257. '*:running services:__docker-compose_runningservices' && ret=0
  258. ;;
  259. (unpause)
  260. _arguments \
  261. $opts_help \
  262. '*:paused services:__docker-compose_pausedservices' && ret=0
  263. ;;
  264. (up)
  265. _arguments \
  266. $opts_help \
  267. '-d[Detached mode: Run containers in the background, print new container names.]' \
  268. '--no-color[Produce monochrome output.]' \
  269. "--no-deps[Don't start linked services.]" \
  270. "--force-recreate[Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.]" \
  271. "--no-recreate[If containers already exist, don't recreate them.]" \
  272. "--no-build[Don't build an image, even if it's missing]" \
  273. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  274. '*:services:__docker-compose_services_all' && ret=0
  275. ;;
  276. (version)
  277. _arguments \
  278. $opts_help \
  279. "--short[Shows only Compose's version number.]" && ret=0
  280. ;;
  281. (*)
  282. _message 'Unknown sub command' && ret=1
  283. ;;
  284. esac
  285. return ret
  286. }
  287. _docker-compose() {
  288. # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
  289. # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
  290. if [[ $service != docker-compose ]]; then
  291. _call_function - _$service
  292. return
  293. fi
  294. local curcontext="$curcontext" state line
  295. integer ret=1
  296. typeset -A opt_args
  297. _arguments -C \
  298. '(- :)'{-h,--help}'[Get help]' \
  299. '--verbose[Show more output]' \
  300. '(- :)'{-v,--version}'[Print version and exit]' \
  301. '(-f --file)'{-f,--file}'[Specify an alternate docker-compose file (default: docker-compose.yml)]:file:_files -g "*.yml"' \
  302. '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
  303. '(-): :->command' \
  304. '(-)*:: :->option-or-argument' && ret=0
  305. local compose_file=${opt_args[-f]}${opt_args[--file]}
  306. local compose_project=${opt_args[-p]}${opt_args[--project-name]}
  307. local compose_options="${compose_file:+--file $compose_file} ${compose_project:+--project-name $compose_project}"
  308. case $state in
  309. (command)
  310. __docker-compose_commands && ret=0
  311. ;;
  312. (option-or-argument)
  313. curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
  314. __docker-compose_subcommand && ret=0
  315. ;;
  316. esac
  317. return ret
  318. }
  319. _docker-compose "$@"