Merge pull request #8364 from tianon/bash-completion-filters

Optimize the bash completion even further
This commit is contained in:
Jessie Frazelle 2014-10-02 15:00:01 -07:00
commit 45667fb1bb
1 changed files with 129 additions and 154 deletions

View File

@ -25,63 +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_image_repos() __docker_containers_pauseable() {
{ __docker_containers_all 'and .State.Running (not .State.Paused)'
local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )" }
__docker_containers_unpauseable() {
__docker_containers_all '.State.Paused'
}
__docker_image_repos() {
local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
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
@ -103,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
@ -118,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" ) )
@ -138,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
@ -162,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
@ -193,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
@ -217,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" ) )
@ -286,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
@ -313,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" ) )
@ -325,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" ) )
@ -356,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" ) )
@ -371,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
@ -385,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
@ -410,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
@ -439,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" ) )
@ -454,16 +430,21 @@ _docker_logs()
esac esac
} }
_docker_port() _docker_pause() {
{ local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then
__docker_containers_pauseable
fi
}
_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
@ -484,8 +465,7 @@ _docker_ps()
esac esac
} }
_docker_pull() _docker_pull() {
{
case "$prev" in case "$prev" in
-t|--tag) -t|--tag)
return return
@ -507,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
@ -535,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" ) )
@ -558,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" ) )
@ -633,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
@ -660,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" ) )
@ -672,8 +644,7 @@ _docker_start()
esac esac
} }
_docker_stop() _docker_stop() {
{
case "$prev" in case "$prev" in
-t|--time) -t|--time)
return return
@ -692,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" ) )
@ -715,27 +685,30 @@ _docker_tag()
esac esac
} }
_docker_top() _docker_unpause() {
{ local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then
__docker_containers_unpauseable
fi
}
_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
@ -755,6 +728,7 @@ _docker()
load load
login login
logs logs
pause
port port
ps ps
pull pull
@ -769,9 +743,10 @@ _docker()
stop stop
tag tag
top top
unpause
version version
wait wait
" )
COMPREPLY=() COMPREPLY=()
local cur prev words cword local cur prev words cword