@@ -27,6 +27,7 @@ import (
27
27
28
28
"github.com/docker/compose/v2/cmd/formatter"
29
29
"github.com/docker/compose/v2/pkg/utils"
30
+ "github.com/docker/docker/api/types"
30
31
31
32
formatter2 "github.com/docker/cli/cli/command/formatter"
32
33
"github.com/pkg/errors"
@@ -146,7 +147,7 @@ SERVICES:
146
147
func writter (containers []api.ContainerSummary ) func (w io.Writer ) {
147
148
return func (w io.Writer ) {
148
149
for _ , container := range containers {
149
- ports := DisplayablePorts (container )
150
+ ports := displayablePorts (container )
150
151
status := container .State
151
152
if status == "running" && container .Health != "" {
152
153
status = fmt .Sprintf ("%s (%s)" , container .State , container .Health )
@@ -178,72 +179,20 @@ func hasStatus(c api.ContainerSummary, statuses []string) bool {
178
179
return false
179
180
}
180
181
181
- type portRange struct {
182
- pStart int
183
- pEnd int
184
- tStart int
185
- tEnd int
186
- IP string
187
- protocol string
188
- }
189
-
190
- func (pr portRange ) String () string {
191
- var (
192
- pub string
193
- tgt string
194
- )
195
-
196
- if pr .pEnd > pr .pStart {
197
- pub = fmt .Sprintf ("%s:%d-%d->" , pr .IP , pr .pStart , pr .pEnd )
198
- } else if pr .pStart > 0 {
199
- pub = fmt .Sprintf ("%s:%d->" , pr .IP , pr .pStart )
200
- }
201
- if pr .tEnd > pr .tStart {
202
- tgt = fmt .Sprintf ("%d-%d" , pr .tStart , pr .tEnd )
203
- } else {
204
- tgt = fmt .Sprintf ("%d" , pr .tStart )
205
- }
206
- return fmt .Sprintf ("%s%s/%s" , pub , tgt , pr .protocol )
207
- }
208
-
209
- // DisplayablePorts is copy pasted from https://github.com/docker/cli/pull/581/files
210
- func DisplayablePorts (c api.ContainerSummary ) string {
182
+ func displayablePorts (c api.ContainerSummary ) string {
211
183
if c .Publishers == nil {
212
184
return ""
213
185
}
214
186
215
- sort .Sort (c .Publishers )
216
-
217
- pr := portRange {}
218
- ports := []string {}
219
- for _ , p := range c .Publishers {
220
- prIsRange := pr .tEnd != pr .tStart
221
- tOverlaps := p .TargetPort <= pr .tEnd
222
-
223
- // Start a new port-range if:
224
- // - the protocol is different from the current port-range
225
- // - published or target port are not consecutive to the current port-range
226
- // - the current port-range is a _range_, and the target port overlaps with the current range's target-ports
227
- if p .Protocol != pr .protocol || p .URL != pr .IP || p .PublishedPort - pr .pEnd > 1 || p .TargetPort - pr .tEnd > 1 || prIsRange && tOverlaps {
228
- // start a new port-range, and print the previous port-range (if any)
229
- if pr .pStart > 0 {
230
- ports = append (ports , pr .String ())
231
- }
232
- pr = portRange {
233
- pStart : p .PublishedPort ,
234
- pEnd : p .PublishedPort ,
235
- tStart : p .TargetPort ,
236
- tEnd : p .TargetPort ,
237
- protocol : p .Protocol ,
238
- IP : p .URL ,
239
- }
240
- continue
187
+ ports := make ([]types.Port , len (c .Publishers ))
188
+ for i , pub := range c .Publishers {
189
+ ports [i ] = types.Port {
190
+ IP : pub .URL ,
191
+ PrivatePort : uint16 (pub .TargetPort ),
192
+ PublicPort : uint16 (pub .PublishedPort ),
193
+ Type : pub .Protocol ,
241
194
}
242
- pr .pEnd = p .PublishedPort
243
- pr .tEnd = p .TargetPort
244
- }
245
- if pr .tStart > 0 {
246
- ports = append (ports , pr .String ())
247
195
}
248
- return strings .Join (ports , ", " )
196
+
197
+ return formatter2 .DisplayablePorts (ports )
249
198
}
0 commit comments