]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/docker-machine/_docker-machine
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / docker-machine / _docker-machine
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
20
21 __docker-machine_get_hosts() {
22     [[ $PREFIX = -* ]] && return 1
23     local state
24     declare -a hosts
25     state=$1; shift
26     if [[ $state != all ]]; then
27         hosts=(${(f)"$(_call_program commands docker-machine ls -q --filter state=$state)"})
28     else
29         hosts=(${(f)"$(_call_program commands docker-machine ls -q)"})
30     fi
31     _describe 'host' hosts "$@" && ret=0
32     return ret
33 }
34
35 __docker-machine_hosts_with_state() {
36     declare -a hosts
37     hosts=(${(f)"$(_call_program commands docker-machine ls -f '{{.Name}}\:{{.DriverName}}\({{.State}}\)\ {{.URL}}')"})
38     _describe 'host' hosts
39 }
40
41 __docker-machine_hosts_all() {
42     __docker-machine_get_hosts all "$@"
43 }
44
45 __docker-machine_hosts_running() {
46     __docker-machine_get_hosts Running "$@"
47 }
48
49 __docker-machine_get_swarm() {
50     declare -a swarms
51     swarms=(${(f)"$(_call_program commands docker-machine ls -f {{.Swarm}} | awk '{print $1}')"})
52     _describe 'swarm' swarms
53 }
54
55 __docker-machine_hosts_and_files() {
56     _alternative "hosts:host:__docker-machine_hosts_all -qS ':'" 'files:files:_path_files'
57 }
58
59 __docker-machine_filters() {
60     [[ $PREFIX = -* ]] && return 1
61     integer ret=1
62
63     if compset -P '*='; then
64         case "${${words[-1]%=*}#*=}" in
65             (driver)
66                 _describe -t driver-filter-opts "driver filter" opts_driver && ret=0
67                 ;;
68             (swarm)
69                 __docker-machine_get_swarm && ret=0
70                 ;;
71             (state)
72                 opts_state=('Running' 'Paused' 'Saved' 'Stopped' 'Stopping' 'Starting' 'Error')
73                 _describe -t state-filter-opts "state filter" opts_state && ret=0
74                 ;;
75             (name)
76                 __docker-machine_hosts_all && ret=0
77                 ;;
78             (label)
79                 _message 'label' && ret=0
80                 ;;
81             *)
82                 _message 'value' && ret=0
83                 ;;
84         esac
85     else
86         opts=('driver' 'swarm' 'state' 'name' 'label')
87         _describe -t filter-opts "filter" opts -qS "=" && ret=0
88     fi
89     return ret
90 }
91
92 __get_swarm_discovery() {
93     declare -a masters services
94     local service
95     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)
100     done
101     _describe -t services "swarm service" services && ret=0
102     return ret
103 }
104
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]}/*=/}
113     fi
114     local driver_opt_cmd
115     local -a opts_provider opts_common opts_read_argument
116     opts_read_argument=(
117         ": :->argument"
118     )
119     opts_common=(
120         $opts_help \
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'
140     )
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)"})
144         _arguments \
145             $opts_provider \
146             $opts_read_argument \
147             $opts_common && ret=0
148     else
149         _arguments $opts_common && ret=0
150     fi
151     case $state in
152         (driver-option)
153             _describe -t driver-option "driver" opts_driver && ret=0
154             ;;
155         (storage-driver-option)
156             _describe -t storage-driver-option "storage driver" opts_storage_driver && ret=0
157             ;;
158         (swarm-service)
159             __get_swarm_discovery && ret=0
160             ;;
161         (argument)
162             ret=0
163             ;;
164     esac
165     return ret
166 }
167
168
169 __docker-machine_subcommand() {
170     local -a opts_help
171     opts_help=("(- :)--help[Print usage]")
172     local -a opts_only_host opts_driver opts_storage_driver opts_state
173     opts_only_host=(
174         "$opts_help"
175         "*:host:__docker-machine_hosts_all"
176     )
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')
179     integer ret=1
180
181     case "$words[1]" in
182         (active)
183             _arguments \
184                 $opts_help \
185                 '(--timeout -t)'{--timeout=,-t=}'[Timeout in seconds, default to 10s]:seconds' && ret=0
186             ;;
187         (config)
188             _arguments \
189                 $opts_help \
190                 '--swarm[Display the Swarm config instead of the Docker daemon]' \
191                 "*:host:__docker-machine_hosts_all" && ret=0
192             ;;
193         (create)
194             __get_create_argument
195            ;;
196         (env)
197             _arguments \
198                 $opts_help \
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
204             ;;
205         (help)
206             _arguments ':subcommand:__docker-machine_commands' && ret=0
207             ;;
208         (inspect)
209             _arguments \
210                 $opts_help \
211                 '(--format -f)'{--format=,-f=}'[Format the output using the given go template]:template' \
212                 '*:host:__docker-machine_hosts_all' && ret=0
213             ;;
214         (ip)
215             _arguments \
216                 $opts_help \
217                 '*:host:__docker-machine_hosts_running' && ret=0
218             ;;
219         (kill)
220              _arguments \
221                 $opts_help \
222                 '*:host:__docker-machine_hosts_with_state' && ret=0
223             ;;
224         (ls)
225             _arguments \
226                 $opts_help \
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
231             case $state in
232                 (filter-options)
233                     __docker-machine_filters && ret=0
234                     ;;
235             esac
236             ;;
237         (provision)
238             _arguments $opts_only_host && ret=0
239             ;;
240         (regenerate-certs)
241             _arguments \
242                 $opts_help \
243                 '(--force -f)'{--force,-f}'[Force rebuild and do not prompt]' \
244                 '*:host:__docker-machine_hosts_all' && ret=0
245             ;;
246         (restart)
247             _arguments \
248                 $opts_help \
249                 '*:host:__docker-machine_hosts_with_state' && ret=0
250             ;;
251         (rm)
252             _arguments \
253                 $opts_help \
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
257             ;;
258         (scp)
259             _arguments \
260                 $opts_help \
261                 '(--recursive -r)'{--recursive,-r}'[Copy files recursively (required to copy directories))]' \
262                 '*:files:__docker-machine_hosts_and_files' && ret=0
263             ;;
264         (ssh)
265             _arguments \
266                 $opts_help \
267                 '*:host:__docker-machine_hosts_running' && ret=0
268             ;;
269         (start)
270             _arguments \
271                 $opts_help \
272                 '*:host:__docker-machine_hosts_with_state' && ret=0
273             ;;
274         (status)
275             _arguments $opts_only_host && ret=0
276             ;;
277         (stop)
278             _arguments \
279                 $opts_help \
280                 '*:host:__docker-machine_hosts_with_state' && ret=0
281             ;;
282         (upgrade)
283             _arguments $opts_only_host && ret=0
284             ;;
285         (url)
286             _arguments \
287                 $opts_help \
288                 '*:host:__docker-machine_hosts_running' && ret=0
289             ;;
290     esac
291
292     return ret
293 }
294
295
296 __docker-machine_commands() {
297     local cache_policy
298
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
302     fi
303
304     if ( [[ ${+_docker_machine_subcommands} -eq 0 ]] || _cache_invalid docker_machine_subcommands) \
305         && ! _retrieve_cache docker_machine_subcommands;
306     then
307         local -a lines
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
311     fi
312     _describe -t docker-machine-commands "docker-machine command" _docker_machine_subcommands
313 }
314
315 __docker-machine_caching_policy() {
316     oldp=( "$1"(Nmh+1) )
317     (( $#oldp ))
318 }
319
320 _docker-machine() {
321     if [[ $service != docker-machine ]]; then
322         _call_function - _$service
323         return
324     fi
325
326     local curcontext="$curcontext" state line
327     integer ret=1
328     typeset -A opt_args
329
330     _arguments -C \
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]' \
342         "(-): :->command" \
343         "(-)*:: :->option-or-argument" && ret=0
344
345     case $state in
346         (command)
347             __docker-machine_commands && ret=0
348             ;;
349         (option-or-argument)
350             curcontext=${curcontext%:*:*}:docker-machine-$words[1]:
351             __docker-machine_subcommand && ret=0
352             ret=0
353             ;;
354     esac
355
356     return ret
357 }
358
359 _docker-machine "$@"