docker / cli

The Docker CLI
Apache License 2.0
4.95k stars 1.93k forks source link

zsh completion: bad math expression in __docker_plugins function #2761

Open mcornella opened 4 years ago

mcornella commented 4 years ago

Description

Steps to reproduce the issue:

  1. Run zsh with docker installed and the docker completion working.
  2. Make sure there's at least one docker plugin installed so that docker plugin ls shows at least one row. For example: docker plugin install vieux/sshfs.
  3. Try to complete docker inspect <TAB>

Describe the results you received:

The shell outputs multiple __docker_plugins:27: bad math expression: empty string error messages and the completion entries are all garbled.

Describe the results you expected:

A list of Docker objects available to be inspected.

Additional information you deem important (e.g. issue happens only occasionally):

I've tracked down the problem to the __docker_plugins function, which expects the output of docker plugin ls to have a TAG column, which it doesn't. I believe the solution is to use the ID column instead, but I haven't used docker plugins before so I may be wrong.

I've also seen that just modifying the function to use the ID column isn't enough to make the results of the function correct, since it also tries to truncate the ID hash to 7 characters, but it truncates the beginning characters of the string, not the ending ones. This is I believe what would make the function work correctly:

diff --git a/contrib/completion/zsh/_docker b/contrib/completion/zsh/_docker
index 4d28ac4..82a77de 100644
--- a/contrib/completion/zsh/_docker
+++ b/contrib/completion/zsh/_docker
@@ -1582,7 +1582,7 @@ __docker_plugins() {
     # Name
     for line in $lines; do
         s="${line[${begin[NAME]},${end[NAME]}]%% ##}"
-        s="$s:${(l:7:: :::)${${line[${begin[TAG]},${end[TAG]}]}%% ##}}"
+        s="$s:${(r:7:: :::)${${line[${begin[ID]},${end[ID]}]}%% ##}}"
         plugins=($plugins $s)
     done

Related issue: https://github.com/ohmyzsh/ohmyzsh/issues/9294

Output of docker version:

Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:45:47 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:44:17 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Output of docker info:

Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 16
 Server Version: 19.03.12
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.19.128-microsoft-standard
 Operating System: Ubuntu 19.10
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.803GiB
 Name: WATSON
 ID: 3F54:JRUE:ZIAJ:KJ42:DQOE:TLU6:774I:JZKT:5I3L:HAJX:US4R:NHFE
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Additional environment details (AWS, VirtualBox, physical, etc.):

Using Ubuntu 19.10 on WSL2 but I don't think it makes a difference.

mcornella commented 3 years ago

Seeing as this got no traction, I pushed #2903 to fix it. I think the original reasoning still stands. I was wrong, fixed it in https://github.com/docker/cli/pull/2903#issuecomment-753341946.