PageRenderTime 215ms CodeModel.GetById 212ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/bash_completion.d/postgresql

http://github.com/brinkman83/bashrc
#! | 171 lines | 150 code | 21 blank | 0 comment | 0 complexity | 7705b24b6fa63ce573c485d7cd1e13ff MD5 | raw file
  1# bash completion for Postgresql
  2
  3have psql && {
  4_pg_databases()
  5{
  6    return # See https://launchpad.net/bugs/164772
  7    COMPREPLY=( $( compgen -W "$( psql -l 2>/dev/null | \
  8        sed -e '1,/^-/d' -e '/^(/,$d' | \
  9        awk '{print $1}' )" -- "$cur" ) )
 10}
 11
 12_pg_users()
 13{
 14    # See https://launchpad.net/bugs/164772
 15    #COMPREPLY=( $( psql -qtc 'select usename from pg_user' template1 2>/dev/null | \
 16    #       grep "^ $cur" ) )
 17    #[ ${#COMPREPLY[@]} -eq 0 ] && COMPREPLY=( $( compgen -u -- $cur ) )
 18    COMPREPLY=( $( compgen -u -- "$cur" ) )
 19}
 20
 21# createdb(1) completion
 22#
 23_createdb()
 24{
 25    local cur prev split=false
 26
 27    COMPREPLY=()
 28    cur=`_get_cword`
 29    prev=${COMP_WORDS[COMP_CWORD-1]}
 30
 31    _split_longopt && split=true
 32
 33    case "$prev" in
 34        -h|--host)
 35            _known_hosts_real "$cur"
 36            return 0
 37            ;;
 38        -U|--username|-O|--owner)
 39            _pg_users
 40            return 0
 41            ;;
 42        -p|--port|-D|--tablespace|-E|--encoding|-T|--template)
 43            # argument required but no completions available
 44            return 0
 45            ;;
 46        --help|--version)
 47            # all other arguments are noop with these
 48            return 0
 49            ;;
 50    esac
 51
 52    $split && return 0
 53
 54    if [[ "$cur" == -* ]]; then
 55        COMPREPLY=( $( compgen -W '-D -T -E -h -p -U -W -e -q \
 56            --tablespace --template --encoding --host --port \
 57            --username --password --echo --quiet --help --version' -- "$cur" ))
 58    else
 59        _pg_databases
 60    fi
 61}
 62complete -F _createdb $default createdb
 63
 64# dropdb(1) completion
 65#
 66_dropdb()
 67{
 68    local cur prev split=false
 69
 70    COMPREPLY=()
 71    cur=`_get_cword`
 72    prev=${COMP_WORDS[COMP_CWORD-1]}
 73
 74    _split_longopt && split=true
 75
 76    case "$prev" in
 77        -h|--host)
 78            _known_hosts_real "$cur"
 79            return 0
 80            ;;
 81        -U|--username)
 82            _pg_users
 83            return 0
 84            ;;
 85        --help|--version)
 86            # all other arguments are noop with these
 87            return 0
 88            ;;
 89    esac
 90
 91    $split && return 0
 92
 93    if [[ "$cur" == -* ]]; then
 94        COMPREPLY=( $( compgen -W '-h -p -U -W -i -e -q \
 95            --host --port --username --password --interactive \
 96            --echo --quiet --help --version' -- "$cur" ) )
 97    else
 98        _pg_databases
 99    fi
100}
101complete -F _dropdb $default dropdb
102
103# psql(1) completion
104#
105_psql()
106{
107    local cur prev split=false
108
109    COMPREPLY=()
110    cur=`_get_cword`
111    prev=${COMP_WORDS[COMP_CWORD-1]}
112
113    _split_longopt && split=true
114
115    case "$prev" in
116        -h|--host)
117            _known_hosts_real "$cur"
118            return 0
119            ;;
120        -U|--username)
121            _pg_users
122            return 0
123            ;;
124        -d|--dbname)
125            _pg_databases
126            return 0
127            ;;
128        -o|--output|-f|--file|-L|--log-file)
129            _filedir
130            return 0
131            ;;
132        -c|--command|-F|--field-separator|-p|--port|-P|--pset|\
133        -R|--record-separator|-T|--table-attr|-v|--set|--variable)
134            # argument required but no completions available
135            return 0
136            ;;
137        -\?|--help|-V|--version)
138            # all other arguments are noop with these
139            return 0
140            ;;
141    esac
142
143    $split && return 0
144
145    if [[ "$cur" == -* ]]; then
146        # return list of available options
147        COMPREPLY=( $( compgen -W '-a --echo-all -A --no-align \
148            -c --command -d --dbname -e --echo-queries \
149            -E --echo-hidden -f --file -F --field-separator \
150            -h --host -H --html -l --list -L --log-file -n \
151            -o --output -p --port -P --pset -q --quiet \
152            -R --record-separator -s --single-step \
153            -S --single-line -t --tuples-only -T --table-attr \
154            -U --username -v --set --variable -V --version \
155            -W --password -x --expanded -X --no-psqlrc \
156            -1 --single-transaction -? --help' -- "$cur" ) )
157    else
158        # return list of available databases
159        _pg_databases
160    fi
161}
162complete -F _psql $filenames psql
163}
164
165# Local variables:
166# mode: shell-script
167# sh-basic-offset: 4
168# sh-indent-comment: t
169# indent-tabs-mode: nil
170# End:
171# ex: ts=4 sw=4 et filetype=sh