github.com/amylindburg/docker@v1.7.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                  {-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          (import)
   274              _arguments \
   275                  ':URL:(- http:// file://)' \
   276                  ':repository:__docker_repositories_with_tags'
   277              ;;
   278          (info)
   279              ;;
   280          (inspect)
   281              _arguments \
   282                  {-f,--format=-}'[Format the output using the given go template]:template: ' \
   283                  '*:containers:__docker_containers'
   284              ;;
   285          (kill)
   286              _arguments \
   287                  {-s,--signal=-}'[Signal to send]:signal:_signals' \
   288                  '*:containers:__docker_runningcontainers'
   289              ;;
   290          (load)
   291              _arguments \
   292                  {-i,--input=-}'[Read from tar archive file]:archive file:_files -g "*.((tar|TAR)(.gz|.GZ|.Z|.bz2|.lzma|.xz|)|(tbz|tgz|txz))(-.)"'
   293              ;;
   294          (login)
   295              _arguments \
   296                  {-e,--email=-}'[Email]:email: ' \
   297                  {-p,--password=-}'[Password]:password: ' \
   298                  {-u,--user=-}'[Username]:username: ' \
   299                  ':server: '
   300              ;;
   301          (logout)
   302              _arguments \
   303                  ':server: '
   304              ;;
   305          (logs)
   306              _arguments \
   307                  {-f,--follow}'[Follow log output]' \
   308                  '-s,--since[Show logs since timestamp]' \
   309                  {-t,--timestamps}'[Show timestamps]' \
   310                  '--tail=-[Output the last K lines]:lines:(1 10 20 50 all)' \
   311                  '*:containers:__docker_containers'
   312              ;;
   313          (port)
   314              _arguments \
   315                  '1:containers:__docker_runningcontainers' \
   316                  '2:port:_ports'
   317              ;;
   318          (pause|unpause)
   319              _arguments \
   320                  '1:containers:__docker_runningcontainers'
   321              ;;
   322          (start)
   323              _arguments \
   324                  {-a,--attach}'[Attach container'"'"'s stdout/stderr and forward all signals]' \
   325                  {-i,--interactive}'[Attach container'"'"'s stding]' \
   326                  '*:containers:__docker_stoppedcontainers'
   327              ;;
   328          (stats)
   329              _arguments \
   330                  '--no-stream[Disable streaming stats and only pull the first result]' \
   331                  '*:containers:__docker_runningcontainers'
   332              ;;
   333          (rm)
   334              _arguments \
   335                  {-f,--force}'[Force removal]' \
   336                  {-l,--link}'[Remove the specified link and not the underlying container]' \
   337                  {-v,--volumes}'[Remove the volumes associated to the container]' \
   338                  '*:containers:__docker_stoppedcontainers'
   339              ;;
   340          (rmi)
   341              _arguments \
   342                  {-f,--force}'[Force removal]' \
   343                  '--no-prune[Do not delete untagged parents]' \
   344                  '*:images:__docker_images'
   345              ;;
   346          (restart|stop)
   347              _arguments \
   348                  {-t,--time=-}'[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)' \
   349                  '*:containers:__docker_runningcontainers'
   350              ;;
   351          (top)
   352              _arguments \
   353                  '1:containers:__docker_runningcontainers' \
   354                  '(-)*:: :->ps-arguments'
   355              case $state in
   356                  (ps-arguments)
   357                      _ps
   358                      ;;
   359              esac
   360  
   361              ;;
   362          (ps)
   363              _arguments \
   364                  {-a,--all}'[Show all containers]' \
   365                  '--before=-[Show only container created before...]:containers:__docker_containers' \
   366                  '*'{-f,--filter=-}'[Filter values]:filter: ' \
   367                  {-l,--latest}'[Show only the latest created container]' \
   368                  '-n[Show n last created containers, include non-running one]:n:(1 5 10 25 50)' \
   369                  '--no-trunc[Do not truncate output]' \
   370                  {-q,--quiet}'[Only show numeric IDs]' \
   371                  {-s,--size}'[Display total file sizes]' \
   372                  '--since=-[Show only containers created since...]:containers:__docker_containers'
   373              ;;
   374          (tag)
   375              _arguments \
   376                  {-f,--force}'[force]'\
   377                  ':image:__docker_images'\
   378                  ':repository:__docker_repositories_with_tags'
   379              ;;
   380          (create|run)
   381              _arguments \
   382                  {-a,--attach}'[Attach to stdin, stdout or stderr]' \
   383                  '*--add-host=-[Add a custom host-to-IP mapping]:host\:ip mapping: ' \
   384                  {-c,--cpu-shares=-}'[CPU shares (relative weight)]:CPU shares:(0 10 100 200 500 800 1000)' \
   385                  '*--cap-add=-[Add Linux capabilities]:capability: ' \
   386                  '*--cap-drop=-[Drop Linux capabilities]:capability: ' \
   387                  '--cidfile=-[Write the container ID to the file]:CID file:_files' \
   388                  '--cpuset=-[CPUs in which to allow execution]:CPU set: ' \
   389                  {-d,--detach}'[Detached mode: leave the container running in the background]' \
   390                  '*--device=-[Add a host device to the container]:device:_files' \
   391                  '*--dns=-[Set custom dns servers]:dns server: ' \
   392                  '*--dns-search=-[Set custom DNS search domains]:dns domains: ' \
   393                  '*'{-e,--environment=-}'[Set environment variables]:environment variable: ' \
   394                  '--entrypoint=-[Overwrite the default entrypoint of the image]:entry point: ' \
   395                  '*--env-file=-[Read environment variables from a file]:environment file:_files' \
   396                  '*--expose=-[Expose a port from the container without publishing it]: ' \
   397                  {-h,--hostname=-}'[Container host name]:hostname:_hosts' \
   398                  {-i,--interactive}'[Keep stdin open even if not attached]' \
   399                  '*--link=-[Add link to another container]:link:->link' \
   400                  '*--lxc-conf=-[Add custom lxc options]:lxc options: ' \
   401                  '-m[Memory limit (in bytes)]:limit: ' \
   402                  '--name=-[Container name]:name: ' \
   403                  '--net=-[Network mode]:network mode:(bridge none container host)' \
   404                  {-P,--publish-all}'[Publish all exposed ports]' \
   405                  '*'{-p,--publish=-}'[Expose a container'"'"'s port to the host]:port:_ports' \
   406                  '--privileged[Give extended privileges to this container]' \
   407                  '--restart=-[Restart policy]:restart policy:(no on-failure always)' \
   408                  '--rm[Remove intermediate containers when it exits]' \
   409                  '*--security-opt=-[Security options]:security option: ' \
   410                  '--sig-proxy[Proxy all received signals to the process (non-TTY mode only)]' \
   411                  {-t,--tty}'[Allocate a pseudo-tty]' \
   412                  {-u,--user=-}'[Username or UID]:user:_users' \
   413                  '*-v[Bind mount a volume]:volume: '\
   414                  '*--volumes-from=-[Mount volumes from the specified container]:volume: ' \
   415                  {-w,--workdir=-}'[Working directory inside the container]:directory:_directories' \
   416                  '(-):images:__docker_images' \
   417                  '(-):command: _command_names -e' \
   418                  '*::arguments: _normal'
   419  
   420              case $state in
   421                  (link)
   422                      if compset -P '*:'; then
   423                          _wanted alias expl 'Alias' compadd -E ""
   424                      else
   425                          __docker_runningcontainers -qS ":"
   426                      fi
   427                      ;;
   428              esac
   429  
   430              ;;
   431          (pull)
   432              _arguments \
   433                  {-a,--all-tags}'[Download all tagged images]' \
   434                  ':name:__docker_search'
   435              ;;
   436          (push)
   437              _arguments ':images:__docker_images'
   438              ;;
   439          (rename)
   440              _arguments \
   441                  ':old name:__docker_containers' \
   442                  ':new name: '
   443              ;;
   444          (save)
   445              _arguments \
   446                  {-o,--output=-}'[Write to file]:file:_files' \
   447                  '*:images:__docker_images'
   448              ;;
   449          (search)
   450              _arguments \
   451                  '--automated[Only show automated builds]' \
   452                  '--no-trunc[Do not truncate output]' \
   453                  {-s,--stars=-}'[Only display with at least X stars]:stars:(0 10 100 1000)' \
   454                  ':term: '
   455              ;;
   456          (wait)
   457              _arguments '*:containers:__docker_runningcontainers'
   458              ;;
   459          (help)
   460              _arguments ':subcommand:__docker_commands'
   461              ;;
   462          (*)
   463              _message 'Unknown sub command'
   464      esac
   465  
   466  }
   467  
   468  _docker () {
   469      # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
   470      # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
   471      if [[ $service != docker ]]; then
   472          _call_function - _$service
   473          return
   474      fi
   475  
   476      local curcontext="$curcontext" state line
   477      typeset -A opt_args
   478  
   479      _arguments -C \
   480        '-H[tcp://host:port to bind/connect to]:socket: ' \
   481           '(-): :->command' \
   482           '(-)*:: :->option-or-argument'
   483  
   484      if (( CURRENT == 1 )); then
   485  
   486      fi
   487      case $state in
   488          (command)
   489              __docker_commands
   490              ;;
   491          (option-or-argument)
   492              curcontext=${curcontext%:*:*}:docker-$words[1]:
   493              __docker_subcommand
   494              ;;
   495      esac
   496  }
   497  
   498  _docker "$@"
   499  
   500  # Local Variables:
   501  # mode: Shell-Script
   502  # sh-indentation: 4
   503  # indent-tabs-mode: nil
   504  # sh-basic-offset: 4
   505  # End:
   506  # vim: ft=zsh sw=4 ts=4 et