Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash completion for docker context command family #1619

Merged
merged 2 commits into from
Jan 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 204 additions & 2 deletions contrib/completion/bash/docker
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ __docker_previous_extglob_setting=$(shopt -p extglob)
shopt -s extglob

__docker_q() {
docker ${host:+-H "$host"} ${config:+--config "$config"} 2>/dev/null "$@"
docker ${host:+--host "$host"} ${config:+--config "$config"} ${context:+--context "$context"} 2>/dev/null "$@"
}

# __docker_configs returns a list of configs. Additional options to
Expand Down Expand Up @@ -178,6 +178,31 @@ __docker_complete_container_ids() {
COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") )
}

# __docker_contexts returns a list of contexts without the special "default" context.
# Completions may be added with `--add`, e.g. `--add default`.
__docker_contexts() {
local add=()
while true ; do
case "$1" in
--add)
add+=("$2")
shift 2
;;
*)
break
;;
esac
done
__docker_q context ls -q
echo "${add[@]}"
}

__docker_complete_contexts() {
local contexts=( $(__docker_contexts "$@") )
COMPREPLY=( $(compgen -W "${contexts[*]}" -- "$cur") )
}


# __docker_images returns a list of images. For each image, up to three representations
# can be generated: the repository (e.g. busybox), repository:tag (e.g. busybox:latest)
# and the ID (e.g. sha256:ee22cbbd4ea3dff63c86ba60c7691287c321e93adfc1009604eb1dde7ec88645).
Expand Down Expand Up @@ -1133,6 +1158,10 @@ _docker_docker() {
_filedir -d
return
;;
--context|-c)
__docker_complete_contexts
return
;;
--log-level|-l)
__docker_complete_log_levels
return
Expand Down Expand Up @@ -2234,6 +2263,172 @@ _docker_container_wait() {
}


_docker_context() {
local subcommands="
create
export
import
inspect
ls
rm
update
use
"
local aliases="
list
remove
"
__docker_subcommands "$subcommands $aliases" && return

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) )
;;
esac
}

_docker_context_create() {
case "$prev" in
--default-stack-orchestrator)
COMPREPLY=( $( compgen -W "all kubernetes swarm" -- "$cur" ) )
return
;;
--description|--docker|--kubernetes)
return
;;
esac

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--default-stack-orchestrator --description --docker --help --kubernetes" -- "$cur" ) )
;;
esac
}

_docker_context_export() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --kubeconfig" -- "$cur" ) )
;;
*)
local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then
__docker_complete_contexts
elif [ "$cword" -eq "$((counter + 1))" ]; then
_filedir
fi
;;
esac
}

_docker_context_import() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then
:
elif [ "$cword" -eq "$((counter + 1))" ]; then
_filedir
fi
;;
esac
}

_docker_context_inspect() {
case "$prev" in
--format|-f)
return
;;
esac

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
;;
*)
__docker_complete_contexts
;;
esac
}

_docker_context_list() {
_docker_context_ls
}

_docker_context_ls() {
case "$prev" in
--format|-f)
return
;;
esac

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--format -f --help --quiet -q" -- "$cur" ) )
;;
esac
}

_docker_context_remove() {
_docker_context_rm
}

_docker_context_rm() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
;;
*)
__docker_complete_contexts
;;
esac
}

_docker_context_update() {
case "$prev" in
--default-stack-orchestrator)
COMPREPLY=( $( compgen -W "all kubernetes swarm" -- "$cur" ) )
return
;;
--description|--docker|--kubernetes)
return
;;
esac

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--default-stack-orchestrator --description --docker --help --kubernetes" -- "$cur" ) )
;;
*)
local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then
__docker_complete_contexts
fi
;;
esac
}

_docker_context_use() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
local counter=$(__docker_pos_first_nonflag)
if [ "$cword" -eq "$counter" ]; then
__docker_complete_contexts --add default
fi
;;
esac
}


_docker_commit() {
_docker_container_commit
}
Expand Down Expand Up @@ -5147,6 +5342,7 @@ _docker() {
local management_commands=(
config
container
context
image
network
node
Expand Down Expand Up @@ -5227,6 +5423,7 @@ _docker() {
"
local global_options_with_args="
--config
--context -c
--host -H
--log-level -l
--tlscacert
Expand All @@ -5239,7 +5436,7 @@ _docker() {
# variables to cache client info, populated on demand for performance reasons
local client_experimental stack_orchestrator_is_kubernetes stack_orchestrator_is_swarm

local host config
local host config context

COMPREPLY=()
local cur prev words cword
Expand All @@ -5262,6 +5459,11 @@ _docker() {
(( counter++ ))
config="${words[$counter]}"
;;
# save context so that completion can use custom daemon
--context|-c)
(( counter++ ))
context="${words[$counter]}"
;;
$(__docker_to_extglob "$global_options_with_args") )
(( counter++ ))
;;
Expand Down