68f3ebb4de11aa2013ccc5252d4415840e0d7342

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
Carlo Sala <carlosalag@protonmail.com>
Date

Message

feat(docker): generate completion function

Closes #10825
Closes #10912
Closes #11751

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/docker/_docker b/plugins/docker/_docker
  2deleted file mode 100644
  3index e6a12d774f4017e7aeb3ca21c09c4d52a4ddfa41..0000000000000000000000000000000000000000
  4--- a/plugins/docker/_docker
  5+++ /dev/null
  6@@ -1,3123 +0,0 @@
  7-#compdef docker dockerd
  8-#
  9-# zsh completion for docker (http://docker.com)
 10-#
 11-# version:  0.3.0
 12-# github:   https://github.com/felixr/docker-zsh-completion
 13-#
 14-# contributors:
 15-#   - Felix Riedel
 16-#   - Steve Durrheimer
 17-#   - Vincent Bernat
 18-#   - Rohan Verma
 19-#
 20-# license:
 21-#
 22-# Copyright (c) 2013, Felix Riedel
 23-# All rights reserved.
 24-#
 25-# Redistribution and use in source and binary forms, with or without
 26-# modification, are permitted provided that the following conditions are met:
 27-#     * Redistributions of source code must retain the above copyright
 28-#       notice, this list of conditions and the following disclaimer.
 29-#     * Redistributions in binary form must reproduce the above copyright
 30-#       notice, this list of conditions and the following disclaimer in the
 31-#       documentation and/or other materials provided with the distribution.
 32-#     * Neither the name of the <organization> nor the
 33-#       names of its contributors may be used to endorse or promote products
 34-#       derived from this software without specific prior written permission.
 35-#
 36-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 37-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 38-# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 39-# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 40-# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 41-# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 42-# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43-# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 44-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 45-# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 46-#
 47-
 48-# Short-option stacking can be enabled with:
 49-#  zstyle ':completion:*:*:docker:*' option-stacking yes
 50-#  zstyle ':completion:*:*:docker-*:*' option-stacking yes
 51-__docker_arguments() {
 52-    if zstyle -t ":completion:${curcontext}:" option-stacking; then
 53-        print -- -s
 54-    fi
 55-}
 56-
 57-__docker_get_containers() {
 58-    [[ $PREFIX = -* ]] && return 1
 59-    integer ret=1
 60-    local kind type line s
 61-    declare -a running stopped lines args names
 62-
 63-    kind=$1; shift
 64-    type=$1; shift
 65-    [[ $kind = (stopped|all) ]] && args=($args -a)
 66-
 67-    lines=(${(f)${:-"$(_call_program commands docker $docker_options ps --format 'table' --no-trunc $args)"$'\n'}})
 68-
 69-    # Parse header line to find columns
 70-    local i=1 j=1 k header=${lines[1]}
 71-    declare -A begin end
 72-    while (( j < ${#header} - 1 )); do
 73-        i=$(( j + ${${header[$j,-1]}[(i)[^ ]]} - 1 ))
 74-        j=$(( i + ${${header[$i,-1]}[(i)  ]} - 1 ))
 75-        k=$(( j + ${${header[$j,-1]}[(i)[^ ]]} - 2 ))
 76-        begin[${header[$i,$((j-1))]}]=$i
 77-        end[${header[$i,$((j-1))]}]=$k
 78-    done
 79-    end[${header[$i,$((j-1))]}]=-1 # Last column, should go to the end of the line
 80-    lines=(${lines[2,-1]})
 81-
 82-    # Container ID
 83-    if [[ $type = (ids|all) ]]; then
 84-        for line in $lines; do
 85-            s="${${line[${begin[CONTAINER ID]},${end[CONTAINER ID]}]%% ##}[0,12]}"
 86-            s="$s:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}"
 87-            s="$s, ${${${line[${begin[IMAGE]},${end[IMAGE]}]}/:/\\:}%% ##}"
 88-            if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = (Exit*|Created*) ]]; then
 89-                stopped=($stopped $s)
 90-            else
 91-                running=($running $s)
 92-            fi
 93-        done
 94-    fi
 95-
 96-    # Names: we only display the one without slash. All other names
 97-    # are generated and may clutter the completion. However, with
 98-    # Swarm, all names may be prefixed by the swarm node name.
 99-    if [[ $type = (names|all) ]]; then
100-        for line in $lines; do
101-            names=(${(ps:,:)${${line[${begin[NAMES]},${end[NAMES]}]}%% *}})
102-            # First step: find a common prefix and strip it (swarm node case)
103-            (( ${#${(u)names%%/*}} == 1 )) && names=${names#${names[1]%%/*}/}
104-            # Second step: only keep the first name without a /
105-            s=${${names:#*/*}[1]}
106diff --git a/plugins/docker/docker.plugin.zsh b/plugins/docker/docker.plugin.zsh
107index 8684a978582c927f7fbf92fca11d3972f1ed0f58..06a191a10727ba15ce7e11cf997c51f0d7cb5bf7 100644
108--- a/plugins/docker/docker.plugin.zsh
109+++ b/plugins/docker/docker.plugin.zsh
110@@ -1,3 +1,13 @@
111+# If the completion file doesn't exist yet, we need to autoload it and
112+# bind it to `docker`. Otherwise, compinit will have already done that.
113+if [[ ! -f "$ZSH_CACHE_DIR/completions/_docker" ]]; then
114+  typeset -g -A _comps
115+  autoload -Uz _docker
116+  _comps[docker]=_docker
117+fi
118+
119+docker completion zsh >| "$ZSH_CACHE_DIR/completions/_docker" &|
120+
121 alias dbl='docker build'
122 alias dcin='docker container inspect'
123 alias dcls='docker container ls'