167 lines
4.0 KiB
Plaintext
Raw Normal View History

2009-05-21 01:21:31 +02:00
# bash completion for Postgresql
have psql && {
_pg_databases()
{
2009-10-04 19:42:50 +02:00
return # See https://launchpad.net/bugs/164772
COMPREPLY=( $( compgen -W "$( psql -l 2>/dev/null | \
sed -e '1,/^-/d' -e '/^(/,$d' | \
awk '{print $1}' )" -- "$cur" ) )
2009-05-21 01:21:31 +02:00
}
_pg_users()
{
2009-10-04 19:42:50 +02:00
# See https://launchpad.net/bugs/164772
#COMPREPLY=( $( psql -qtc 'select usename from pg_user' template1 2>/dev/null | \
# grep "^ $cur" ) )
#[ ${#COMPREPLY[@]} -eq 0 ] && COMPREPLY=( $( compgen -u -- $cur ) )
COMPREPLY=( $( compgen -u -- "$cur" ) )
2009-05-21 01:21:31 +02:00
}
# createdb(1) completion
#
_createdb()
{
2009-10-04 19:42:50 +02:00
local cur prev split=false
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
_split_longopt && split=true
case "$prev" in
-h|--host)
_known_hosts_real "$cur"
return 0
;;
-U|--username|-O|--owner)
_pg_users
return 0
;;
-p|--port|-D|--tablespace|-E|--encoding|-T|--template)
# argument required but no completions available
return 0
;;
--help|--version)
# all other arguments are noop with these
return 0
;;
esac
$split && return 0
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '--tablespace --template --encoding --host \
--port --username --password --echo --quiet --help --version' \
-- "$cur" ) )
2009-10-04 19:42:50 +02:00
else
_pg_databases
fi
2009-05-21 01:21:31 +02:00
}
complete -F _createdb $default createdb
# dropdb(1) completion
#
_dropdb()
{
2009-10-04 19:42:50 +02:00
local cur prev split=false
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
_split_longopt && split=true
case "$prev" in
-h|--host)
_known_hosts_real "$cur"
return 0
;;
-U|--username)
_pg_users
return 0
;;
--help|--version)
# all other arguments are noop with these
return 0
;;
esac
$split && return 0
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '--host --port --username --password \
--interactive --echo --quiet --help --version' -- "$cur" ) )
2009-10-04 19:42:50 +02:00
else
_pg_databases
fi
2009-05-21 01:21:31 +02:00
}
complete -F _dropdb $default dropdb
# psql(1) completion
#
_psql()
{
2009-10-04 19:42:50 +02:00
local cur prev split=false
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
_split_longopt && split=true
case "$prev" in
-h|--host)
_known_hosts_real "$cur"
return 0
;;
-U|--username)
_pg_users
return 0
;;
-d|--dbname)
_pg_databases
return 0
;;
-o|--output|-f|--file|-L|--log-file)
_filedir
return 0
;;
-c|--command|-F|--field-separator|-p|--port|-P|--pset|\
-R|--record-separator|-T|--table-attr|-v|--set|--variable)
# argument required but no completions available
return 0
;;
-\?|--help|-V|--version)
# all other arguments are noop with these
return 0
;;
esac
$split && return 0
if [[ "$cur" == -* ]]; then
# return list of available options
COMPREPLY=( $( compgen -W '--echo-all --no-align --command --dbname \
--echo-queries --echo-hidden --file --field-separator --host \
--html --list --log-file --output --port --pset --quiet \
--record-separator --single-step --single-line --tuples-only \
--table-attr --username --set --version --password --expanded \
--no-psqlrc --single-transaction --help' -- "$cur" ) )
2009-10-04 19:42:50 +02:00
else
# return list of available databases
_pg_databases
fi
2009-05-21 01:21:31 +02:00
}
complete -F _psql $filenames psql
2009-05-21 01:21:31 +02:00
}
# Local variables:
# mode: shell-script
2009-10-04 19:42:50 +02:00
# sh-basic-offset: 4
# sh-indent-comment: t
2009-10-04 19:42:50 +02:00
# indent-tabs-mode: nil
# End:
2009-10-04 19:42:50 +02:00
# ex: ts=4 sw=4 et filetype=sh