From 7e606764834174dfdc8425b4daef50249d868100 Mon Sep 17 00:00:00 2001 From: Daniel de Cloet Date: Thu, 26 May 2022 16:15:16 +0200 Subject: [PATCH] Add zsh tabcompletion for docker-env --- templates/zshrc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/templates/zshrc b/templates/zshrc index 8301c1e..4acbefc 100644 --- a/templates/zshrc +++ b/templates/zshrc @@ -244,6 +244,45 @@ EOS eval $run_command } +# Completion for docker-env +compdef _docker-env docker-env +function _docker-env() { + local curcontext="$curcontext" state + + _arguments \ + "*-p[Publish a containers port(s) to the host]:port:" \ + "*-e[Set environment variables]:env:" \ + "*-v[Bind mount a volume]:volume:" \ + "-w[Working directory inside the container]" \ + "(-p -e -v -w)--[Explicitely stop switches and start positional arguments]" \ + "(-p -e -v -w --)1:Container image:->image" \ + "*:Container CMD:->cmd" + + case $state in + image) + # Check if you can find a colon, + # if so strip everything up to that colon from further completion matching + if compset -P -1 '*:'; then + # Get the tags for the current image and use those for completion + local tags=$(docker image ls --filter 'dangling=false' --filter 'reference='"${words[-1]}"'*' --format '{% raw %}{{.Tag}}{% endraw %}' | sed 's/:/\\:/g' | uniq) + + # Add tags for autocompletion. If none exist, then show latest + if [ {% raw %}${#tags}{% endraw %} -gt 0 ]; then + compadd $tags + else + compadd "latest" + fi + else + local repositories=($(docker image ls --filter 'dangling=false' --format "{% raw %}{{.Repository}}{% endraw %}" | sed 's/:/\\:/g' | uniq)) + compadd -S : $repositories + fi + ;; + cmd) + _default + ;; + esac +} + function limit_video_size() { if [ $# -ne 3 ]; then echo "Usage: limit_video_size "