From 10638a7a3e5849bcc6d1ac92805f9fe2203e255b Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Sun, 2 Apr 2017 15:32:33 -0700 Subject: [PATCH] Add bash completion for `secret ls --format` Signed-off-by: Harald Albers --- contrib/completion/bash/docker | 55 ++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index 27c1d0cc7f..1df86ba616 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -328,20 +328,40 @@ __docker_complete_runtimes() { COMPREPLY=( $(compgen -W "$(__docker_runtimes)" -- "$cur") ) } -# __docker_secrets returns a list of all secrets. -# By default, only names of secrets are returned. -# Set DOCKER_COMPLETION_SHOW_SECRET_IDS=yes to also complete IDs of secrets. +# __docker_secrets returns a list of secrets. Additional options to +# `docker secret ls` may be specified in order to filter the list, e.g. +# `__docker_secrets --filter label=stage=production` +# By default, only names are returned. +# Set DOCKER_COMPLETION_SHOW_SECRET_IDS=yes to also complete IDs. +# An optional first option `--id|--name` may be used to limit the +# output to the IDs or names of matching items. This setting takes +# precedence over the environment setting. __docker_secrets() { - local fields='$2' # default: name only - [ "${DOCKER_COMPLETION_SHOW_SECRET_IDS}" = yes ] && fields='$1,$2' # ID and name + local format + if [ "$1" = "--id" ] ; then + format='{{.ID}}' + shift + elif [ "$1" = "--name" ] ; then + format='{{.Name}}' + shift + elif [ "$DOCKER_COMPLETION_SHOW_SECRET_IDS" = yes ] ; then + format='{{.ID}} {{.Name}}' + else + format='{{.Name}}' + fi - __docker_q secret ls | awk "NR>1 {print $fields}" + __docker_q secret ls --format "$format" "$@" } # __docker_complete_secrets applies completion of secrets based on the current value -# of `$cur`. +# of `$cur` or the value of the optional first option `--cur`, if given. __docker_complete_secrets() { - COMPREPLY=( $(compgen -W "$(__docker_secrets)" -- "$cur") ) + local current="$cur" + if [ "$1" = "--cur" ] ; then + current="$2" + shift 2 + fi + COMPREPLY=( $(compgen -W "$(__docker_secrets "$@")" -- "$current") ) } # __docker_stacks returns a list of all stacks. @@ -3854,7 +3874,24 @@ _docker_secret_list() { } _docker_secret_ls() { + local key=$(__docker_map_key_of_current_option '--filter|-f') + case "$key" in + id) + __docker_complete_secrets --cur "${cur##*=}" --id + return + ;; + name) + __docker_complete_secrets --cur "${cur##*=}" --name + return + ;; + esac + case "$prev" in + --filter|-f) + COMPREPLY=( $( compgen -S = -W "id label name" -- "$cur" ) ) + __docker_nospace + return + ;; --format) return ;; @@ -3862,7 +3899,7 @@ _docker_secret_ls() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--format --help --quiet -q" -- "$cur" ) ) + COMPREPLY=( $( compgen -W "--format --filter -f --help --quiet -q" -- "$cur" ) ) ;; esac }