_docker-compose 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. # 0.1.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. # 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 fig.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. services=$(___docker-compose_all_services_in_compose_file)
  43. _alternative "args:services:($services)"
  44. }
  45. # All services that have an entry with the given key in their docker-compose.yml section
  46. ___docker-compose_services_with_key() {
  47. local already_selected
  48. local -a buildable
  49. already_selected=$(echo ${words[@]} | tr " " "|")
  50. # flatten sections to one line, then filter lines containing the key and return section name.
  51. 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"
  52. }
  53. # All services that are defined by a Dockerfile reference
  54. __docker-compose_services_from_build() {
  55. buildable=$(___docker-compose_services_with_key build)
  56. _alternative "args:buildable services:($buildable)"
  57. }
  58. # All services that are defined by an image
  59. __docker-compose_services_from_image() {
  60. pullable=$(___docker-compose_services_with_key image)
  61. _alternative "args:pullable services:($pullable)"
  62. }
  63. __docker-compose_get_services() {
  64. local kind expl
  65. declare -a running stopped lines args services
  66. docker_status=$(docker ps > /dev/null 2>&1)
  67. if [ $? -ne 0 ]; then
  68. _message "Error! Docker is not running."
  69. return 1
  70. fi
  71. kind=$1
  72. shift
  73. [[ $kind = (stopped|all) ]] && args=($args -a)
  74. lines=(${(f)"$(_call_program commands docker ps ${args})"})
  75. services=(${(f)"$(_call_program commands docker-compose 2>/dev/null ${compose_file:+-f $compose_file} ${compose_project:+-p $compose_project} ps -q)"})
  76. # Parse header line to find columns
  77. local i=1 j=1 k header=${lines[1]}
  78. declare -A begin end
  79. while (( $j < ${#header} - 1 )) {
  80. i=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 1))
  81. j=$(( $i + ${${header[$i,-1]}[(i) ]} - 1))
  82. k=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 2))
  83. begin[${header[$i,$(($j-1))]}]=$i
  84. end[${header[$i,$(($j-1))]}]=$k
  85. }
  86. lines=(${lines[2,-1]})
  87. # Container ID
  88. local line s name
  89. local -a names
  90. for line in $lines; do
  91. if [[ $services == *"${line[${begin[CONTAINER ID]},${end[CONTAINER ID]}]%% ##}"* ]]; then
  92. names=(${(ps:,:)${${line[${begin[NAMES]},-1]}%% *}})
  93. for name in $names; do
  94. s="${${name%_*}#*_}:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}"
  95. s="$s, ${line[${begin[CONTAINER ID]},${end[CONTAINER ID]}]%% ##}"
  96. s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}"
  97. if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then
  98. stopped=($stopped $s)
  99. else
  100. running=($running $s)
  101. fi
  102. done
  103. fi
  104. done
  105. [[ $kind = (running|all) ]] && _describe -t services-running "running services" running
  106. [[ $kind = (stopped|all) ]] && _describe -t services-stopped "stopped services" stopped
  107. }
  108. __docker-compose_stoppedservices() {
  109. __docker-compose_get_services stopped "$@"
  110. }
  111. __docker-compose_runningservices() {
  112. __docker-compose_get_services running "$@"
  113. }
  114. __docker-compose_services () {
  115. __docker-compose_get_services all "$@"
  116. }
  117. __docker-compose_caching_policy() {
  118. oldp=( "$1"(Nmh+1) ) # 1 hour
  119. (( $#oldp ))
  120. }
  121. __docker-compose_commands () {
  122. local cache_policy
  123. zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
  124. if [[ -z "$cache_policy" ]]; then
  125. zstyle ":completion:${curcontext}:" cache-policy __docker-compose_caching_policy
  126. fi
  127. if ( [[ ${+_docker_compose_subcommands} -eq 0 ]] || _cache_invalid docker_compose_subcommands) \
  128. && ! _retrieve_cache docker_compose_subcommands;
  129. then
  130. local -a lines
  131. lines=(${(f)"$(_call_program commands docker-compose 2>&1)"})
  132. _docker_compose_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I) *]}]}## #}/ ##/:})
  133. _store_cache docker_compose_subcommands _docker_compose_subcommands
  134. fi
  135. _describe -t docker-compose-commands "docker-compose command" _docker_compose_subcommands
  136. }
  137. __docker-compose_subcommand () {
  138. local -a _command_args
  139. integer ret=1
  140. case "$words[1]" in
  141. (build)
  142. _arguments \
  143. '--no-cache[Do not use cache when building the image]' \
  144. '*:services:__docker-compose_services_from_build' && ret=0
  145. ;;
  146. (help)
  147. _arguments ':subcommand:__docker-compose_commands' && ret=0
  148. ;;
  149. (kill)
  150. _arguments \
  151. '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
  152. '*:running services:__docker-compose_runningservices' && ret=0
  153. ;;
  154. (logs)
  155. _arguments \
  156. '--no-color[Produce monochrome output.]' \
  157. '*:services:__docker-compose_services_all' && ret=0
  158. ;;
  159. (migrate-to-labels)
  160. _arguments \
  161. '(-):Recreate containers to add labels' && ret=0
  162. ;;
  163. (port)
  164. _arguments \
  165. '--protocol=-[tcp or udap (defaults to tcp)]:protocol:(tcp udp)' \
  166. '--index=-[index of the container if there are mutiple instances of a service (defaults to 1)]:index: ' \
  167. '1:running services:__docker-compose_runningservices' \
  168. '2:port:_ports' && ret=0
  169. ;;
  170. (ps)
  171. _arguments \
  172. '-q[Only display IDs]' \
  173. '*:services:__docker-compose_services_all' && ret=0
  174. ;;
  175. (pull)
  176. _arguments \
  177. '--allow-insecure-ssl[Allow insecure connections to the docker registry]' \
  178. '*:services:__docker-compose_services_from_image' && ret=0
  179. ;;
  180. (rm)
  181. _arguments \
  182. '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
  183. '-v[Remove volumes associated with containers]' \
  184. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  185. ;;
  186. (run)
  187. _arguments \
  188. '--allow-insecure-ssl[Allow insecure connections to the docker registry]' \
  189. '-d[Detached mode: Run container in the background, print new container name.]' \
  190. '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
  191. '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  192. '(-u --user)'{-u,--user=-}'[Run as specified username or uid]:username or uid:_users' \
  193. "--no-deps[Don't start linked services.]" \
  194. '--rm[Remove container after run. Ignored in detached mode.]' \
  195. "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
  196. '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
  197. '(-):services:__docker-compose_services' \
  198. '(-):command: _command_names -e' \
  199. '*::arguments: _normal' && ret=0
  200. ;;
  201. (scale)
  202. _arguments '*:running services:__docker-compose_runningservices' && ret=0
  203. ;;
  204. (start)
  205. _arguments '*:stopped services:__docker-compose_stoppedservices' && ret=0
  206. ;;
  207. (stop|restart)
  208. _arguments \
  209. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  210. '*:running services:__docker-compose_runningservices' && ret=0
  211. ;;
  212. (up)
  213. _arguments \
  214. '--allow-insecure-ssl[Allow insecure connections to the docker registry]' \
  215. '-d[Detached mode: Run containers in the background, print new container names.]' \
  216. '--no-color[Produce monochrome output.]' \
  217. "--no-deps[Don't start linked services.]" \
  218. "--no-recreate[If containers already exist, don't recreate them.]" \
  219. "--no-build[Don't build an image, even if it's missing]" \
  220. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  221. "--x-smart-recreate[Only recreate containers whose configuration or image needs to be updated. (EXPERIMENTAL)]" \
  222. '*:services:__docker-compose_services_all' && ret=0
  223. ;;
  224. (version)
  225. _arguments \
  226. "--short[Shows only Compose's version number.]" && ret=0
  227. ;;
  228. (*)
  229. _message 'Unknown sub command'
  230. esac
  231. return ret
  232. }
  233. _docker-compose () {
  234. # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
  235. # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
  236. if [[ $service != docker-compose ]]; then
  237. _call_function - _$service
  238. return
  239. fi
  240. local curcontext="$curcontext" state line ret=1
  241. typeset -A opt_args
  242. _arguments -C \
  243. '(- :)'{-h,--help}'[Get help]' \
  244. '--verbose[Show more output]' \
  245. '(- :)'{-v,--version}'[Print version and exit]' \
  246. '(-f --file)'{-f,--file}'[Specify an alternate docker-compose file (default: docker-compose.yml)]:file:_files -g "*.yml"' \
  247. '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
  248. '(-): :->command' \
  249. '(-)*:: :->option-or-argument' && ret=0
  250. local counter=1
  251. #local compose_file compose_project
  252. while [ $counter -lt ${#words[@]} ]; do
  253. case "${words[$counter]}" in
  254. -f|--file)
  255. (( counter++ ))
  256. compose_file="${words[$counter]}"
  257. ;;
  258. -p|--project-name)
  259. (( counter++ ))
  260. compose_project="${words[$counter]}"
  261. ;;
  262. *)
  263. ;;
  264. esac
  265. (( counter++ ))
  266. done
  267. case $state in
  268. (command)
  269. __docker-compose_commands && ret=0
  270. ;;
  271. (option-or-argument)
  272. curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
  273. __docker-compose_subcommand && ret=0
  274. ;;
  275. esac
  276. return ret
  277. }
  278. _docker-compose "$@"