Skip to content

Commit b4bec22

Browse files
authored
Add bash completion for services defined in ~/.pg_service.conf (#1491)
1 parent 12ae730 commit b4bec22

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Contributors:
138138
* Mathieu Dupuy (deronnax)
139139
* Chris Novakovic
140140
* Josh Lynch (josh-lynch)
141+
* Fabio (3ximus)
141142

142143
Creator:
143144
--------

changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Features
55
--------
66
* Add a `--ping` command line option; allows pgcli to replace `pg_isready`
77
* Changed the packaging metadata from setup.py to pyproject.toml
8+
* Add bash completion for services defined in the service file `~/.pg_service.conf`
89

910
Bug fixes:
1011
----------

pgcli-completion.bash

+36-21
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,33 @@ _pg_databases()
33
# -w was introduced in 8.4, https://launchpad.net/bugs/164772
44
# "Access privileges" in output may contain linefeeds, hence the NF > 1
55
COMPREPLY=( $( compgen -W "$( psql -AtqwlF $'\t' 2>/dev/null | \
6-
awk 'NF > 1 { print $1 }' )" -- "$cur" ) )
6+
awk 'NF > 1 { print $1 }' )" -- "$cur" ) )
77
}
8-
8+
99
_pg_users()
1010
{
1111
# -w was introduced in 8.4, https://launchpad.net/bugs/164772
1212
COMPREPLY=( $( compgen -W "$( psql -Atqwc 'select usename from pg_user' \
1313
template1 2>/dev/null )" -- "$cur" ) )
1414
[[ ${#COMPREPLY[@]} -eq 0 ]] && COMPREPLY=( $( compgen -u -- "$cur" ) )
1515
}
16-
16+
17+
_pg_services()
18+
{
19+
# return list of available services
20+
local services
21+
if [[ -f "$HOME/.pg_service.conf" ]]; then
22+
services=$(grep -oP '(?<=^\[).*?(?=\])' "$HOME/.pg_service.conf")
23+
fi
24+
local suffix="${cur#*=}"
25+
COMPREPLY=( $(compgen -W "$services" -- "$suffix") )
26+
}
27+
1728
_pgcli()
1829
{
1930
local cur prev words cword
2031
_init_completion -s || return
21-
32+
2233
case $prev in
2334
-h|--host)
2435
_known_hosts_real "$cur"
@@ -39,23 +50,27 @@ _pgcli()
3950
esac
4051

4152
case "$cur" in
42-
--*)
43-
# return list of available options
44-
COMPREPLY=( $( compgen -W '--host --port --user --password --no-password
45-
--single-connection --version --dbname --pgclirc --dsn
46-
--row-limit --help' -- "$cur" ) )
47-
[[ $COMPREPLY == *= ]] && compopt -o nospace
48-
return 0
49-
;;
50-
-)
51-
# only complete long options
52-
compopt -o nospace
53-
COMPREPLY=( -- )
54-
return 0
55-
;;
56-
*)
53+
service=*)
54+
_pg_services
55+
return 0
56+
;;
57+
--*)
58+
# return list of available options
59+
COMPREPLY=( $( compgen -W '--host --port --user --password --no-password
60+
--single-connection --version --dbname --pgclirc --dsn
61+
--row-limit --help' -- "$cur" ) )
62+
[[ $COMPREPLY == *= ]] && compopt -o nospace
63+
return 0
64+
;;
65+
-)
66+
# only complete long options
67+
compopt -o nospace
68+
COMPREPLY=( -- )
69+
return 0
70+
;;
71+
*)
5772
# return list of available databases
58-
_pg_databases
73+
_pg_databases
5974
esac
60-
} &&
75+
} &&
6176
complete -F _pgcli pgcli

0 commit comments

Comments
 (0)