Skip to content

Commit 1e7b661

Browse files
committed
Fix parsing of docker-compose < 1.25.2 output on narrow terminals
1 parent 916c91a commit 1e7b661

File tree

2 files changed

+26
-3
lines changed
  • docker-compose-rule-core/src

2 files changed

+26
-3
lines changed

docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/Ports.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import com.google.common.base.Preconditions;
1919
import com.google.common.base.Strings;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.List;
2324
import java.util.Objects;
2425
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
27+
import java.util.stream.Collectors;
2628
import java.util.stream.Stream;
2729

2830
public final class Ports {
@@ -55,8 +57,12 @@ public Stream<DockerPort> stream() {
5557
}
5658

5759
public static Ports parseFromDockerComposePs(String psOutput, String dockerMachineIp) {
58-
Preconditions.checkArgument(!Strings.isNullOrEmpty(psOutput), "No container found");
59-
Matcher matcher = PORT_PATTERN.matcher(psOutput);
60+
// Clean the ps output to remove line breaks and unneeded spaces
61+
String cleanOutput = Arrays.stream(psOutput.split("\\R"))
62+
.map(String::trim)
63+
.collect(Collectors.joining(""));
64+
Preconditions.checkArgument(!Strings.isNullOrEmpty(cleanOutput), "No container found");
65+
Matcher matcher = PORT_PATTERN.matcher(cleanOutput);
6066
List<DockerPort> ports = new ArrayList<>();
6167
while (matcher.find()) {
6268
String matchedIpAddress = matcher.group(IP_ADDRESS);
@@ -66,7 +72,7 @@ public static Ports parseFromDockerComposePs(String psOutput, String dockerMachi
6672

6773
ports.add(new DockerPort(ip, externalPort, internalPort));
6874
}
69-
Matcher rangeMatcher = PORT_RANGE_PATTERN.matcher(psOutput);
75+
Matcher rangeMatcher = PORT_RANGE_PATTERN.matcher(cleanOutput);
7076
while (rangeMatcher.find()) {
7177
String matchedIpAddress = rangeMatcher.group(IP_ADDRESS);
7278
String ip = matchedIpAddress.equals(NO_IP_ADDRESS) ? dockerMachineIp : matchedIpAddress;

docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/PortsShould.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ public void parse_actual_docker_compose_output() {
9595
assertThat(ports, is(expected));
9696
}
9797

98+
@Test
99+
public void parse_docker_compose_output_on_narrow_terminal() {
100+
String psOutput =
101+
" Name Command State Ports \n" +
102+
"--------------------------------------------------------------------------------\n" +
103+
"projectnamemyserv docker-entrypoint.sh Up (healthy) 4510/tcp, 4511/tcp,\n" +
104+
"ice_localstack_1 4512/tcp, 4513/tcp,\n" +
105+
" 4514/tcp, 4515/tcp,\n" +
106+
" 4558/tcp, 4559/tcp,\n" +
107+
" 0.0.0.0:49153->4566\n" +
108+
" /tcp,:::49153->4566\n" +
109+
" /tcp, 5678/tcp ";
110+
Ports ports = Ports.parseFromDockerComposePs(psOutput, LOCALHOST_IP);
111+
Ports expected = new Ports(newArrayList(new DockerPort(LOCALHOST_IP, 49153, 4566)));
112+
assertThat(ports, is(expected));
113+
}
114+
98115
@Test
99116
public void throw_illegal_state_exception_when_no_running_container_found_for_service() {
100117
exception.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)