summaryrefslogtreecommitdiffstats
path: root/pgcli-completion.bash
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-02-08 10:31:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-02-08 10:31:05 +0000
commit6884720fae8a2622b14e93d9e35ca5fcc2283b40 (patch)
treedf6f736bb623cdd7932bbe2256101a6ac4ef7f35 /pgcli-completion.bash
parentInitial commit. (diff)
downloadpgcli-6884720fae8a2622b14e93d9e35ca5fcc2283b40.tar.xz
pgcli-6884720fae8a2622b14e93d9e35ca5fcc2283b40.zip
Adding upstream version 3.1.0.upstream/3.1.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pgcli-completion.bash')
-rw-r--r--pgcli-completion.bash61
1 files changed, 61 insertions, 0 deletions
diff --git a/pgcli-completion.bash b/pgcli-completion.bash
new file mode 100644
index 0000000..3549b56
--- /dev/null
+++ b/pgcli-completion.bash
@@ -0,0 +1,61 @@
+_pg_databases()
+{
+ # -w was introduced in 8.4, https://launchpad.net/bugs/164772
+ # "Access privileges" in output may contain linefeeds, hence the NF > 1
+ COMPREPLY=( $( compgen -W "$( psql -AtqwlF $'\t' 2>/dev/null | \
+ awk 'NF > 1 { print $1 }' )" -- "$cur" ) )
+}
+
+_pg_users()
+{
+ # -w was introduced in 8.4, https://launchpad.net/bugs/164772
+ COMPREPLY=( $( compgen -W "$( psql -Atqwc 'select usename from pg_user' \
+ template1 2>/dev/null )" -- "$cur" ) )
+ [[ ${#COMPREPLY[@]} -eq 0 ]] && COMPREPLY=( $( compgen -u -- "$cur" ) )
+}
+
+_pgcli()
+{
+ local cur prev words cword
+ _init_completion -s || return
+
+ case $prev in
+ -h|--host)
+ _known_hosts_real "$cur"
+ return 0
+ ;;
+ -U|--user)
+ _pg_users
+ return 0
+ ;;
+ -d|--dbname)
+ _pg_databases
+ return 0
+ ;;
+ --help|-v|--version|-p|--port|-R|--row-limit)
+ # all other arguments are noop with these
+ return 0
+ ;;
+ esac
+
+ case "$cur" in
+ --*)
+ # return list of available options
+ COMPREPLY=( $( compgen -W '--host --port --user --password --no-password
+ --single-connection --version --dbname --pgclirc --dsn
+ --row-limit --help' -- "$cur" ) )
+ [[ $COMPREPLY == *= ]] && compopt -o nospace
+ return 0
+ ;;
+ -)
+ # only complete long options
+ compopt -o nospace
+ COMPREPLY=( -- )
+ return 0
+ ;;
+ *)
+ # return list of available databases
+ _pg_databases
+ esac
+} &&
+complete -F _pgcli pgcli