_docker-compose 13 KB

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