Skip to content

Commit

Permalink
Support PGAPPNAME (#1444)
Browse files Browse the repository at this point in the history
The application_name to be used when connecting to a database can now be
specified as a command line argument (--application-name) or be taken
directly from environment variables (PGAPPNAME). It still defaults to
'pgcli' when not specified.

Closes #1421.
  • Loading branch information
crazybolillo authored Dec 9, 2023
1 parent f2156b3 commit e2ff38d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Contributors:
* Rob Berry (rob-b)
* Sharon Yogev (sharonyogev)
* Hollis Wu (holi0317)
* Antonio Aguilar (crazybolillo)

Creator:
--------
Expand Down
4 changes: 4 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Upcoming
========

Features:
---------
* Support `PGAPPNAME` as an environment variable and `--application-name` as a command line argument.

Bug fixes:
----------

Expand Down
13 changes: 12 additions & 1 deletion pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def __init__(
pgexecute=None,
pgclirc_file=None,
row_limit=None,
application_name="pgcli",
single_connection=False,
less_chatty=None,
prompt=None,
Expand Down Expand Up @@ -210,6 +211,8 @@ def __init__(
else:
self.row_limit = c["main"].as_int("row_limit")

self.application_name = application_name

# if not specified, set to DEFAULT_MAX_FIELD_WIDTH
# if specified but empty, set to None to disable truncation
# ellipsis will take at least 3 symbols, so this can't be less than 3 if specified and > 0
Expand Down Expand Up @@ -568,7 +571,7 @@ def connect(
if not database:
database = user

kwargs.setdefault("application_name", "pgcli")
kwargs.setdefault("application_name", self.application_name)

# If password prompt is not forced but no password is provided, try
# getting it from environment variable.
Expand Down Expand Up @@ -1337,6 +1340,12 @@ def echo_via_pager(self, text, color=None):
type=click.INT,
help="Set threshold for row limit prompt. Use 0 to disable prompt.",
)
@click.option(
"--application-name",
default="pgcli",
envvar="PGAPPNAME",
help="Application name for the connection.",
)
@click.option(
"--less-chatty",
"less_chatty",
Expand Down Expand Up @@ -1387,6 +1396,7 @@ def cli(
pgclirc,
dsn,
row_limit,
application_name,
less_chatty,
prompt,
prompt_dsn,
Expand Down Expand Up @@ -1445,6 +1455,7 @@ def cli(
never_prompt,
pgclirc_file=pgclirc,
row_limit=row_limit,
application_name=application_name,
single_connection=single_connection,
less_chatty=less_chatty,
prompt=prompt,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_application_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from unittest.mock import patch

from click.testing import CliRunner

from pgcli.main import cli
from pgcli.pgexecute import PGExecute


def test_application_name_in_env():
runner = CliRunner()
app_name = "wonderful_app"
with patch.object(PGExecute, "__init__") as mock_pgxecute:
runner.invoke(
cli, ["127.0.0.1:5432/hello", "user"], env={"PGAPPNAME": app_name}
)
kwargs = mock_pgxecute.call_args.kwargs
assert kwargs.get("application_name") == app_name

0 comments on commit e2ff38d

Please sign in to comment.