Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-arabaolaza committed Jan 26, 2024
1 parent 8e9c615 commit be883c6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public String ipBound(int n) {
if (out.isEmpty()) // expected to return single line like "0.0.0.0:55326" or [::]:55326
throw new IllegalStateException(format("Port %d is not mapped for container %s", n, cid));
// assumes it is published only in ipv6 or only in ipv4
return ipv6Enabled() ? out.substring(1,out.lastIndexOf(":") - 1) : out.split(":")[0];
return out.substring(0,out.lastIndexOf(":")).replaceAll("[\\[\\]]","");
} catch (IOException | InterruptedException e) {
throw new AssertionError("Failed to figure out port map " + n, e);
}
Expand All @@ -111,7 +111,7 @@ public String ipUdpBound(int n) {
String out = Docker.cmd("port").add(cid, n + "/udp").popen().verifyOrDieWith("docker port command failed").trim();
if (out.isEmpty()) // expected to return single line like "0.0.0.0:55326" or [::]:55326
throw new IllegalStateException(format("Udp port %d is not mapped for container %s", n, cid));
return ipv6Enabled() ? out.substring(1,out.lastIndexOf(":") -1) : out.split(":")[0];
return out.substring(0,out.lastIndexOf(":")).replaceAll("[\\[\\]]","");
} catch (IOException | InterruptedException e) {
throw new AssertionError("Failed to figure out udp port map " + n, e);
}
Expand All @@ -130,7 +130,7 @@ public int port(int n) {
if (out.isEmpty()) // expected to return single line like "0.0.0.0:55326" or [::]:55326
throw new IllegalStateException(format("Port %d is not mapped for container %s", n, cid));

return Integer.parseInt(out.split(":")[ipv6Enabled() ? 3 : 1]);
return Integer.parseInt(out.substring(out.lastIndexOf(":") + 1, out.length()));
} catch (IOException | InterruptedException e) {
throw new AssertionError("Failed to figure out port map " + n, e);
}
Expand All @@ -149,7 +149,7 @@ public int udpPort(int n) {
if (out.isEmpty()) // expected to return single line like "0.0.0.0:55326" or [::]:55326
throw new IllegalStateException(format("Udp port %d is not mapped for container %s", n, cid));

return Integer.parseInt(out.split(":")[ipv6Enabled() ? 3 : 1]);
return Integer.parseInt(out.substring(out.lastIndexOf(":") + 1, out.length()));
} catch (IOException | InterruptedException e) {
throw new AssertionError("Failed to figure out udp port map " + n, e);
}
Expand Down Expand Up @@ -240,7 +240,4 @@ public static boolean ipv6Enabled() {
return Boolean.getBoolean("java.net.preferIPv6Addresses");
}

public static String encloseInBrackets(String toEnclose) {
return String.format("[%s]", toEnclose);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private String getUdpPortMapping(int udpPort) {
}

private static String addBracketsIfNeeded(String ipAddress) {
return (DockerContainer.ipv6Enabled() && !ipAddress.contains("[")) ? DockerContainer.encloseInBrackets(ipAddress) : ipAddress;
return DockerContainer.ipv6Enabled() && !ipAddress.contains("[") ? String.format("[%s]", ipAddress) : ipAddress;
}
}
}

0 comments on commit be883c6

Please sign in to comment.