_docker-machine 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #compdef docker-machine
  2. # Description
  3. # -----------
  4. # zsh completion for docker-machine
  5. # https://github.com/leonhartX/docker-machine-zsh-completion
  6. # -------------------------------------------------------------------------
  7. # Version
  8. # -------
  9. # 0.1.1
  10. # -------------------------------------------------------------------------
  11. # Authors
  12. # -------
  13. # * Ke Xu <leonhartx.k@gmail.com>
  14. # -------------------------------------------------------------------------
  15. # Inspiration
  16. # -----------
  17. # * @sdurrheimer docker-compose-zsh-completion https://github.com/sdurrheimer/docker-compose-zsh-completion
  18. # * @ilkka _docker-machine
  19. __docker-machine_get_hosts() {
  20. [[ $PREFIX = -* ]] && return 1
  21. local state
  22. declare -a hosts
  23. state=$1; shift
  24. if [[ $state != all ]]; then
  25. hosts=(${(f)"$(_call_program commands docker-machine ls -q --filter state=$state)"})
  26. else
  27. hosts=(${(f)"$(_call_program commands docker-machine ls -q)"})
  28. fi
  29. _describe 'host' hosts "$@" && ret=0
  30. return ret
  31. }
  32. __docker-machine_hosts_with_state() {
  33. declare -a hosts
  34. hosts=(${(f)"$(_call_program commands docker-machine ls -f '{{.Name}}\:{{.DriverName}}\({{.State}}\)\ {{.URL}}')"})
  35. _describe 'host' hosts
  36. }
  37. __docker-machine_hosts_all() {
  38. __docker-machine_get_hosts all "$@"
  39. }
  40. __docker-machine_hosts_running() {
  41. __docker-machine_get_hosts Running "$@"
  42. }
  43. __docker-machine_get_swarm() {
  44. declare -a swarms
  45. swarms=(${(f)"$(_call_program commands docker-machine ls -f {{.Swarm}} | awk '{print $1}')"})
  46. _describe 'swarm' swarms
  47. }
  48. __docker-machine_hosts_and_files() {
  49. _alternative "hosts:host:__docker-machine_hosts_all -qS ':'" 'files:files:_path_files'
  50. }
  51. __docker-machine_filters() {
  52. [[ $PREFIX = -* ]] && return 1
  53. integer ret=1
  54. if compset -P '*='; then
  55. case "${${words[-1]%=*}#*=}" in
  56. (driver)
  57. _describe -t driver-filter-opts "driver filter" opts_driver && ret=0
  58. ;;
  59. (swarm)
  60. __docker-machine_get_swarm && ret=0
  61. ;;
  62. (state)
  63. opts_state=('Running' 'Paused' 'Saved' 'Stopped' 'Stopping' 'Starting' 'Error')
  64. _describe -t state-filter-opts "state filter" opts_state && ret=0
  65. ;;
  66. (name)
  67. __docker-machine_hosts_all && ret=0
  68. ;;
  69. (label)
  70. _message 'label' && ret=0
  71. ;;
  72. *)
  73. _message 'value' && ret=0
  74. ;;
  75. esac
  76. else
  77. opts=('driver' 'swarm' 'state' 'name' 'label')
  78. _describe -t filter-opts "filter" opts -qS "=" && ret=0
  79. fi
  80. return ret
  81. }
  82. __get_swarm_discovery() {
  83. declare -a masters serivces
  84. local service
  85. services=()
  86. masters=($(docker-machine ls -f {{.Swarm}} |grep '(master)' |awk '{print $1}'))
  87. for master in $masters; do
  88. service=${${${(f)"$(_call_program commands docker-machine inspect -f '{{.HostOptions.SwarmOptions.Discovery}}:{{.Name}}' $master)"}/:/\\:}}
  89. services=($services $service)
  90. done
  91. _describe -t services "swarm service" services && ret=0
  92. return ret
  93. }
  94. __get_create_argument() {
  95. typeset -g docker_machine_driver
  96. if [[ CURRENT -le 2 ]]; then
  97. docker_machine_driver="none"
  98. elif [[ CURRENT > 2 && $words[CURRENT-2] = '-d' || $words[CURRENT-2] = '--driver' ]]; then
  99. docker_machine_driver=$words[CURRENT-1]
  100. elif [[ $words[CURRENT-1] =~ '^(-d|--driver)=' ]]; then
  101. docker_machine_driver=${${words[CURRENT-1]}/*=/}
  102. fi
  103. local driver_opt_cmd
  104. local -a opts_provider opts_common opts_read_argument
  105. opts_read_argument=(
  106. ": :->argument"
  107. )
  108. opts_common=(
  109. $opts_help \
  110. '(--driver -d)'{--driver=,-d=}'[Driver to create machine with]:dirver:->driver-option' \
  111. '--engine-install-url=[Custom URL to use for engine installation]:url' \
  112. '*--engine-opt=[Specify arbitrary flags to include with the created engine in the form flag=value]:flag' \
  113. '*--engine-insecure-registry=[Specify insecure registries to allow with the created engine]:registry' \
  114. '*--engine-registry-mirror=[Specify registry mirrors to use]:mirror' \
  115. '*--engine-label=[Specify labels for the created engine]:label' \
  116. '--engine-storage-driver=[Specify a storage driver to use with the engine]:storage-driver:->storage-driver-option' \
  117. '*--engine-env=[Specify environment variables to set in the engine]:environment' \
  118. '--swarm[Configure Machine with Swarm]' \
  119. '--swarm-image=[Specify Docker image to use for Swarm]:image' \
  120. '--swarm-master[Configure Machine to be a Swarm master]' \
  121. '--swarm-discovery=[Discovery service to use with Swarm]:service:->swarm-service' \
  122. '--swarm-strategy=[Define a default scheduling strategy for Swarm]:strategy:(spread binpack random)' \
  123. '*--swarm-opt=[Define arbitrary flags for swarm]:flag' \
  124. '*--swarm-join-opt=[Define arbitrary flags for Swarm join]:flag' \
  125. '--swarm-host=[ip/socket to listen on for Swarm master]:host' \
  126. '--swarm-addr=[addr to advertise for Swarm (default: detect and use the machine IP)]:address' \
  127. '--swarm-experimental[Enable Swarm experimental features]' \
  128. '*--tls-san=[Support extra SANs for TLS certs]:option'
  129. )
  130. driver_opt_cmd="docker-machine create -d $docker_machine_driver | grep $docker_machine_driver | sed -e 's/\(--.*\)\ *\[\1[^]]*\]/*\1/g' -e 's/\(\[[^]]*\)/\\\\\\1\\\\/g' -e 's/\".*\"\(.*\)/\1/g' | awk '{printf \"%s[\", \$1; for(i=2;i<=NF;i++) {printf \"%s \", \$i}; print \"]\"}'"
  131. if [[ $docker_machine_driver != "none" ]]; then
  132. opts_provider=(${(f)"$(_call_program commands $driver_opt_cmd)"})
  133. _arguments \
  134. $opts_provider \
  135. $opts_read_argument \
  136. $opts_common && ret=0
  137. else
  138. _arguments $opts_common && ret=0
  139. fi
  140. case $state in
  141. (driver-option)
  142. _describe -t driver-option "driver" opts_driver && ret=0
  143. ;;
  144. (storage-driver-option)
  145. _describe -t storage-driver-option "storage driver" opts_storage_driver && ret=0
  146. ;;
  147. (swarm-service)
  148. __get_swarm_discovery && ret=0
  149. ;;
  150. (argument)
  151. ret=0
  152. ;;
  153. esac
  154. return ret
  155. }
  156. __docker-machine_subcommand() {
  157. local -a opts_help
  158. opts_help=("(- :)--help[Print usage]")
  159. local -a opts_only_host opts_driver opts_storage_driver opts_stragery
  160. opts_only_host=(
  161. "$opts_help"
  162. "*:host:__docker-machine_hosts_all"
  163. )
  164. opts_driver=('amazonec2' 'azure' 'digitalocean' 'exoscale' 'generic' 'google' 'hyperv' 'none' 'openstack' 'rackspace' 'softlayer' 'virtualbox' 'vmwarefusion' 'vmwarevcloudair' 'vmwarevsphere')
  165. opts_storage_driver=('overlay' 'aufs' 'btrfs' 'devicemapper' 'vfs' 'zfs')
  166. integer ret=1
  167. case "$words[1]" in
  168. (active)
  169. _arguments \
  170. $opts_help \
  171. '(--timeout -t)'{--timeout=,-t=}'[Timeout in seconds, default to 10s]:seconds' && ret=0
  172. ;;
  173. (config)
  174. _arguments \
  175. $opts_help \
  176. '--swarm[Display the Swarm config instead of the Docker daemon]' \
  177. "*:host:__docker-machine_hosts_all" && ret=0
  178. ;;
  179. (create)
  180. __get_create_argument
  181. ;;
  182. (env)
  183. _arguments \
  184. $opts_help \
  185. '--swarm[Display the Swarm config instead of the Docker daemon]' \
  186. '--shell=[Force environment to be configured for a specified shell: \[fish, cmd, powershell\], default is auto-detect]:shell' \
  187. '(--unset -u)'{--unset,-u}'[Unset variables instead of setting them]' \
  188. '--no-proxy[Add machine IP to NO_PROXY environment variable]' \
  189. '*:host:__docker-machine_hosts_running' && ret=0
  190. ;;
  191. (help)
  192. _arguments ':subcommand:__docker-machine_commands' && ret=0
  193. ;;
  194. (inspect)
  195. _arguments \
  196. $opts_help \
  197. '(--format -f)'{--format=,-f=}'[Format the output using the given go template]:template' \
  198. '*:host:__docker-machine_hosts_all' && ret=0
  199. ;;
  200. (ip)
  201. _arguments \
  202. $opts_help \
  203. '*:host:__docker-machine_hosts_running' && ret=0
  204. ;;
  205. (kill)
  206. _arguments \
  207. $opts_help \
  208. '*:host:__docker-machine_hosts_with_state' && ret=0
  209. ;;
  210. (ls)
  211. _arguments \
  212. $opts_help \
  213. '(--quiet -q)'{--quiet,-q}'[Enable quiet mode]' \
  214. '*--filter=[Filter output based on conditions provided]:filter:->filter-options' \
  215. '(--timeout -t)'{--timeout=,-t=}'[Timeout in seconds, default to 10s]:seconds' \
  216. '(--format -f)'{--format=,-f=}'[Pretty-print machines using a Go template]:template' && ret=0
  217. case $state in
  218. (filter-options)
  219. __docker-machine_filters && ret=0
  220. ;;
  221. esac
  222. ;;
  223. (provision)
  224. _arguments $opts_only_host && ret=0
  225. ;;
  226. (regenerate-certs)
  227. _arguments \
  228. $opts_help \
  229. '(--force -f)'{--force,-f}'[Force rebuild and do not prompt]' \
  230. '*:host:__docker-machine_hosts_all' && ret=0
  231. ;;
  232. (restart)
  233. _arguments \
  234. $opts_help \
  235. '*:host:__docker-machine_hosts_with_state' && ret=0
  236. ;;
  237. (rm)
  238. _arguments \
  239. $opts_help \
  240. '(--force -f)'{--force,-f}'[Remove local configuration even if machine cannot be removed, also implies an automatic yes (`-y`)]' \
  241. '-y[Assumes automatic yes to proceed with remove, without prompting further user confirmation]' \
  242. '*:host:__docker-machine_hosts_with_state' && ret=0
  243. ;;
  244. (scp)
  245. _arguments \
  246. $opts_help \
  247. '(--recursive -r)'{--recursive,-r}'[Copy files recursively (required to copy directories))]' \
  248. '*:files:__docker-machine_hosts_and_files' && ret=0
  249. ;;
  250. (ssh)
  251. _arguments \
  252. $opts_help \
  253. '*:host:__docker-machine_hosts_running' && ret=0
  254. ;;
  255. (start)
  256. _arguments \
  257. $opts_help \
  258. '*:host:__docker-machine_hosts_with_state' && ret=0
  259. ;;
  260. (status)
  261. _arguments $opts_only_host && ret=0
  262. ;;
  263. (stop)
  264. _arguments \
  265. $opts_help \
  266. '*:host:__docker-machine_hosts_with_state' && ret=0
  267. ;;
  268. (upgrade)
  269. _arguments $opts_only_host && ret=0
  270. ;;
  271. (url)
  272. _arguments \
  273. $opts_help \
  274. '*:host:__docker-machine_hosts_running' && ret=0
  275. ;;
  276. esac
  277. return ret
  278. }
  279. __docker-machine_commands() {
  280. local cache_policy
  281. zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
  282. if [[ -z "$cache_policy" ]]; then
  283. zstyle ":completion:${curcontext}:" cache-policy __docker-machine_caching_policy
  284. fi
  285. if ( [[ ${+_docker_machine_subcommands} -eq 0 ]] || _cache_invalid docker_machine_subcommands) \
  286. && ! _retrieve_cache docker_machine_subcommands;
  287. then
  288. local -a lines
  289. lines=(${(f)"$(_call_program commands docker-machine 2>&1)"})
  290. _docker_machine_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I) *]}]}## #}/$'\t'##/:})
  291. (( $#_docker_machine_subcommands > 0 )) && _store_cache docker_machine_subcommands _docker_machine_subcommands
  292. fi
  293. _describe -t docker-machine-commands "docker-machine command" _docker_machine_subcommands
  294. }
  295. __docker-machine_caching_policy() {
  296. oldp=( "$1"(Nmh+1) )
  297. (( $#oldp ))
  298. }
  299. _docker-machine() {
  300. if [[ $service != docker-machine ]]; then
  301. _call_function - _$service
  302. return
  303. fi
  304. local curcontext="$curcontext" state line
  305. integer ret=1
  306. typeset -A opt_args
  307. _arguments -C \
  308. "(- :)"{-h,--help}"[Show help]" \
  309. "(-D --debug)"{-D,--debug}"[Enable debug mode]" \
  310. '(-s --stroage-path)'{-s,--storage-path}'[Configures storage path]:file:_files' \
  311. '--tls-ca-cert[CA to verify remotes against]:file:_files' \
  312. '--tls-ca-key[Private key to generate certificates]:file:_files' \
  313. '--tls-client-cert[Client cert to use for TLS]:file:_files' \
  314. '--tls-client-key[Private key used in client TLS auth]:file:_files' \
  315. '--github-api-token[Token to use for requests to the Github API]' \
  316. '--native-ssh[Use the native (Go-based) SSH implementation.]' \
  317. '--bugsnag-api-token[BugSnag API token for crash reporting]' \
  318. '(- :)'{-v,--version}'[Print the version]' \
  319. "(-): :->command" \
  320. "(-)*:: :->option-or-argument" && ret=0
  321. case $state in
  322. (command)
  323. __docker-machine_commands && ret=0
  324. ;;
  325. (option-or-argument)
  326. curcontext=${curcontext%:*:*}:docker-machine-$words[1]:
  327. __docker-machine_subcommand && ret=0
  328. ret=0
  329. ;;
  330. esac
  331. return ret
  332. }
  333. _docker-machine "$@"