1
0

docker-compose 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #!bash
  2. #
  3. # bash completion for docker-compose
  4. #
  5. # This work is based on the completion for the docker command.
  6. #
  7. # This script provides completion of:
  8. # - commands and their options
  9. # - service names
  10. # - filepaths
  11. #
  12. # To enable the completions either:
  13. # - place this file in /etc/bash_completion.d
  14. # or
  15. # - copy this file to e.g. ~/.docker-compose-completion.sh and add the line
  16. # below to your .bashrc after bash completion features are loaded
  17. # . ~/.docker-compose-completion.sh
  18. # For compatibility reasons, Compose and therefore its completion supports several
  19. # stack compositon files as listed here, in descending priority.
  20. # Support for these filenames might be dropped in some future version.
  21. __docker_compose_compose_file() {
  22. local file
  23. for file in docker-compose.y{,a}ml fig.y{,a}ml ; do
  24. [ -e $file ] && {
  25. echo $file
  26. return
  27. }
  28. done
  29. echo docker-compose.yml
  30. }
  31. # Extracts all service names from the compose file.
  32. ___docker_compose_all_services_in_compose_file() {
  33. awk -F: '/^[a-zA-Z0-9]/{print $1}' "${compose_file:-$(__docker_compose_compose_file)}" 2>/dev/null
  34. }
  35. # All services, even those without an existing container
  36. __docker_compose_services_all() {
  37. COMPREPLY=( $(compgen -W "$(___docker_compose_all_services_in_compose_file)" -- "$cur") )
  38. }
  39. # All services that have an entry with the given key in their compose_file section
  40. ___docker_compose_services_with_key() {
  41. # flatten sections to one line, then filter lines containing the key and return section name.
  42. 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}'
  43. }
  44. # All services that are defined by a Dockerfile reference
  45. __docker_compose_services_from_build() {
  46. COMPREPLY=( $(compgen -W "$(___docker_compose_services_with_key build)" -- "$cur") )
  47. }
  48. # All services that are defined by an image
  49. __docker_compose_services_from_image() {
  50. COMPREPLY=( $(compgen -W "$(___docker_compose_services_with_key image)" -- "$cur") )
  51. }
  52. # The services for which containers have been created, optionally filtered
  53. # by a boolean expression passed in as argument.
  54. __docker_compose_services_with() {
  55. local containers names
  56. containers="$(docker-compose 2>/dev/null ${compose_file:+-f $compose_file} ${compose_project:+-p $compose_project} ps -q)"
  57. names=( $(docker 2>/dev/null inspect --format "{{if ${1:-true}}} {{ .Name }} {{end}}" $containers) )
  58. names=( ${names[@]%_*} ) # strip trailing numbers
  59. names=( ${names[@]#*_} ) # strip project name
  60. COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") )
  61. }
  62. # The services for which at least one paused container exists
  63. __docker_compose_services_paused() {
  64. __docker_compose_services_with '.State.Paused'
  65. }
  66. # The services for which at least one running container exists
  67. __docker_compose_services_running() {
  68. __docker_compose_services_with '.State.Running'
  69. }
  70. # The services for which at least one stopped container exists
  71. __docker_compose_services_stopped() {
  72. __docker_compose_services_with 'not .State.Running'
  73. }
  74. _docker_compose_build() {
  75. case "$cur" in
  76. -*)
  77. COMPREPLY=( $( compgen -W "--help --no-cache --pull" -- "$cur" ) )
  78. ;;
  79. *)
  80. __docker_compose_services_from_build
  81. ;;
  82. esac
  83. }
  84. _docker_compose_docker_compose() {
  85. case "$prev" in
  86. --file|-f)
  87. _filedir "y?(a)ml"
  88. return
  89. ;;
  90. --project-name|-p)
  91. return
  92. ;;
  93. --x-network-driver)
  94. COMPREPLY=( $( compgen -W "bridge host none overlay" -- "$cur" ) )
  95. return
  96. ;;
  97. esac
  98. case "$cur" in
  99. -*)
  100. COMPREPLY=( $( compgen -W "--file -f --help -h --project-name -p --verbose --version -v --x-networking --x-network-driver" -- "$cur" ) )
  101. ;;
  102. *)
  103. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  104. ;;
  105. esac
  106. }
  107. _docker_compose_help() {
  108. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  109. }
  110. _docker_compose_kill() {
  111. case "$prev" in
  112. -s)
  113. COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) )
  114. return
  115. ;;
  116. esac
  117. case "$cur" in
  118. -*)
  119. COMPREPLY=( $( compgen -W "--help -s" -- "$cur" ) )
  120. ;;
  121. *)
  122. __docker_compose_services_running
  123. ;;
  124. esac
  125. }
  126. _docker_compose_logs() {
  127. case "$cur" in
  128. -*)
  129. COMPREPLY=( $( compgen -W "--help --no-color" -- "$cur" ) )
  130. ;;
  131. *)
  132. __docker_compose_services_all
  133. ;;
  134. esac
  135. }
  136. _docker_compose_migrate_to_labels() {
  137. case "$cur" in
  138. -*)
  139. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  140. ;;
  141. esac
  142. }
  143. _docker_compose_pause() {
  144. case "$cur" in
  145. -*)
  146. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  147. ;;
  148. *)
  149. __docker_compose_services_running
  150. ;;
  151. esac
  152. }
  153. _docker_compose_port() {
  154. case "$prev" in
  155. --protocol)
  156. COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) )
  157. return;
  158. ;;
  159. --index)
  160. return;
  161. ;;
  162. esac
  163. case "$cur" in
  164. -*)
  165. COMPREPLY=( $( compgen -W "--help --index --protocol" -- "$cur" ) )
  166. ;;
  167. *)
  168. __docker_compose_services_all
  169. ;;
  170. esac
  171. }
  172. _docker_compose_ps() {
  173. case "$cur" in
  174. -*)
  175. COMPREPLY=( $( compgen -W "--help -q" -- "$cur" ) )
  176. ;;
  177. *)
  178. __docker_compose_services_all
  179. ;;
  180. esac
  181. }
  182. _docker_compose_pull() {
  183. case "$cur" in
  184. -*)
  185. COMPREPLY=( $( compgen -W "--help --ignore-pull-failures" -- "$cur" ) )
  186. ;;
  187. *)
  188. __docker_compose_services_from_image
  189. ;;
  190. esac
  191. }
  192. _docker_compose_restart() {
  193. case "$prev" in
  194. --timeout|-t)
  195. return
  196. ;;
  197. esac
  198. case "$cur" in
  199. -*)
  200. COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
  201. ;;
  202. *)
  203. __docker_compose_services_running
  204. ;;
  205. esac
  206. }
  207. _docker_compose_rm() {
  208. case "$cur" in
  209. -*)
  210. COMPREPLY=( $( compgen -W "--force -f --help -v" -- "$cur" ) )
  211. ;;
  212. *)
  213. __docker_compose_services_stopped
  214. ;;
  215. esac
  216. }
  217. _docker_compose_run() {
  218. case "$prev" in
  219. -e)
  220. COMPREPLY=( $( compgen -e -- "$cur" ) )
  221. compopt -o nospace
  222. return
  223. ;;
  224. --entrypoint|--name|--user|-u)
  225. return
  226. ;;
  227. esac
  228. case "$cur" in
  229. -*)
  230. COMPREPLY=( $( compgen -W "-d --entrypoint -e --help --name --no-deps --publish -p --rm --service-ports -T --user -u" -- "$cur" ) )
  231. ;;
  232. *)
  233. __docker_compose_services_all
  234. ;;
  235. esac
  236. }
  237. _docker_compose_scale() {
  238. case "$prev" in
  239. =)
  240. COMPREPLY=("$cur")
  241. return
  242. ;;
  243. --timeout|-t)
  244. return
  245. ;;
  246. esac
  247. case "$cur" in
  248. -*)
  249. COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
  250. ;;
  251. *)
  252. COMPREPLY=( $(compgen -S "=" -W "$(___docker_compose_all_services_in_compose_file)" -- "$cur") )
  253. compopt -o nospace
  254. ;;
  255. esac
  256. }
  257. _docker_compose_start() {
  258. case "$cur" in
  259. -*)
  260. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  261. ;;
  262. *)
  263. __docker_compose_services_stopped
  264. ;;
  265. esac
  266. }
  267. _docker_compose_stop() {
  268. case "$prev" in
  269. --timeout|-t)
  270. return
  271. ;;
  272. esac
  273. case "$cur" in
  274. -*)
  275. COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
  276. ;;
  277. *)
  278. __docker_compose_services_running
  279. ;;
  280. esac
  281. }
  282. _docker_compose_unpause() {
  283. case "$cur" in
  284. -*)
  285. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  286. ;;
  287. *)
  288. __docker_compose_services_paused
  289. ;;
  290. esac
  291. }
  292. _docker_compose_up() {
  293. case "$prev" in
  294. --timeout|-t)
  295. return
  296. ;;
  297. esac
  298. case "$cur" in
  299. -*)
  300. COMPREPLY=( $( compgen -W "-d --help --no-build --no-color --no-deps --no-recreate --force-recreate --timeout -t" -- "$cur" ) )
  301. ;;
  302. *)
  303. __docker_compose_services_all
  304. ;;
  305. esac
  306. }
  307. _docker_compose_version() {
  308. case "$cur" in
  309. -*)
  310. COMPREPLY=( $( compgen -W "--short" -- "$cur" ) )
  311. ;;
  312. esac
  313. }
  314. _docker_compose() {
  315. local previous_extglob_setting=$(shopt -p extglob)
  316. shopt -s extglob
  317. local commands=(
  318. build
  319. help
  320. kill
  321. logs
  322. migrate-to-labels
  323. pause
  324. port
  325. ps
  326. pull
  327. restart
  328. rm
  329. run
  330. scale
  331. start
  332. stop
  333. unpause
  334. up
  335. version
  336. )
  337. COMPREPLY=()
  338. local cur prev words cword
  339. _get_comp_words_by_ref -n : cur prev words cword
  340. # search subcommand and invoke its handler.
  341. # special treatment of some top-level options
  342. local command='docker_compose'
  343. local counter=1
  344. local compose_file compose_project
  345. while [ $counter -lt $cword ]; do
  346. case "${words[$counter]}" in
  347. --file|-f)
  348. (( counter++ ))
  349. compose_file="${words[$counter]}"
  350. ;;
  351. --project-name|p)
  352. (( counter++ ))
  353. compose_project="${words[$counter]}"
  354. ;;
  355. --x-network-driver)
  356. (( counter++ ))
  357. ;;
  358. -*)
  359. ;;
  360. *)
  361. command="${words[$counter]}"
  362. break
  363. ;;
  364. esac
  365. (( counter++ ))
  366. done
  367. local completions_func=_docker_compose_${command//-/_}
  368. declare -F $completions_func >/dev/null && $completions_func
  369. eval "$previous_extglob_setting"
  370. return 0
  371. }
  372. complete -F _docker_compose docker-compose