diff --git a/Dockerfile b/Dockerfile index dec35c4..445126e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine:3.19.0 -RUN apk --no-cache add git==2.43.0-r0 openssh==9.6_p1-r0 && \ +RUN apk --no-cache add git==2.43.0-r0 python3==3.11.6-r1 openssh==9.6_p1-r0 && \ mkdir -p ~/.ssh COPY bin /bin diff --git a/bin/parse-ssh-host b/bin/parse-ssh-host index 47c8f74..8d1386a 100755 --- a/bin/parse-ssh-host +++ b/bin/parse-ssh-host @@ -1,12 +1,50 @@ -#!/bin/sh -l -set -e +#!/usr/bin/python3 -if [ -n "$TRACE" ]; then - set -x -fi +import ipaddress +import os +import sys +import urllib.parse -if [ -n "$PLUGIN_GIT_REMOTE_URL" ]; then - export GIT_REMOTE_URL="$PLUGIN_GIT_REMOTE_URL" -fi -echo "$GIT_REMOTE_URL" | sed -e 's/.*@//' -e 's/[:/].*//' +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 + """ + git_remote_url = os.getenv("GIT_REMOTE_URL") + if os.getenv("PLUGIN_GIT_REMOTE_URL"): + git_remote_url = os.getenv("PLUGIN_GIT_REMOTE_URL") + + if not git_remote_url: + print("GIT_REMOTE_URL is empty", file=sys.stderr) + sys.exit(1) + + u = urllib.parse.urlparse(git_remote_url) + host = u.hostname + if is_ipv6(host): + host = f"[{host}]" + print(host) + + +if __name__ == "__main__": + main() + +# if [ -n "$TRACE" ]; then +# set -x +# fi + +# if [ -n "$PLUGIN_GIT_REMOTE_URL" ]; then +# export GIT_REMOTE_URL="$PLUGIN_GIT_REMOTE_URL" +# fi + +# echo "$GIT_REMOTE_URL" | sed -e 's/.*@//' -e 's/[:/].*//' diff --git a/bin/parse-ssh-port b/bin/parse-ssh-port index b268070..82cad5c 100755 --- a/bin/parse-ssh-port +++ b/bin/parse-ssh-port @@ -1,17 +1,29 @@ -#!/bin/sh -l -set -e +#!/usr/bin/python3 -if [ -n "$TRACE" ]; then - set -x -fi +import os +import sys +import urllib.parse -if [ -n "$PLUGIN_GIT_REMOTE_URL" ]; then - export GIT_REMOTE_URL="$PLUGIN_GIT_REMOTE_URL" -fi -ssh_port="$(echo "$GIT_REMOTE_URL" | sed -e 's/.*@//' -e 's/\/.*//' -ne 's/.*:\([0-9]*\)/\1/p')" -if [ -z "$ssh_port" ]; then - ssh_port=22 -fi +def main(): + """ + Prints out the port of the git remote url + """ + git_remote_url = os.getenv("GIT_REMOTE_URL") + if os.getenv("PLUGIN_GIT_REMOTE_URL"): + git_remote_url = os.getenv("PLUGIN_GIT_REMOTE_URL") -echo "$ssh_port" + if not git_remote_url: + print("GIT_REMOTE_URL is empty", file=sys.stderr) + sys.exit(1) + + u = urllib.parse.urlparse(git_remote_url) + port = "22" + if u.port: + port = u.port + + print(port) + + +if __name__ == "__main__": + main()