github.com/nguyentm83/docker@v1.5.0/contrib/completion/zsh/_docker (about)

     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                  '--force-rm[Always remove intermediate containers]' \
   201                  '--no-cache[Do not use cache when building the image]' \
   202                  {-q,--quiet}'[Suppress verbose build output]' \
   203                  '--rm[Remove intermediate containers after a successful build]' \
   204                  {-t,--tag=-}'[Repository, name and tag to be applied]:repository:__docker_repositories_with_tags' \
   205                  ':path or URL:_directories'
   206              ;;
   207          (commit)
   208              _arguments \
   209                  {-a,--author=-}'[Author]:author: ' \
   210                  {-m,--message=-}'[Commit message]:message: ' \
   211                  {-p,--pause}'[Pause container during commit]' \
   212                  '--run=-[Configuration automatically applied when the image is run]:configuration: ' \
   213                  ':container:__docker_containers' \
   214                  ':repository:__docker_repositories_with_tags'
   215              ;;
   216          (cp)
   217              _arguments \
   218                  ':container:->container' \
   219                  ':hostpath:_files'
   220              case $state in
   221                  (container)
   222                      if compset -P '*:'; then
   223                          _files
   224                      else
   225                          __docker_containers -qS ":"
   226                      fi
   227                      ;;
   228              esac
   229              ;;
   230          (diff|export)
   231              _arguments '*:containers:__docker_containers'
   232              ;;
   233          (events)
   234              _arguments \
   235                  '--since=-[Events created since this timestamp]:timestamp: ' \
   236                  '--until=-[Events created until this timestamp]:timestamp: '
   237              ;;
   238          (exec)
   239              _arguments \
   240                  {-d,--detach}'[Detached mode: leave the container running in the background]' \
   241                  {-i,--interactive}'[Keep stdin open even if not attached]' \
   242                  {-t,--tty}'[Allocate a pseudo-tty]' \
   243                  ':containers:__docker_runningcontainers'
   244              ;;
   245          (history)
   246              _arguments \
   247                  '--no-trunc[Do not truncate output]' \
   248                  {-q,--quiet}'[Only show numeric IDs]' \
   249                  '*:images:__docker_images'
   250              ;;
   251          (images)
   252              _arguments \
   253                  {-a,--all}'[Show all images]' \
   254                  '*'{-f,--filter=-}'[Filter values]:filter: ' \
   255                  '--no-trunc[Do not truncate output]' \
   256                  {-q,--quiet}'[Only show numeric IDs]' \
   257                  '--tree[Output graph in tree format]' \
   258                  '--viz[Output graph in graphviz format]' \
   259                  ':repository:__docker_repositories'
   260              ;;
   261          (inspect)
   262              _arguments \
   263                  {-f,--format=-}'[Format the output using the given go template]:template: ' \
   264                  '*:containers:__docker_containers'
   265              ;;
   266          (import)
   267              _arguments \
   268                  ':URL:(- http:// file://)' \
   269                  ':repository:__docker_repositories_with_tags'
   270              ;;
   271          (info)
   272              ;;
   273          (import)
   274              _arguments \
   275                  ':URL:(- http:// file://)' \
   276                  ':repository:__docker_repositories_with_tags'
   277              ;;
   278          (insert)
   279              _arguments '1:containers:__docker_containers' \
   280                         '2:URL:(http:// file://)' \
   281                         '3:file:_files'
   282              ;;
   283          (kill)
   284              _arguments \
   285                  {-s,--signal=-}'[Signal to send]:signal:_signals' \
   286                  '*:containers:__docker_runningcontainers'
   287              ;;
   288          (load)
   289              _arguments \
   290                  {-i,--input=-}'[Read from tar archive file]:tar:_files'
   291              ;;
   292          (login)
   293              _arguments \
   294                  {-e,--email=-}'[Email]:email: ' \
   295                  {-p,--password=-}'[Password]:password: ' \
   296                  {-u,--user=-}'[Username]:username: ' \
   297                  ':server: '
   298              ;;
   299          (logout)
   300              _arguments \
   301                  ':server: '
   302              ;;
   303          (logs)
   304              _arguments \
   305                  {-f,--follow}'[Follow log output]' \
   306                  {-t,--timestamps}'[Show timestamps]' \
   307                  '*:containers:__docker_containers'
   308              ;;
   309          (port)
   310              _arguments \
   311                  '1:containers:__docker_runningcontainers' \
   312                  '2:port:_ports'
   313              ;;
   314          (pause|unpause)
   315              _arguments \
   316                  '1:containers:__docker_runningcontainers'
   317              ;;
   318          (start)
   319              _arguments \
   320                  {-a,--attach}'[Attach container'"'"'s stdout/stderr and forward all signals]' \
   321                  {-i,--interactive}'[Attach container'"'"'s stding]' \
   322                  '*:containers:__docker_stoppedcontainers'
   323              ;;
   324          (rm)
   325              _arguments \
   326                  {-f,--force}'[Force removal]' \
   327                  {-l,--link}'[Remove the specified link and not the underlying container]' \
   328                  {-v,--volumes}'[Remove the volumes associated to the container]' \
   329                  '*:containers:__docker_stoppedcontainers'
   330              ;;
   331          (rmi)
   332              _arguments \
   333                  {-f,--force}'[Force removal]' \
   334                  '--no-prune[Do not delete untagged parents]' \
   335                  '*:images:__docker_images'
   336              ;;
   337          (restart|stop)
   338              _arguments \
   339                  {-t,--time=-}'[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)' \
   340                  '*:containers:__docker_runningcontainers'
   341              ;;
   342          (top)
   343              _arguments \
   344                  '1:containers:__docker_runningcontainers' \
   345                  '(-)*:: :->ps-arguments'
   346              case $state in
   347                  (ps-arguments)
   348                      _ps
   349                      ;;
   350              esac
   351  
   352              ;;
   353          (ps)
   354              _arguments \
   355                  {-a,--all}'[Show all containers]' \
   356                  '--before=-[Show only container created before...]:containers:__docker_containers' \
   357                  '*'{-f,--filter=-}'[Filter values]:filter: ' \
   358                  {-l,--latest}'[Show only the latest created container]' \
   359                  '-n[Show n last created containers, include non-running one]:n:(1 5 10 25 50)' \
   360                  '--no-trunc[Do not truncate output]' \
   361                  {-q,--quiet}'[Only show numeric IDs]' \
   362                  {-s,--size}'[Display total file sizes]' \
   363                  '--since=-[Show only containers created since...]:containers:__docker_containers'
   364              ;;
   365          (tag)
   366              _arguments \
   367                  {-f,--force}'[force]'\
   368                  ':image:__docker_images'\
   369                  ':repository:__docker_repositories_with_tags'
   370              ;;
   371          (create|run)
   372              _arguments \
   373                  {-a,--attach}'[Attach to stdin, stdout or stderr]' \
   374                  '*--add-host=-[Add a custom host-to-IP mapping]:host\:ip mapping: ' \
   375                  {-c,--cpu-shares=-}'[CPU shares (relative weight)]:CPU shares:(0 10 100 200 500 800 1000)' \
   376                  '*--cap-add=-[Add Linux capabilities]:capability: ' \
   377                  '*--cap-drop=-[Drop Linux capabilities]:capability: ' \
   378                  '--cidfile=-[Write the container ID to the file]:CID file:_files' \
   379                  '--cpuset=-[CPUs in which to allow execution]:CPU set: ' \
   380                  {-d,--detach}'[Detached mode: leave the container running in the background]' \
   381                  '*--device=-[Add a host device to the container]:device:_files' \
   382                  '*--dns=-[Set custom dns servers]:dns server: ' \
   383                  '*--dns-search=-[Set custom DNS search domains]:dns domains: ' \
   384                  '*'{-e,--environment=-}'[Set environment variables]:environment variable: ' \
   385                  '--entrypoint=-[Overwrite the default entrypoint of the image]:entry point: ' \
   386                  '*--env-file=-[Read environment variables from a file]:environment file:_files' \
   387                  '*--expose=-[Expose a port from the container without publishing it]: ' \
   388                  {-h,--hostname=-}'[Container host name]:hostname:_hosts' \
   389                  {-i,--interactive}'[Keep stdin open even if not attached]' \
   390                  '*--link=-[Add link to another container]:link:->link' \
   391                  '*--lxc-conf=-[Add custom lxc options]:lxc options: ' \
   392                  '-m[Memory limit (in bytes)]:limit: ' \
   393                  '--name=-[Container name]:name: ' \
   394                  '--net=-[Network mode]:network mode:(bridge none container: host)' \
   395                  {-P,--publish-all}'[Publish all exposed ports]' \
   396                  '*'{-p,--publish=-}'[Expose a container'"'"'s port to the host]:port:_ports' \
   397                  '--privileged[Give extended privileges to this container]' \
   398                  '--restart=-[Restart policy]:restart policy:(no on-failure always)' \
   399                  '--rm[Remove intermediate containers when it exits]' \
   400                  '*--security-opt=-[Security options]:security option: ' \
   401                  '--sig-proxy[Proxy all received signals to the process (non-TTY mode only)]' \
   402                  {-t,--tty}'[Allocate a pseudo-tty]' \
   403                  {-u,--user=-}'[Username or UID]:user:_users' \
   404                  '*-v[Bind mount a volume]:volume: '\
   405                  '*--volumes-from=-[Mount volumes from the specified container]:volume: ' \
   406                  {-w,--workdir=-}'[Working directory inside the container]:directory:_directories' \
   407                  '(-):images:__docker_images' \
   408                  '(-):command: _command_names -e' \
   409                  '*::arguments: _normal'
   410  
   411              case $state in
   412                  (link)
   413                      if compset -P '*:'; then
   414                          _wanted alias expl 'Alias' compadd -E ""
   415                      else
   416                          __docker_runningcontainers -qS ":"
   417                      fi
   418                      ;;
   419              esac
   420  
   421              ;;
   422          (pull|search)
   423              _arguments ':name:__docker_search'
   424              ;;
   425          (push)
   426              _arguments ':images:__docker_images'
   427              ;;
   428          (save)
   429              _arguments \
   430                  {-o,--output=-}'[Write to file]:file:_files' \
   431                  ':images:__docker_images'
   432              ;;
   433          (wait)
   434              _arguments ':containers:__docker_runningcontainers'
   435              ;;
   436          (help)
   437              _arguments ':subcommand:__docker_commands'
   438              ;;
   439          (*)
   440              _message 'Unknown sub command'
   441      esac
   442  
   443  }
   444  
   445  _docker () {
   446      # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
   447      # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
   448      if [[ $service != docker ]]; then
   449          _call_function - _$service
   450          return
   451      fi
   452  
   453      local curcontext="$curcontext" state line
   454      typeset -A opt_args
   455  
   456      _arguments -C \
   457        '-H[tcp://host:port to bind/connect to]:socket: ' \
   458           '(-): :->command' \
   459           '(-)*:: :->option-or-argument'
   460  
   461      if (( CURRENT == 1 )); then
   462  
   463      fi
   464      case $state in
   465          (command)
   466              __docker_commands
   467              ;;
   468          (option-or-argument)
   469              curcontext=${curcontext%:*:*}:docker-$words[1]:
   470              __docker_subcommand
   471              ;;
   472      esac
   473  }
   474  
   475  _docker "$@"
   476  
   477  # Local Variables:
   478  # mode: Shell-Script
   479  # sh-indentation: 4
   480  # indent-tabs-mode: nil
   481  # sh-basic-offset: 4
   482  # End:
   483  # vim: ft=zsh sw=4 ts=4 et