From c40952b3058db3c6e44bca8c518d888b83878ce5 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Mon, 3 Jul 2017 16:49:25 +0200 Subject: [PATCH 1/3] Add bash completion for `docker config` command family This adds bash completion for - https://github.com/docker/cli/pull/45 - https://github.com/moby/moby/pull/32336 Signed-off-by: Harald Albers --- contrib/completion/bash/docker | 160 +++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index 03e0bc625a..ebfbd30bee 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -20,6 +20,7 @@ # For several commands, the amount of completions can be configured by # setting environment variables. # +# DOCKER_COMPLETION_SHOW_CONFIG_IDS # DOCKER_COMPLETION_SHOW_CONTAINER_IDS # DOCKER_COMPLETION_SHOW_NETWORK_IDS # DOCKER_COMPLETION_SHOW_NODE_IDS @@ -61,6 +62,42 @@ __docker_q() { docker ${host:+-H "$host"} ${config:+--config "$config"} 2>/dev/null "$@" } +# __docker_configs returns a list of configs. Additional options to +# `docker config ls` may be specified in order to filter the list, e.g. +# `__docker_configs --filter label=stage=production`. +# By default, only names are returned. +# Set DOCKER_COMPLETION_SHOW_CONFIG_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_configs() { + local format + if [ "$1" = "--id" ] ; then + format='{{.ID}}' + shift + elif [ "$1" = "--name" ] ; then + format='{{.Name}}' + shift + elif [ "$DOCKER_COMPLETION_SHOW_CONFIG_IDS" = yes ] ; then + format='{{.ID}} {{.Name}}' + else + format='{{.Name}}' + fi + + __docker_q config ls --format "$format" "$@" +} + +# __docker_complete_configs applies completion of configs based on the current value +# of `$cur` or the value of the optional first option `--cur`, if given. +__docker_complete_configs() { + local current="$cur" + if [ "$1" = "--cur" ] ; then + current="$2" + shift 2 + fi + COMPREPLY=( $(compgen -W "$(__docker_configs "$@")" -- "$current") ) +} + # __docker_containers returns a list of containers. Additional options to # `docker ps` may be specified in order to filter the list, e.g. # `__docker_containers --filter status=running` @@ -1096,6 +1133,117 @@ _docker_checkpoint_rm() { } +_docker_config() { + local subcommands=" + create + inspect + ls + rm + " + local aliases=" + list + remove + " + __docker_subcommands "$subcommands $aliases" && return + + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) + ;; + *) + COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) ) + ;; + esac +} + +_docker_config_create() { + case "$prev" in + --label|-l) + return + ;; + esac + + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "--help --label -l" -- "$cur" ) ) + ;; + *) + local counter=$(__docker_pos_first_nonflag '--label|-l') + if [ $cword -eq $((counter + 1)) ]; then + _filedir + fi + ;; + esac +} + +_docker_config_inspect() { + case "$prev" in + --format|-f) + return + ;; + esac + + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "--format -f --help --pretty" -- "$cur" ) ) + ;; + *) + __docker_complete_configs + ;; + esac +} + +_docker_config_list() { + _docker_config_ls +} + +_docker_config_ls() { + local key=$(__docker_map_key_of_current_option '--filter|-f') + case "$key" in + id) + __docker_complete_configs --cur "${cur##*=}" --id + return + ;; + name) + __docker_complete_configs --cur "${cur##*=}" --name + return + ;; + esac + + case "$prev" in + --filter|-f) + COMPREPLY=( $( compgen -S = -W "id label name" -- "$cur" ) ) + __docker_nospace + return + ;; + --format) + return + ;; + esac + + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "--format --filter -f --help --quiet -q" -- "$cur" ) ) + ;; + esac +} + +_docker_config_remove() { + _docker_config_rm +} + +_docker_config_rm() { + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) + ;; + *) + __docker_complete_configs + ;; + esac +} + + _docker_container() { local subcommands=" attach @@ -3146,6 +3294,7 @@ _docker_service_update_and_create() { if [ "$subcommand" = "create" ] ; then options_with_args="$options_with_args + --config --constraint --container-label --dns @@ -3162,6 +3311,10 @@ _docker_service_update_and_create() { " case "$prev" in + --config) + __docker_complete_configs + return + ;; --env-file) _filedir return @@ -3196,6 +3349,8 @@ _docker_service_update_and_create() { if [ "$subcommand" = "update" ] ; then options_with_args="$options_with_args --args + --config-add + --config-rm --constraint-add --constraint-rm --container-label-add @@ -3223,6 +3378,10 @@ _docker_service_update_and_create() { " case "$prev" in + --config-add|--config-rm) + __docker_complete_configs + return + ;; --group-add|--group-rm) COMPREPLY=( $(compgen -g -- "$cur") ) return @@ -4594,6 +4753,7 @@ _docker() { shopt -s extglob local management_commands=( + config container image network From 211bd55ae736705a48908833a5e10491f613e255 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Mon, 3 Jul 2017 16:55:16 +0200 Subject: [PATCH 2/3] Add bash completion for `secret inspect --pretty` Signed-off-by: Harald Albers --- contrib/completion/bash/docker | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index ebfbd30bee..e6f3063a29 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -4151,7 +4151,7 @@ _docker_secret_inspect() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) ) + COMPREPLY=( $( compgen -W "--format -f --help --pretty" -- "$cur" ) ) ;; *) __docker_complete_secrets From d585e554d5e3a16f424cb3d92d0f7be342b8c7ca Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Mon, 3 Jul 2017 16:55:40 +0200 Subject: [PATCH 3/3] Refactor alias delegation in bash completion for `secret rm|remove` The completion logic should be implemented in the documented subcommands (ls, rm) and delegated to from the aliases (list, remove). For the rm|remove pair, this was implemented vice versa. Signed-off-by: Harald Albers --- contrib/completion/bash/docker | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index e6f3063a29..15fdec5ac0 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -4195,6 +4195,10 @@ _docker_secret_ls() { } _docker_secret_remove() { + _docker_secret_rm +} + +_docker_secret_rm() { case "$cur" in -*) COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) @@ -4205,10 +4209,6 @@ _docker_secret_remove() { esac } -_docker_secret_rm() { - _docker_secret_remove -} - _docker_search() {