Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bash completion for services defined in ~/.pg_service.conf #1491

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Contributors:
* Mathieu Dupuy (deronnax)
* Chris Novakovic
* Josh Lynch (josh-lynch)
* Fabio (3ximus)

Creator:
--------
Expand Down
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Features
--------
* Add a `--ping` command line option; allows pgcli to replace `pg_isready`
* Changed the packaging metadata from setup.py to pyproject.toml
* Add bash completion for services defined in the service file `~/.pg_service.conf`

Bug fixes:
----------
Expand Down
57 changes: 36 additions & 21 deletions pgcli-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@ _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" ) )
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" ) )
}


_pg_services()
{
# return list of available services
local services
if [[ -f "$HOME/.pg_service.conf" ]]; then
services=$(grep -oP '(?<=^\[).*?(?=\])' "$HOME/.pg_service.conf")
fi
local suffix="${cur#*=}"
COMPREPLY=( $(compgen -W "$services" -- "$suffix") )
}

_pgcli()
{
local cur prev words cword
_init_completion -s || return

case $prev in
-h|--host)
_known_hosts_real "$cur"
Expand All @@ -39,23 +50,27 @@ _pgcli()
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
;;
*)
service=*)
_pg_services
return 0
;;
--*)
# 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
_pg_databases
esac
} &&
} &&
complete -F _pgcli pgcli
Loading