18
18
import com .google .common .base .Preconditions ;
19
19
import com .google .common .base .Strings ;
20
20
import java .util .ArrayList ;
21
+ import java .util .Arrays ;
21
22
import java .util .Collections ;
22
23
import java .util .List ;
23
24
import java .util .Objects ;
24
25
import java .util .regex .Matcher ;
25
26
import java .util .regex .Pattern ;
27
+ import java .util .stream .Collectors ;
26
28
import java .util .stream .Stream ;
27
29
28
30
public final class Ports {
@@ -55,8 +57,12 @@ public Stream<DockerPort> stream() {
55
57
}
56
58
57
59
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 );
60
66
List <DockerPort > ports = new ArrayList <>();
61
67
while (matcher .find ()) {
62
68
String matchedIpAddress = matcher .group (IP_ADDRESS );
@@ -66,7 +72,7 @@ public static Ports parseFromDockerComposePs(String psOutput, String dockerMachi
66
72
67
73
ports .add (new DockerPort (ip , externalPort , internalPort ));
68
74
}
69
- Matcher rangeMatcher = PORT_RANGE_PATTERN .matcher (psOutput );
75
+ Matcher rangeMatcher = PORT_RANGE_PATTERN .matcher (cleanOutput );
70
76
while (rangeMatcher .find ()) {
71
77
String matchedIpAddress = rangeMatcher .group (IP_ADDRESS );
72
78
String ip = matchedIpAddress .equals (NO_IP_ADDRESS ) ? dockerMachineIp : matchedIpAddress ;
0 commit comments