Skip to content

Conversation

@albers
Copy link
Collaborator

@albers albers commented Nov 29, 2017

Shortcomings of the current solution

Bash completion of images uses the vanilla output of docker images and filters it with awk.
This is error prone because users may customize the output of docker images by setting a go template in ~/.docker/config.json with the imagesFormat key.
And it is ugly.

There are several valid combinations of repository, tag and ID in Docker CLI. This diversity is not sufficiently covered yet, leading to issues like moby/moby#27791.

Furthermore, problems arise because completion of images is customizable through environment variables:

  • The user may hide tags by setting DOCKER_COMPLETION_SHOW_TAGS=no.
  • The user may choose the amount of image IDs shown in completion: DOCKER_COMPLETION_SHOW_IMAGE_IDS=none(default)|non-intermediate|all

These settings are not fine-grained enough, though.

  • In cases where an exact image specification is required, e.g. docker history, we have to show tags even if the user opted to hide them.
  • IDs are not legal in all image specifications.

Solution

  • Use explicit formatting in docker images like in the completions of containers, networks, volumes... .
  • Specify which of repository, tag and id are valid with arguments (--repo, --tag, --id) to one central completion function and let this function decide what to complete wrt environment settings.
  • Create an override for cases where tags are required (--force-tag)

We now use the following combinations:
__docker_complete_images --force-tag --id
__docker_complete_images --repo
__docker_complete_images --repo --tag
__docker_complete_images --repo --tag --id

I prefer to leave the invocations as listed here instead of creating 4 helper functions because these would not increase readibility. The explicit list of legal choices very clearly expresses differences between related completetions, see images --filter (before|since)= and images --filter reference= for an example.

Fixes

Changes

Completion of image import now completes local files instead of nothing. The previous choice was made because URLs are also valid syntax. The result feels like a broken completion.

Some minor changes in style were applied in a separate commit in order to increase code consistency.

Testing

If you have bash completion installed and working, just source the updated completion script.
The main helper function can also be called directly to study the influence of the customizion via environment variables, e.g.

__docker_images --repo --tag --id
DOCKER_COMPLETION_SHOW_IMAGE_IDS=none __docker_images --repo --tag --id
DOCKER_COMPLETION_SHOW_IMAGE_IDS=non-intermediate __docker_images --repo --tag --id
DOCKER_COMPLETION_SHOW_TAGS=no __docker_images --force-tag

ping @mlaventure Fixes moby/moby#27791, PTAL

@codecov-io
Copy link

codecov-io commented Nov 29, 2017

Codecov Report

Merging #717 into master will decrease coverage by 0.21%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master     #717      +/-   ##
==========================================
- Coverage   53.51%   53.29%   -0.22%     
==========================================
  Files         218      218              
  Lines       14565    14565              
==========================================
- Hits         7795     7763      -32     
- Misses       6289     6326      +37     
+ Partials      481      476       -5

@albers albers changed the title Improve bash completion for images Improve and fix bash completion for images Nov 29, 2017
@albers albers force-pushed the completion-images-2 branch from 8cd7a85 to 783d7f0 Compare November 30, 2017 16:30
fi
shift
;;
--force-tag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it should be additive with --tag, like --tag --force or something like that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work because it is not clear what should be forced. My intent is to use --repo, --tag and --id to express that a representation can be completed (if permitted by environment settings) and to use --force-repo, --force-tag and --force-id when a representation has to be added, regardless of environment settings. Of the latter, currently only --force-tag is used, so the other options from this set are not yet implemented.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also want to keep this function very simple. That's why it doesn't care about the order of the arguments. Thus, a --force is not deterministic. And I want the invocations to fit on one line, so I opted for the short names like --tag instead of something more exact like --try-tag or --allow-tag.
I think this simplicity is appropriate for this function because it is only used internally in the bash completion script.

__docker_q image ls --no-trunc --format "${format%\\n}" $all "$@" | grep -v '<none>$'
}

# __docker_complete_images appies completion of images based on the current value of `$cur` or
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typo "appies" should be "applies"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, I'll change that. Thanks.

@albers albers force-pushed the completion-images-2 branch from 783d7f0 to 1f86efe Compare December 4, 2017 08:31
Signed-off-by: Harald Albers <github@albersweb.de>
Signed-off-by: Harald Albers <github@albersweb.de>
Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
cc @mlaventure

Copy link
Contributor

@mlaventure mlaventure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah
Copy link
Member

whoops forgot about this one LGTM, thanks!

@thaJeztah thaJeztah merged commit 4586609 into docker:master Jan 28, 2018
@GordonTheTurtle GordonTheTurtle added this to the 18.02.0 milestone Jan 28, 2018
@albers albers deleted the completion-images-2 branch January 28, 2018 13:37
@thaJeztah thaJeztah modified the milestones: 18.02.0, 18.03.0 Jan 31, 2018
@albers
Copy link
Collaborator Author

albers commented Feb 1, 2018

@thaJeztah Would you mind looking at #682? It's been hanging around even longer.

nobiit pushed a commit to nobidev/docker-cli that referenced this pull request Nov 19, 2025
Improve and fix bash completion for images
Upstream-commit: 4586609
Component: cli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants