From f617883fdfbad001c46b7d7887255c475ab9f6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 1 Jan 2021 13:16:12 +0100 Subject: [PATCH] Fix docker plugin suggestions in Zsh completion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The __docker_plugins function calls `docker plugin ls` to get its information, but it doesn't output a TAG column anymore. Without this change, triggering the function, e.g. trying to complete `docker plugin inspect`, produces the following error repeatedly: __docker_plugins:27: bad math expression: empty string Also, the tag part of the docker plugin is shown in the NAME column, but the colon (:) character is swallowed by the `_describe` function and interpreted so that the right-side of the string is displayed as a comment to the suggestion. For example: vieux/sshfs:latest is suggested like vieux/sshfs -- latest instead of the tag being part of the suggestion. This means that if there are more than one tags installed of the same docker plugin, they are both combined into a single completion entry, instead of two, one for each tag. By quoting the colon character, the tag is now correctly offered in the suggestion. With this commit the plugin description is added to each completion suggestion and plugin names are shown without the redundant ":latest" tag. Fixes #9294 Signed-off-by: Marc Cornellà --- contrib/completion/zsh/_docker | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/contrib/completion/zsh/_docker b/contrib/completion/zsh/_docker index cf8bda6a76..a155565bac 100644 --- a/contrib/completion/zsh/_docker +++ b/contrib/completion/zsh/_docker @@ -1559,31 +1559,21 @@ __docker_plugin_complete_ls_filters() { __docker_plugins() { [[ $PREFIX = -* ]] && return 1 integer ret=1 - local line s + local filter line s declare -a lines plugins args filter=$1; shift [[ $filter != "none" ]] && args=("-f $filter") - lines=(${(f)${:-"$(_call_program commands docker $docker_options plugin ls $args)"$'\n'}}) + # Output plugins in format "name:tag\|description" + lines=(${(f)${:-"$(_call_program commands docker $docker_options plugin ls --format='{{.Name}}\|{{.Description}}' $args)"}}) - # Parse header line to find columns - local i=1 j=1 k header=${lines[1]} - declare -A begin end - while (( j < ${#header} - 1 )); do - i=$(( j + ${${header[$j,-1]}[(i)[^ ]]} - 1 )) - j=$(( i + ${${header[$i,-1]}[(i) ]} - 1 )) - k=$(( j + ${${header[$j,-1]}[(i)[^ ]]} - 2 )) - begin[${header[$i,$((j-1))]}]=$i - end[${header[$i,$((j-1))]}]=$k - done - end[${header[$i,$((j-1))]}]=-1 - lines=(${lines[2,-1]}) - - # Name + # Suggestion entries: name:tag -- description for line in $lines; do - s="${line[${begin[NAME]},${end[NAME]}]%% ##}" - s="$s:${(l:7:: :::)${${line[${begin[TAG]},${end[TAG]}]}%% ##}}" + # - Remove redundant :latest tag + # - Quote : in name:tag (_describe splits on : to separate entry and description) + # - Replace \| separator with : + s="${${${line//:latest/}//:/\:}//\|/:}" plugins=($plugins $s) done