_docker-compose 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <[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 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. '--help[Print usage]' \
  144. '--no-cache[Do not use cache when building the image]' \
  145. '*:services:__docker-compose_services_from_build' && ret=0
  146. ;;
  147. (help)
  148. _arguments ':subcommand:__docker-compose_commands' && ret=0
  149. ;;
  150. (kill)
  151. _arguments \
  152. '--help[Print usage]' \
  153. '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
  154. '*:running services:__docker-compose_runningservices' && ret=0
  155. ;;
  156. (logs)
  157. _arguments \
  158. '--help[Print usage]' \
  159. '--no-color[Produce monochrome output.]' \
  160. '*:services:__docker-compose_services_all' && ret=0
  161. ;;
  162. (migrate-to-labels)
  163. _arguments -A '-*' \
  164. '--help[Print usage]' \
  165. '(-):Recreate containers to add labels' && ret=0
  166. ;;
  167. (port)
  168. _arguments \
  169. '--help[Print usage]' \
  170. '--protocol=-[tcp or udap (defaults to tcp)]:protocol:(tcp udp)' \
  171. '--index=-[index of the container if there are mutiple instances of a service (defaults to 1)]:index: ' \
  172. '1:running services:__docker-compose_runningservices' \
  173. '2:port:_ports' && ret=0
  174. ;;
  175. (ps)
  176. _arguments \
  177. '--help[Print usage]' \
  178. '-q[Only display IDs]' \
  179. '*:services:__docker-compose_services_all' && ret=0
  180. ;;
  181. (pull)
  182. _arguments \
  183. '--help[Print usage]' \
  184. '*:services:__docker-compose_services_from_image' && ret=0
  185. ;;
  186. (rm)
  187. _arguments \
  188. '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
  189. '--help[Print usage]' \
  190. '-v[Remove volumes associated with containers]' \
  191. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  192. ;;
  193. (run)
  194. _arguments \
  195. '-d[Detached mode: Run container in the background, print new container name.]' \
  196. '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
  197. '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
  198. '--help[Print usage]' \
  199. '(-u --user)'{-u,--user=-}'[Run as specified username or uid]:username or uid:_users' \
  200. "--no-deps[Don't start linked services.]" \
  201. '--rm[Remove container after run. Ignored in detached mode.]' \
  202. "--publish[Run command with manually mapped container's port(s) to the host.]" \
  203. "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
  204. '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
  205. '(-):services:__docker-compose_services' \
  206. '(-):command: _command_names -e' \
  207. '*::arguments: _normal' && ret=0
  208. ;;
  209. (scale)
  210. _arguments \
  211. '--help[Print usage]' \
  212. '*:running services:__docker-compose_runningservices' && ret=0
  213. ;;
  214. (start)
  215. _arguments \
  216. '--help[Print usage]' \
  217. '*:stopped services:__docker-compose_stoppedservices' && ret=0
  218. ;;
  219. (stop|restart)
  220. _arguments \
  221. '--help[Print usage]' \
  222. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  223. '*:running services:__docker-compose_runningservices' && ret=0
  224. ;;
  225. (up)
  226. _arguments \
  227. '-d[Detached mode: Run containers in the background, print new container names.]' \
  228. '--help[Print usage]' \
  229. '--no-color[Produce monochrome output.]' \
  230. "--no-deps[Don't start linked services.]" \
  231. "--no-recreate[If containers already exist, don't recreate them.]" \
  232. "--force-recreate[Recreate containers even if their configuration and image haven't changed]" \
  233. "--no-build[Don't build an image, even if it's missing]" \
  234. '(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: " \
  235. '*:services:__docker-compose_services_all' && ret=0
  236. ;;
  237. (version)
  238. _arguments \
  239. '--help[Print usage]' \
  240. "--short[Shows only Compose's version number.]" && ret=0
  241. ;;
  242. (*)
  243. _message 'Unknown sub command'
  244. esac
  245. return ret
  246. }
  247. _docker-compose () {
  248. # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
  249. # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
  250. if [[ $service != docker-compose ]]; then
  251. _call_function - _$service
  252. return
  253. fi
  254. local curcontext="$curcontext" state line ret=1
  255. typeset -A opt_args
  256. _arguments -C \
  257. '(- :)'{-h,--help}'[Get help]' \
  258. '--verbose[Show more output]' \
  259. '(- :)'{-v,--version}'[Print version and exit]' \
  260. '(-f --file)'{-f,--file}'[Specify an alternate docker-compose file (default: docker-compose.yml)]:file:_files -g "*.yml"' \
  261. '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
  262. '(-): :->command' \
  263. '(-)*:: :->option-or-argument' && ret=0
  264. local counter=1
  265. #local compose_file compose_project
  266. while [ $counter -lt ${#words[@]} ]; do
  267. case "${words[$counter]}" in
  268. -f|--file)
  269. (( counter++ ))
  270. compose_file="${words[$counter]}"
  271. ;;
  272. -p|--project-name)
  273. (( counter++ ))
  274. compose_project="${words[$counter]}"
  275. ;;
  276. *)
  277. ;;
  278. esac
  279. (( counter++ ))
  280. done
  281. case $state in
  282. (command)
  283. __docker-compose_commands && ret=0
  284. ;;
  285. (option-or-argument)
  286. curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
  287. __docker-compose_subcommand && ret=0
  288. ;;
  289. esac
  290. return ret
  291. }
  292. _docker-compose "$@"