1 #compdef docker-machine
4 # zsh completion for docker-machine
5 # https://github.com/leonhartX/docker-machine-zsh-completion
6 # -------------------------------------------------------------------------
10 # -------------------------------------------------------------------------
13 # * Ke Xu <leonhartx.k@gmail.com>
14 # -------------------------------------------------------------------------
17 # * @sdurrheimer docker-compose-zsh-completion https://github.com/sdurrheimer/docker-compose-zsh-completion
18 # * @ilkka _docker-machine
21 __docker-machine_get_hosts() {
22 [[ $PREFIX = -* ]] && return 1
26 if [[ $state != all ]]; then
27 hosts=(${(f)"$(_call_program commands docker-machine ls -q --filter state=$state)"})
29 hosts=(${(f)"$(_call_program commands docker-machine ls -q)"})
31 _describe 'host' hosts "$@" && ret=0
35 __docker-machine_hosts_with_state() {
37 hosts=(${(f)"$(_call_program commands docker-machine ls -f '{{.Name}}\:{{.DriverName}}\({{.State}}\)\ {{.URL}}')"})
38 _describe 'host' hosts
41 __docker-machine_hosts_all() {
42 __docker-machine_get_hosts all "$@"
45 __docker-machine_hosts_running() {
46 __docker-machine_get_hosts Running "$@"
49 __docker-machine_get_swarm() {
51 swarms=(${(f)"$(_call_program commands docker-machine ls -f {{.Swarm}} | awk '{print $1}')"})
52 _describe 'swarm' swarms
55 __docker-machine_hosts_and_files() {
56 _alternative "hosts:host:__docker-machine_hosts_all -qS ':'" 'files:files:_path_files'
59 __docker-machine_filters() {
60 [[ $PREFIX = -* ]] && return 1
63 if compset -P '*='; then
64 case "${${words[-1]%=*}#*=}" in
66 _describe -t driver-filter-opts "driver filter" opts_driver && ret=0
69 __docker-machine_get_swarm && ret=0
72 opts_state=('Running' 'Paused' 'Saved' 'Stopped' 'Stopping' 'Starting' 'Error')
73 _describe -t state-filter-opts "state filter" opts_state && ret=0
76 __docker-machine_hosts_all && ret=0
79 _message 'label' && ret=0
82 _message 'value' && ret=0
86 opts=('driver' 'swarm' 'state' 'name' 'label')
87 _describe -t filter-opts "filter" opts -qS "=" && ret=0
92 __get_swarm_discovery() {
93 declare -a masters services
96 masters=($(docker-machine ls -f {{.Swarm}} |grep '(master)' |awk '{print $1}'))
97 for master in $masters; do
98 service=${${${(f)"$(_call_program commands docker-machine inspect -f '{{.HostOptions.SwarmOptions.Discovery}}:{{.Name}}' $master)"}/:/\\:}}
99 services=($services $service)
101 _describe -t services "swarm service" services && ret=0
105 __get_create_argument() {
106 typeset -g docker_machine_driver
107 if [[ CURRENT -le 2 ]]; then
108 docker_machine_driver="none"
109 elif [[ CURRENT > 2 && $words[CURRENT-2] = '-d' || $words[CURRENT-2] = '--driver' ]]; then
110 docker_machine_driver=$words[CURRENT-1]
111 elif [[ $words[CURRENT-1] =~ '^(-d|--driver)=' ]]; then
112 docker_machine_driver=${${words[CURRENT-1]}/*=/}
115 local -a opts_provider opts_common opts_read_argument
121 '(--driver -d)'{--driver=,-d=}'[Driver to create machine with]:dirver:->driver-option' \
122 '--engine-install-url=[Custom URL to use for engine installation]:url' \
123 '*--engine-opt=[Specify arbitrary flags to include with the created engine in the form flag=value]:flag' \
124 '*--engine-insecure-registry=[Specify insecure registries to allow with the created engine]:registry' \
125 '*--engine-registry-mirror=[Specify registry mirrors to use]:mirror' \
126 '*--engine-label=[Specify labels for the created engine]:label' \
127 '--engine-storage-driver=[Specify a storage driver to use with the engine]:storage-driver:->storage-driver-option' \
128 '*--engine-env=[Specify environment variables to set in the engine]:environment' \
129 '--swarm[Configure Machine with Swarm]' \
130 '--swarm-image=[Specify Docker image to use for Swarm]:image' \
131 '--swarm-master[Configure Machine to be a Swarm master]' \
132 '--swarm-discovery=[Discovery service to use with Swarm]:service:->swarm-service' \
133 '--swarm-strategy=[Define a default scheduling strategy for Swarm]:strategy:(spread binpack random)' \
134 '*--swarm-opt=[Define arbitrary flags for swarm]:flag' \
135 '*--swarm-join-opt=[Define arbitrary flags for Swarm join]:flag' \
136 '--swarm-host=[ip/socket to listen on for Swarm master]:host' \
137 '--swarm-addr=[addr to advertise for Swarm (default: detect and use the machine IP)]:address' \
138 '--swarm-experimental[Enable Swarm experimental features]' \
139 '*--tls-san=[Support extra SANs for TLS certs]:option'
141 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 \"]\"}'"
142 if [[ $docker_machine_driver != "none" ]]; then
143 opts_provider=(${(f)"$(_call_program commands $driver_opt_cmd)"})
146 $opts_read_argument \
147 $opts_common && ret=0
149 _arguments $opts_common && ret=0
153 _describe -t driver-option "driver" opts_driver && ret=0
155 (storage-driver-option)
156 _describe -t storage-driver-option "storage driver" opts_storage_driver && ret=0
159 __get_swarm_discovery && ret=0
169 __docker-machine_subcommand() {
171 opts_help=("(- :)--help[Print usage]")
172 local -a opts_only_host opts_driver opts_storage_driver opts_state
175 "*:host:__docker-machine_hosts_all"
177 opts_driver=('amazonec2' 'azure' 'digitalocean' 'exoscale' 'generic' 'google' 'hyperv' 'none' 'openstack' 'rackspace' 'softlayer' 'virtualbox' 'vmwarefusion' 'vmwarevcloudair' 'vmwarevsphere')
178 opts_storage_driver=('overlay' 'aufs' 'btrfs' 'devicemapper' 'vfs' 'zfs')
185 '(--timeout -t)'{--timeout=,-t=}'[Timeout in seconds, default to 10s]:seconds' && ret=0
190 '--swarm[Display the Swarm config instead of the Docker daemon]' \
191 "*:host:__docker-machine_hosts_all" && ret=0
194 __get_create_argument
199 '--swarm[Display the Swarm config instead of the Docker daemon]' \
200 '--shell=[Force environment to be configured for a specified shell: \[fish, cmd, powershell\], default is auto-detect]:shell' \
201 '(--unset -u)'{--unset,-u}'[Unset variables instead of setting them]' \
202 '--no-proxy[Add machine IP to NO_PROXY environment variable]' \
203 '*:host:__docker-machine_hosts_running' && ret=0
206 _arguments ':subcommand:__docker-machine_commands' && ret=0
211 '(--format -f)'{--format=,-f=}'[Format the output using the given go template]:template' \
212 '*:host:__docker-machine_hosts_all' && ret=0
217 '*:host:__docker-machine_hosts_running' && ret=0
222 '*:host:__docker-machine_hosts_with_state' && ret=0
227 '(--quiet -q)'{--quiet,-q}'[Enable quiet mode]' \
228 '*--filter=[Filter output based on conditions provided]:filter:->filter-options' \
229 '(--timeout -t)'{--timeout=,-t=}'[Timeout in seconds, default to 10s]:seconds' \
230 '(--format -f)'{--format=,-f=}'[Pretty-print machines using a Go template]:template' && ret=0
233 __docker-machine_filters && ret=0
238 _arguments $opts_only_host && ret=0
243 '(--force -f)'{--force,-f}'[Force rebuild and do not prompt]' \
244 '*:host:__docker-machine_hosts_all' && ret=0
249 '*:host:__docker-machine_hosts_with_state' && ret=0
254 '(--force -f)'{--force,-f}'[Remove local configuration even if machine cannot be removed, also implies an automatic yes (`-y`)]' \
255 '-y[Assumes automatic yes to proceed with remove, without prompting further user confirmation]' \
256 '*:host:__docker-machine_hosts_with_state' && ret=0
261 '(--recursive -r)'{--recursive,-r}'[Copy files recursively (required to copy directories))]' \
262 '*:files:__docker-machine_hosts_and_files' && ret=0
267 '*:host:__docker-machine_hosts_running' && ret=0
272 '*:host:__docker-machine_hosts_with_state' && ret=0
275 _arguments $opts_only_host && ret=0
280 '*:host:__docker-machine_hosts_with_state' && ret=0
283 _arguments $opts_only_host && ret=0
288 '*:host:__docker-machine_hosts_running' && ret=0
296 __docker-machine_commands() {
299 zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
300 if [[ -z "$cache_policy" ]]; then
301 zstyle ":completion:${curcontext}:" cache-policy __docker-machine_caching_policy
304 if ( [[ ${+_docker_machine_subcommands} -eq 0 ]] || _cache_invalid docker_machine_subcommands) \
305 && ! _retrieve_cache docker_machine_subcommands;
308 lines=(${(f)"$(_call_program commands docker-machine 2>&1)"})
309 _docker_machine_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I) *]}]}## #}/$'\t'##/:})
310 (( $#_docker_machine_subcommands > 0 )) && _store_cache docker_machine_subcommands _docker_machine_subcommands
312 _describe -t docker-machine-commands "docker-machine command" _docker_machine_subcommands
315 __docker-machine_caching_policy() {
321 if [[ $service != docker-machine ]]; then
322 _call_function - _$service
326 local curcontext="$curcontext" state line
331 "(- :)"{-h,--help}"[Show help]" \
332 "(-D --debug)"{-D,--debug}"[Enable debug mode]" \
333 '(-s --storage-path)'{-s,--storage-path}'[Configures storage path]:file:_files' \
334 '--tls-ca-cert[CA to verify remotes against]:file:_files' \
335 '--tls-ca-key[Private key to generate certificates]:file:_files' \
336 '--tls-client-cert[Client cert to use for TLS]:file:_files' \
337 '--tls-client-key[Private key used in client TLS auth]:file:_files' \
338 '--github-api-token[Token to use for requests to the Github API]' \
339 '--native-ssh[Use the native (Go-based) SSH implementation.]' \
340 '--bugsnag-api-token[BugSnag API token for crash reporting]' \
341 '(- :)'{-v,--version}'[Print the version]' \
343 "(-)*:: :->option-or-argument" && ret=0
347 __docker-machine_commands && ret=0
350 curcontext=${curcontext%:*:*}:docker-machine-$words[1]:
351 __docker-machine_subcommand && ret=0