Skip to content

Commit

Permalink
fix: wrap ipv6 addresses in brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Jan 29, 2024
1 parent 75f4e93 commit 6d2f500
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion bin/parse-ssh-host
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#!/usr/bin/python3

import ipaddress
import os
import sys
import urllib.parse


def is_ipv6(ip):
"""
Returns True if the ip is an IPv6 address
"""
try:
return isinstance(ipaddress.ip_address(ip), ipaddress.IPv6Address)
except ValueError:
print(f"Invalid IP address: {ip}", file=sys.stderr)
return False


def main():
"""
Prints out the host of the git remote url
Expand All @@ -18,7 +30,10 @@ def main():
sys.exit(1)

u = urllib.parse.urlparse(git_remote_url)
print(u.hostname)
host = u.hostname
if is_ipv6(host):
host = f"[{host}]"
print(host)


if __name__ == "__main__":
Expand Down

0 comments on commit 6d2f500

Please sign in to comment.