Skip to content

Commit e2ff38d

Browse files
authored
Support PGAPPNAME (#1444)
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.
1 parent f2156b3 commit e2ff38d

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Contributors:
131131
* Rob Berry (rob-b)
132132
* Sharon Yogev (sharonyogev)
133133
* Hollis Wu (holi0317)
134+
* Antonio Aguilar (crazybolillo)
134135

135136
Creator:
136137
--------

changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Upcoming
22
========
33

4+
Features:
5+
---------
6+
* Support `PGAPPNAME` as an environment variable and `--application-name` as a command line argument.
7+
48
Bug fixes:
59
----------
610

pgcli/main.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def __init__(
165165
pgexecute=None,
166166
pgclirc_file=None,
167167
row_limit=None,
168+
application_name="pgcli",
168169
single_connection=False,
169170
less_chatty=None,
170171
prompt=None,
@@ -210,6 +211,8 @@ def __init__(
210211
else:
211212
self.row_limit = c["main"].as_int("row_limit")
212213

214+
self.application_name = application_name
215+
213216
# if not specified, set to DEFAULT_MAX_FIELD_WIDTH
214217
# if specified but empty, set to None to disable truncation
215218
# ellipsis will take at least 3 symbols, so this can't be less than 3 if specified and > 0
@@ -568,7 +571,7 @@ def connect(
568571
if not database:
569572
database = user
570573

571-
kwargs.setdefault("application_name", "pgcli")
574+
kwargs.setdefault("application_name", self.application_name)
572575

573576
# If password prompt is not forced but no password is provided, try
574577
# getting it from environment variable.
@@ -1337,6 +1340,12 @@ def echo_via_pager(self, text, color=None):
13371340
type=click.INT,
13381341
help="Set threshold for row limit prompt. Use 0 to disable prompt.",
13391342
)
1343+
@click.option(
1344+
"--application-name",
1345+
default="pgcli",
1346+
envvar="PGAPPNAME",
1347+
help="Application name for the connection.",
1348+
)
13401349
@click.option(
13411350
"--less-chatty",
13421351
"less_chatty",
@@ -1387,6 +1396,7 @@ def cli(
13871396
pgclirc,
13881397
dsn,
13891398
row_limit,
1399+
application_name,
13901400
less_chatty,
13911401
prompt,
13921402
prompt_dsn,
@@ -1445,6 +1455,7 @@ def cli(
14451455
never_prompt,
14461456
pgclirc_file=pgclirc,
14471457
row_limit=row_limit,
1458+
application_name=application_name,
14481459
single_connection=single_connection,
14491460
less_chatty=less_chatty,
14501461
prompt=prompt,

tests/test_application_name.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from unittest.mock import patch
2+
3+
from click.testing import CliRunner
4+
5+
from pgcli.main import cli
6+
from pgcli.pgexecute import PGExecute
7+
8+
9+
def test_application_name_in_env():
10+
runner = CliRunner()
11+
app_name = "wonderful_app"
12+
with patch.object(PGExecute, "__init__") as mock_pgxecute:
13+
runner.invoke(
14+
cli, ["127.0.0.1:5432/hello", "user"], env={"PGAPPNAME": app_name}
15+
)
16+
kwargs = mock_pgxecute.call_args.kwargs
17+
assert kwargs.get("application_name") == app_name

0 commit comments

Comments
 (0)