_docker (19367B)
1 #compdef docker 2 # 3 # zsh completion for docker (http://docker.com) 4 # 5 # version: 0.3.0 6 # github: https://github.com/felixr/docker-zsh-completion 7 # 8 # contributors: 9 # - Felix Riedel 10 # - Vincent Bernat 11 # 12 # license: 13 # 14 # Copyright (c) 2013, Felix Riedel 15 # All rights reserved. 16 # 17 # Redistribution and use in source and binary forms, with or without 18 # modification, are permitted provided that the following conditions are met: 19 # * Redistributions of source code must retain the above copyright 20 # notice, this list of conditions and the following disclaimer. 21 # * Redistributions in binary form must reproduce the above copyright 22 # notice, this list of conditions and the following disclaimer in the 23 # documentation and/or other materials provided with the distribution. 24 # * Neither the name of the <organization> nor the 25 # names of its contributors may be used to endorse or promote products 26 # derived from this software without specific prior written permission. 27 # 28 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 29 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 30 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 32 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 33 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 34 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 35 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 # 39 40 __docker_get_containers() { 41 local kind expl 42 declare -a running stopped lines args 43 44 kind=$1 45 shift 46 [[ $kind = (stopped|all) ]] && args=($args -a) 47 48 lines=(${(f)"$(_call_program commands docker ps ${args})"}) 49 50 # Parse header line to find columns 51 local i=1 j=1 k header=${lines[1]} 52 declare -A begin end 53 while (( $j < ${#header} - 1 )) { 54 i=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 1)) 55 j=$(( $i + ${${header[$i,-1]}[(i) ]} - 1)) 56 k=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 2)) 57 begin[${header[$i,$(($j-1))]}]=$i 58 end[${header[$i,$(($j-1))]}]=$k 59 } 60 lines=(${lines[2,-1]}) 61 62 # Container ID 63 local line 64 local s 65 for line in $lines; do 66 s="${line[${begin[CONTAINER ID]},${end[CONTAINER ID]}]%% ##}" 67 s="$s:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}" 68 s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}" 69 if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then 70 stopped=($stopped $s) 71 else 72 running=($running $s) 73 fi 74 done 75 76 # Names 77 local name 78 local -a names 79 for line in $lines; do 80 names=(${(ps:,:)${${line[${begin[NAMES]},-1]}%% *}}) 81 for name in $names; do 82 s="${name}:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}" 83 s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}" 84 if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then 85 stopped=($stopped $s) 86 else 87 running=($running $s) 88 fi 89 done 90 done 91 92 [[ $kind = (running|all) ]] && _describe -t containers-running "running containers" running 93 [[ $kind = (stopped|all) ]] && _describe -t containers-stopped "stopped containers" stopped 94 } 95 96 __docker_stoppedcontainers() { 97 __docker_get_containers stopped "$@" 98 } 99 100 __docker_runningcontainers() { 101 __docker_get_containers running "$@" 102 } 103 104 __docker_containers () { 105 __docker_get_containers all "$@" 106 } 107 108 __docker_images () { 109 local expl 110 declare -a images 111 images=(${${${${(f)"$(_call_program commands docker images)"}[2,-1]}/ ##/\\:}%% *}) 112 images=(${${images%\\:<none>}#<none>} ${${${(f)"$(_call_program commands docker images)"}[2,-1]}/(#b)([^ ]##) ##([^ ]##) ##([^ ]##)*/${match[3]}:${(r:15:: :::)match[2]} in ${match[1]}}) 113 _describe -t docker-images "images" images 114 } 115 116 __docker_tags() { 117 local expl 118 declare -a tags 119 tags=(${${${${${(f)"$(_call_program commands docker images)"}#* }## #}%% *}[2,-1]}) 120 _describe -t docker-tags "tags" tags 121 } 122 123 __docker_repositories_with_tags() { 124 if compset -P '*:'; then 125 __docker_tags 126 else 127 __docker_repositories -qS ":" 128 fi 129 } 130 131 __docker_search() { 132 # declare -a dockersearch 133 local cache_policy 134 zstyle -s ":completion:${curcontext}:" cache-policy cache_policy 135 if [[ -z "$cache_policy" ]]; then 136 zstyle ":completion:${curcontext}:" cache-policy __docker_caching_policy 137 fi 138 139 local searchterm cachename 140 searchterm="${words[$CURRENT]%/}" 141 cachename=_docker-search-$searchterm 142 143 local expl 144 local -a result 145 if ( [[ ${(P)+cachename} -eq 0 ]] || _cache_invalid ${cachename#_} ) \ 146 && ! _retrieve_cache ${cachename#_}; then 147 _message "Searching for ${searchterm}..." 148 result=(${${${(f)"$(_call_program commands docker search ${searchterm})"}%% *}[2,-1]}) 149 _store_cache ${cachename#_} result 150 fi 151 _wanted dockersearch expl 'available images' compadd -a result 152 } 153 154 __docker_caching_policy() 155 { 156 oldp=( "$1"(Nmh+1) ) # 1 hour 157 (( $#oldp )) 158 } 159 160 161 __docker_repositories () { 162 local expl 163 declare -a repos 164 repos=(${${${(f)"$(_call_program commands docker images)"}%% *}[2,-1]}) 165 _describe -t docker-repos "repositories" repos "$@" 166 } 167 168 __docker_commands () { 169 # local -a _docker_subcommands 170 local cache_policy 171 172 zstyle -s ":completion:${curcontext}:" cache-policy cache_policy 173 if [[ -z "$cache_policy" ]]; then 174 zstyle ":completion:${curcontext}:" cache-policy __docker_caching_policy 175 fi 176 177 if ( [[ ${+_docker_subcommands} -eq 0 ]] || _cache_invalid docker_subcommands) \ 178 && ! _retrieve_cache docker_subcommands; 179 then 180 local -a lines 181 lines=(${(f)"$(_call_program commands docker 2>&1)"}) 182 _docker_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I) *]}]}## #}/ ##/:}) 183 _docker_subcommands=($_docker_subcommands 'help:Show help for a command') 184 _store_cache docker_subcommands _docker_subcommands 185 fi 186 _describe -t docker-commands "docker command" _docker_subcommands 187 } 188 189 __docker_subcommand () { 190 local -a _command_args 191 case "$words[1]" in 192 (attach) 193 _arguments \ 194 '--no-stdin[Do not attach stdin]' \ 195 '--sig-proxy[Proxy all received signals to the process (non-TTY mode only)]' \ 196 ':containers:__docker_runningcontainers' 197 ;; 198 (build) 199 _arguments \ 200 {-f,--file=-}'[Dockerfile to use]:Dockerfile:_files' \ 201 '--force-rm[Always remove intermediate containers]' \ 202 '--no-cache[Do not use cache when building the image]' \ 203 '--pull[Attempt to pull a newer version of the image]' \ 204 {-q,--quiet}'[Suppress verbose build output]' \ 205 '--rm[Remove intermediate containers after a successful build]' \ 206 {-t,--tag=-}'[Repository, name and tag to be applied]:repository:__docker_repositories_with_tags' \ 207 ':path or URL:_directories' 208 ;; 209 (commit) 210 _arguments \ 211 {-a,--author=-}'[Author]:author: ' \ 212 {-m,--message=-}'[Commit message]:message: ' \ 213 {-p,--pause}'[Pause container during commit]' \ 214 ':container:__docker_containers' \ 215 ':repository:__docker_repositories_with_tags' 216 ;; 217 (cp) 218 _arguments \ 219 ':container:->container' \ 220 ':hostpath:_files' 221 case $state in 222 (container) 223 if compset -P '*:'; then 224 _files 225 else 226 __docker_containers -qS ":" 227 fi 228 ;; 229 esac 230 ;; 231 (diff|export) 232 _arguments '*:containers:__docker_containers' 233 ;; 234 (events) 235 _arguments \ 236 '*'{-f,--filter=-}'[Filter values]:filter: ' \ 237 '--since=-[Events created since this timestamp]:timestamp: ' \ 238 '--until=-[Events created until this timestamp]:timestamp: ' 239 ;; 240 (exec) 241 local state ret 242 _arguments \ 243 {-d,--detach}'[Detached mode: leave the container running in the background]' \ 244 {-i,--interactive}'[Keep stdin open even if not attached]' \ 245 {-t,--tty}'[Allocate a pseudo-tty]' \ 246 ':containers:__docker_runningcontainers' \ 247 '*::command:->anycommand' && ret=0 248 249 case $state in 250 (anycommand) 251 shift 1 words 252 (( CURRENT-- )) 253 _normal 254 ;; 255 esac 256 257 return ret 258 ;; 259 (history) 260 _arguments \ 261 '--no-trunc[Do not truncate output]' \ 262 {-q,--quiet}'[Only show numeric IDs]' \ 263 '*:images:__docker_images' 264 ;; 265 (images) 266 _arguments \ 267 {-a,--all}'[Show all images]' \ 268 '*'{-f,--filter=-}'[Filter values]:filter: ' \ 269 '--no-trunc[Do not truncate output]' \ 270 {-q,--quiet}'[Only show numeric IDs]' \ 271 ':repository:__docker_repositories' 272 ;; 273 (inspect) 274 _arguments \ 275 {-f,--format=-}'[Format the output using the given go template]:template: ' \ 276 '*:containers:__docker_containers' 277 ;; 278 (import) 279 _arguments \ 280 ':URL:(- http:// file://)' \ 281 ':repository:__docker_repositories_with_tags' 282 ;; 283 (info) 284 ;; 285 (import) 286 _arguments \ 287 ':URL:(- http:// file://)' \ 288 ':repository:__docker_repositories_with_tags' 289 ;; 290 (insert) 291 _arguments '1:containers:__docker_containers' \ 292 '2:URL:(http:// file://)' \ 293 '3:file:_files' 294 ;; 295 (kill) 296 _arguments \ 297 {-s,--signal=-}'[Signal to send]:signal:_signals' \ 298 '*:containers:__docker_runningcontainers' 299 ;; 300 (load) 301 _arguments \ 302 {-i,--input=-}'[Read from tar archive file]:archive file:_files -g "*.((tar|TAR)(.gz|.GZ|.Z|.bz2|.lzma|.xz|)|(tbz|tgz|txz))(-.)"' 303 ;; 304 (login) 305 _arguments \ 306 {-e,--email=-}'[Email]:email: ' \ 307 {-p,--password=-}'[Password]:password: ' \ 308 {-u,--user=-}'[Username]:username: ' \ 309 ':server: ' 310 ;; 311 (logout) 312 _arguments \ 313 ':server: ' 314 ;; 315 (logs) 316 _arguments \ 317 {-f,--follow}'[Follow log output]' \ 318 {-t,--timestamps}'[Show timestamps]' \ 319 '--tail=-[Output the last K lines]:lines:(1 10 20 50 all)' \ 320 '*:containers:__docker_containers' 321 ;; 322 (port) 323 _arguments \ 324 '1:containers:__docker_runningcontainers' \ 325 '2:port:_ports' 326 ;; 327 (pause|unpause) 328 _arguments \ 329 '1:containers:__docker_runningcontainers' 330 ;; 331 (start) 332 _arguments \ 333 {-a,--attach}'[Attach container'"'"'s stdout/stderr and forward all signals]' \ 334 {-i,--interactive}'[Attach container'"'"'s stding]' \ 335 '*:containers:__docker_stoppedcontainers' 336 ;; 337 (stats) 338 _arguments \ 339 '*:containers:__docker_runningcontainers' 340 ;; 341 (rm) 342 _arguments \ 343 {-f,--force}'[Force removal]' \ 344 {-l,--link}'[Remove the specified link and not the underlying container]' \ 345 {-v,--volumes}'[Remove the volumes associated to the container]' \ 346 '*:containers:__docker_stoppedcontainers' 347 ;; 348 (rmi) 349 _arguments \ 350 {-f,--force}'[Force removal]' \ 351 '--no-prune[Do not delete untagged parents]' \ 352 '*:images:__docker_images' 353 ;; 354 (restart|stop) 355 _arguments \ 356 {-t,--time=-}'[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)' \ 357 '*:containers:__docker_runningcontainers' 358 ;; 359 (top) 360 _arguments \ 361 '1:containers:__docker_runningcontainers' \ 362 '(-)*:: :->ps-arguments' 363 case $state in 364 (ps-arguments) 365 _ps 366 ;; 367 esac 368 369 ;; 370 (ps) 371 _arguments \ 372 {-a,--all}'[Show all containers]' \ 373 '--before=-[Show only container created before...]:containers:__docker_containers' \ 374 '*'{-f,--filter=-}'[Filter values]:filter: ' \ 375 {-l,--latest}'[Show only the latest created container]' \ 376 '-n[Show n last created containers, include non-running one]:n:(1 5 10 25 50)' \ 377 '--no-trunc[Do not truncate output]' \ 378 {-q,--quiet}'[Only show numeric IDs]' \ 379 {-s,--size}'[Display total file sizes]' \ 380 '--since=-[Show only containers created since...]:containers:__docker_containers' 381 ;; 382 (tag) 383 _arguments \ 384 {-f,--force}'[force]'\ 385 ':image:__docker_images'\ 386 ':repository:__docker_repositories_with_tags' 387 ;; 388 (create|run) 389 _arguments \ 390 {-a,--attach}'[Attach to stdin, stdout or stderr]' \ 391 '*--add-host=-[Add a custom host-to-IP mapping]:host\:ip mapping: ' \ 392 {-c,--cpu-shares=-}'[CPU shares (relative weight)]:CPU shares:(0 10 100 200 500 800 1000)' \ 393 '*--cap-add=-[Add Linux capabilities]:capability: ' \ 394 '*--cap-drop=-[Drop Linux capabilities]:capability: ' \ 395 '--cidfile=-[Write the container ID to the file]:CID file:_files' \ 396 '--cpuset=-[CPUs in which to allow execution]:CPU set: ' \ 397 {-d,--detach}'[Detached mode: leave the container running in the background]' \ 398 '*--device=-[Add a host device to the container]:device:_files' \ 399 '*--dns=-[Set custom dns servers]:dns server: ' \ 400 '*--dns-search=-[Set custom DNS search domains]:dns domains: ' \ 401 '*'{-e,--environment=-}'[Set environment variables]:environment variable: ' \ 402 '--entrypoint=-[Overwrite the default entrypoint of the image]:entry point: ' \ 403 '*--env-file=-[Read environment variables from a file]:environment file:_files' \ 404 '*--expose=-[Expose a port from the container without publishing it]: ' \ 405 {-h,--hostname=-}'[Container host name]:hostname:_hosts' \ 406 {-i,--interactive}'[Keep stdin open even if not attached]' \ 407 '*--link=-[Add link to another container]:link:->link' \ 408 '*--lxc-conf=-[Add custom lxc options]:lxc options: ' \ 409 '-m[Memory limit (in bytes)]:limit: ' \ 410 '--name=-[Container name]:name: ' \ 411 '--net=-[Network mode]:network mode:(bridge none container host)' \ 412 {-P,--publish-all}'[Publish all exposed ports]' \ 413 '*'{-p,--publish=-}'[Expose a container'"'"'s port to the host]:port:_ports' \ 414 '--privileged[Give extended privileges to this container]' \ 415 '--restart=-[Restart policy]:restart policy:(no on-failure always)' \ 416 '--rm[Remove intermediate containers when it exits]' \ 417 '*--security-opt=-[Security options]:security option: ' \ 418 '--sig-proxy[Proxy all received signals to the process (non-TTY mode only)]' \ 419 {-t,--tty}'[Allocate a pseudo-tty]' \ 420 {-u,--user=-}'[Username or UID]:user:_users' \ 421 '*-v[Bind mount a volume]:volume: '\ 422 '*--volumes-from=-[Mount volumes from the specified container]:volume: ' \ 423 {-w,--workdir=-}'[Working directory inside the container]:directory:_directories' \ 424 '(-):images:__docker_images' \ 425 '(-):command: _command_names -e' \ 426 '*::arguments: _normal' 427 428 case $state in 429 (link) 430 if compset -P '*:'; then 431 _wanted alias expl 'Alias' compadd -E "" 432 else 433 __docker_runningcontainers -qS ":" 434 fi 435 ;; 436 esac 437 438 ;; 439 (pull) 440 _arguments \ 441 {-a,--all-tags}'[Download all tagged images]' \ 442 ':name:__docker_search' 443 ;; 444 (push) 445 _arguments ':images:__docker_images' 446 ;; 447 (rename) 448 _arguments \ 449 ':old name:__docker_containers' \ 450 ':new name: ' 451 ;; 452 (save) 453 _arguments \ 454 {-o,--output=-}'[Write to file]:file:_files' \ 455 '*:images:__docker_images' 456 ;; 457 (search) 458 _arguments \ 459 '--automated[Only show automated builds]' \ 460 '--no-trunc[Do not truncate output]' \ 461 {-s,--stars=-}'[Only display with at least X stars]:stars:(0 10 100 1000)' \ 462 ':term: ' 463 ;; 464 (wait) 465 _arguments '*:containers:__docker_runningcontainers' 466 ;; 467 (help) 468 _arguments ':subcommand:__docker_commands' 469 ;; 470 (*) 471 _message 'Unknown sub command' 472 esac 473 474 } 475 476 _docker () { 477 # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`. 478 # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`. 479 if [[ $service != docker ]]; then 480 _call_function - _$service 481 return 482 fi 483 484 local curcontext="$curcontext" state line 485 typeset -A opt_args 486 487 _arguments -C \ 488 '-H[tcp://host:port to bind/connect to]:socket: ' \ 489 '(-): :->command' \ 490 '(-)*:: :->option-or-argument' 491 492 if (( CURRENT == 1 )); then 493 494 fi 495 case $state in 496 (command) 497 __docker_commands 498 ;; 499 (option-or-argument) 500 curcontext=${curcontext%:*:*}:docker-$words[1]: 501 __docker_subcommand 502 ;; 503 esac 504 } 505 506 _docker "$@" 507 508 # Local Variables: 509 # mode: Shell-Script 510 # sh-indentation: 4 511 # indent-tabs-mode: nil 512 # sh-basic-offset: 4 513 # End: 514 # vim: ft=zsh sw=4 ts=4 et