Optimize the bash completion even further

The biggest/bestest change here is cutting down on the number of calls to Docker in the filtering helpers (`__docker_containers_running`, etc), especially calls to the really heavy `docker images`.

Signed-off-by: Andrew Page <admwiggin@gmail.com>
master
Tianon Gravi 2014-10-02 15:13:37 -06:00 committed by Tibor Vass
parent e775b26a78
commit f071599471
1 changed files with 116 additions and 174 deletions

View File

@ -25,79 +25,59 @@ __docker_q() {
docker 2>/dev/null "$@" docker 2>/dev/null "$@"
} }
__docker_containers_all() __docker_containers_all() {
{ local IFS=$'\n'
local containers="$( __docker_q ps -a -q )" local containers=( $(__docker_q ps -aq --no-trunc) )
local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )" if [ "$1" ]; then
COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) ) containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") )
fi
local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
unset IFS
COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") )
} }
__docker_containers_running() __docker_containers_running() {
{ __docker_containers_all '.State.Running'
local containers="$( __docker_q ps -q )"
local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )"
COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
} }
__docker_containers_stopped() __docker_containers_stopped() {
{ __docker_containers_all 'not .State.Running'
local containers="$( { __docker_q ps -a -q; __docker_q ps -q; } | sort | uniq -u )"
local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )"
COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
} }
__docker_containers_paused() __docker_containers_pauseable() {
{ __docker_containers_all 'and .State.Running (not .State.Paused)'
local containers="$( __docker_q ps -q)"
local names="$( __docker_q inspect --format='{{.State.Paused}} {{.Name}}' $containers | sed 's/^false.*//' | sed 's,^true /,,' )"
local hostnames="$( __docker_q inspect --format='{{.State.Paused}} {{.Config.Hostname}}' $containers | sed 's/^false.*//' | sed 's,^true ,,' )"
COMPREPLY=( $( compgen -W "$names $hostnames" -- "$cur" ) )
} }
__docker_containers_not_paused() __docker_containers_unpauseable() {
{ __docker_containers_all '.State.Paused'
local containers="$( __docker_q ps -q)"
local names="$( __docker_q inspect --format='{{.State.Paused}} {{.Name}}' $containers | sed 's/^true.*//' | sed 's,^false /,,' )"
local hostnames="$( __docker_q inspect --format='{{.State.Paused}} {{.Config.Hostname}}' $containers | sed 's/^true.*//' | sed 's/^false //' )"
COMPREPLY=( $( compgen -W "$names $hostnames" -- "$cur" ) )
} }
__docker_image_repos() __docker_image_repos() {
{ local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )" COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
COMPREPLY=( $( compgen -W "$repos" -- "$cur" ) )
} }
__docker_image_repos_and_tags() __docker_image_repos_and_tags() {
{ local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )" COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
local images="$( __docker_q images | awk 'NR>1{print $1":"$2}' | grep -v '^<none>:' )"
COMPREPLY=( $( compgen -W "$repos $images" -- "$cur" ) )
__ltrim_colon_completions "$cur" __ltrim_colon_completions "$cur"
} }
__docker_image_repos_and_tags_and_ids() __docker_image_repos_and_tags_and_ids() {
{ local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')"
local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )" COMPREPLY=( $(compgen -W "$images" -- "$cur") )
local images="$( __docker_q images | awk 'NR>1{print $1":"$2}' | grep -v '^<none>:' )"
local ids="$( __docker_q images -a -q )"
COMPREPLY=( $( compgen -W "$repos $images $ids" -- "$cur" ) )
__ltrim_colon_completions "$cur" __ltrim_colon_completions "$cur"
} }
__docker_containers_and_images() __docker_containers_and_images() {
{ __docker_containers_all
local containers="$( __docker_q ps -a -q )" local containers=( "${COMPREPLY[@]}" )
local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )" __docker_image_repos_and_tags_and_ids
local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )" COMPREPLY+=( "${containers[@]}" )
local images="$( __docker_q images | awk 'NR>1{print $1":"$2}' | grep -v '^<none>:' )"
local ids="$( __docker_q images -a -q )"
COMPREPLY=( $( compgen -W "$containers $names $repos $images $ids" -- "$cur" ) )
__ltrim_colon_completions "$cur"
} }
__docker_pos_first_nonflag() __docker_pos_first_nonflag() {
{
local argument_flags=$1 local argument_flags=$1
local counter=$cpos local counter=$cpos
@ -119,8 +99,7 @@ __docker_pos_first_nonflag()
echo $counter echo $counter
} }
_docker_docker() _docker_docker() {
{
case "$prev" in case "$prev" in
-H) -H)
return return
@ -134,13 +113,12 @@ _docker_docker()
COMPREPLY=( $( compgen -W "-H" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-H" -- "$cur" ) )
;; ;;
*) *)
COMPREPLY=( $( compgen -W "$commands help" -- "$cur" ) ) COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
;; ;;
esac esac
} }
_docker_attach() _docker_attach() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--no-stdin --sig-proxy" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--no-stdin --sig-proxy" -- "$cur" ) )
@ -154,8 +132,7 @@ _docker_attach()
esac esac
} }
_docker_build() _docker_build() {
{
case "$prev" in case "$prev" in
-t|--tag) -t|--tag)
__docker_image_repos_and_tags __docker_image_repos_and_tags
@ -178,8 +155,7 @@ _docker_build()
esac esac
} }
_docker_commit() _docker_commit() {
{
case "$prev" in case "$prev" in
-m|--message|-a|--author|--run) -m|--message|-a|--author|--run)
return return
@ -209,8 +185,7 @@ _docker_commit()
esac esac
} }
_docker_cp() _docker_cp() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
case "$cur" in case "$cur" in
@ -233,8 +208,7 @@ _docker_cp()
fi fi
} }
_docker_create() _docker_create() {
{
case "$prev" in case "$prev" in
-a|--attach) -a|--attach)
COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) ) COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
@ -302,16 +276,14 @@ _docker_create()
esac esac
} }
_docker_diff() _docker_diff() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_containers_all __docker_containers_all
fi fi
} }
_docker_events() _docker_events() {
{
case "$prev" in case "$prev" in
--since) --since)
return return
@ -329,8 +301,7 @@ _docker_events()
esac esac
} }
_docker_exec() _docker_exec() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-d --detach -i --interactive -t --tty" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-d --detach -i --interactive -t --tty" -- "$cur" ) )
@ -341,24 +312,21 @@ _docker_exec()
esac esac
} }
_docker_export() _docker_export() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_containers_all __docker_containers_all
fi fi
} }
_docker_help() _docker_help() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) ) COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
fi fi
} }
_docker_history() _docker_history() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-q --quiet --no-trunc" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-q --quiet --no-trunc" -- "$cur" ) )
@ -372,8 +340,7 @@ _docker_history()
esac esac
} }
_docker_images() _docker_images() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-q --quiet -a --all --no-trunc -v --viz -t --tree" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-q --quiet -a --all --no-trunc -v --viz -t --tree" -- "$cur" ) )
@ -387,8 +354,7 @@ _docker_images()
esac esac
} }
_docker_import() _docker_import() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
return return
@ -401,13 +367,11 @@ _docker_import()
fi fi
} }
_docker_info() _docker_info() {
{
return return
} }
_docker_inspect() _docker_inspect() {
{
case "$prev" in case "$prev" in
-f|--format) -f|--format)
return return
@ -426,18 +390,15 @@ _docker_inspect()
esac esac
} }
_docker_kill() _docker_kill() {
{
__docker_containers_running __docker_containers_running
} }
_docker_load() _docker_load() {
{
return return
} }
_docker_login() _docker_login() {
{
case "$prev" in case "$prev" in
-u|--username|-p|--password|-e|--email) -u|--username|-p|--password|-e|--email)
return return
@ -455,8 +416,7 @@ _docker_login()
esac esac
} }
_docker_logs() _docker_logs() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-f --follow" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-f --follow" -- "$cur" ) )
@ -469,24 +429,22 @@ _docker_logs()
;; ;;
esac esac
} }
_docker_pause()
{ _docker_pause() {
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_containers_not_paused __docker_containers_pauseable
fi fi
} }
_docker_port() _docker_port() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_containers_all __docker_containers_all
fi fi
} }
_docker_ps() _docker_ps() {
{
case "$prev" in case "$prev" in
--since|--before) --since|--before)
__docker_containers_all __docker_containers_all
@ -507,8 +465,7 @@ _docker_ps()
esac esac
} }
_docker_pull() _docker_pull() {
{
case "$prev" in case "$prev" in
-t|--tag) -t|--tag)
return return
@ -530,16 +487,14 @@ _docker_pull()
esac esac
} }
_docker_push() _docker_push() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_image_repos_and_tags __docker_image_repos_and_tags
fi fi
} }
_docker_restart() _docker_restart() {
{
case "$prev" in case "$prev" in
-t|--time) -t|--time)
return return
@ -558,8 +513,7 @@ _docker_restart()
esac esac
} }
_docker_rm() _docker_rm() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-f --force -l --link -v --volumes" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-f --force -l --link -v --volumes" -- "$cur" ) )
@ -581,13 +535,11 @@ _docker_rm()
esac esac
} }
_docker_rmi() _docker_rmi() {
{
__docker_image_repos_and_tags_and_ids __docker_image_repos_and_tags_and_ids
} }
_docker_run() _docker_run() {
{
case "$prev" in case "$prev" in
-a|--attach) -a|--attach)
COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) ) COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
@ -656,16 +608,14 @@ _docker_run()
esac esac
} }
_docker_save() _docker_save() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_image_repos_and_tags_and_ids __docker_image_repos_and_tags_and_ids
fi fi
} }
_docker_search() _docker_search() {
{
case "$prev" in case "$prev" in
-s|--stars) -s|--stars)
return return
@ -683,8 +633,7 @@ _docker_search()
esac esac
} }
_docker_start() _docker_start() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) )
@ -695,8 +644,7 @@ _docker_start()
esac esac
} }
_docker_stop() _docker_stop() {
{
case "$prev" in case "$prev" in
-t|--time) -t|--time)
return return
@ -715,8 +663,7 @@ _docker_stop()
esac esac
} }
_docker_tag() _docker_tag() {
{
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) )
@ -738,73 +685,68 @@ _docker_tag()
esac esac
} }
_docker_unpause() _docker_unpause() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_containers_paused __docker_containers_unpauseable
fi fi
} }
_docker_top() _docker_top() {
{
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_containers_running __docker_containers_running
fi fi
} }
_docker_version() _docker_version() {
{
return return
} }
_docker_wait() _docker_wait() {
{
__docker_containers_all __docker_containers_all
} }
_docker() _docker() {
{ local commands=(
local commands=" attach
attach build
build commit
commit cp
cp create
create diff
diff events
events exec
exec export
export history
history images
images import
import info
info insert
insert inspect
inspect kill
kill load
load login
login logs
logs pause
pause port
port ps
ps pull
pull push
push restart
restart rm
rm rmi
rmi run
run save
save search
search start
start stop
stop tag
tag top
top unpause
unpause version
version wait
wait )
"
COMPREPLY=() COMPREPLY=()
local cur prev words cword local cur prev words cword