summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamien Baty <damien@damienbaty.com>2023-11-18 07:38:50 +0100
committerGitHub <noreply@github.com>2023-11-17 22:38:50 -0800
commitf2156b3151d74fa6022e9b6a33e516d318594c0e (patch)
treeef52d198842c2196bd8c0942829395184595e8e1
parent89979a93538d81b43c5a79c95d3c268dd32cf1d2 (diff)
Fix short host (\h) display in prompt when using an IP address (#1441)
When connecting to an IPv4 address (`pgcli -h 127.0.0.1`), trying to use the "short host" in the prompt (with `\h`) would only display the first octet (127). Now it shows the full IP. Fixes #964.
-rw-r--r--changelog.rst9
-rw-r--r--pgcli/pgexecute.py6
-rw-r--r--tests/test_pgexecute.py4
3 files changed, 19 insertions, 0 deletions
diff --git a/changelog.rst b/changelog.rst
index d1b9f72a..ffaaae93 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -1,3 +1,12 @@
+Upcoming
+========
+
+Bug fixes:
+----------
+
+* Fix display of "short host" in prompt (with `\h`) for IPv4 addresses ([issue 964](https://github.com/dbcli/pgcli/issues/964)).
+
+
==================
4.0.1 (2023-10-30)
==================
diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py
index 497d6816..db56aca3 100644
--- a/pgcli/pgexecute.py
+++ b/pgcli/pgexecute.py
@@ -1,3 +1,4 @@
+import ipaddress
import logging
import traceback
from collections import namedtuple
@@ -273,6 +274,11 @@ class PGExecute:
@property
def short_host(self):
+ try:
+ ipaddress.ip_address(self.host)
+ return self.host
+ except ValueError:
+ pass
if "," in self.host:
host, _, _ = self.host.partition(",")
else:
diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py
index 636795ba..95f60ad7 100644
--- a/tests/test_pgexecute.py
+++ b/tests/test_pgexecute.py
@@ -721,6 +721,10 @@ def test_short_host(executor):
executor, "host", "localhost1.example.org,localhost2.example.org"
):
assert executor.short_host == "localhost1"
+ with patch.object(executor, "host", "ec2-11-222-333-444.compute-1.amazonaws.com"):
+ assert executor.short_host == "ec2-11-222-333-444"
+ with patch.object(executor, "host", "1.2.3.4"):
+ assert executor.short_host == "1.2.3.4"
class VirtualCursor: