group ports to render ranges as ... ranges

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-09-21 15:21:28 +02:00 committed by Nicolas De loof
parent e39ea13002
commit 97a0efd7c3
4 changed files with 104 additions and 17 deletions

View file

@ -297,7 +297,36 @@ type ContainerSummary struct {
State string
Health string
ExitCode int
Publishers []PortPublisher
Publishers PortPublishers
}
// PortPublishers is a slice of PortPublisher
type PortPublishers []PortPublisher
// Len implements sort.Interface
func (p PortPublishers) Len() int {
return len(p)
}
// Less implements sort.Interface
func (p PortPublishers) Less(i, j int) bool {
left := p[i]
right := p[j]
if left.URL != right.URL {
return left.URL < right.URL
}
if left.TargetPort != right.TargetPort {
return left.TargetPort < right.TargetPort
}
if left.PublishedPort != right.PublishedPort {
return left.PublishedPort < right.PublishedPort
}
return left.Protocol < right.Protocol
}
// Swap implements sort.Interface
func (p PortPublishers) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
// ContainerProcSummary holds container processes top data

View file

@ -18,7 +18,6 @@ package compose
import (
"context"
"fmt"
"sort"
"golang.org/x/sync/errgroup"
@ -46,12 +45,8 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
return container.Ports[i].PrivatePort < container.Ports[j].PrivatePort
})
for _, p := range container.Ports {
var url string
if p.PublicPort != 0 {
url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
}
publishers = append(publishers, api.PortPublisher{
URL: url,
URL: p.IP,
TargetPort: int(p.PrivatePort),
PublishedPort: int(p.PublicPort),
Protocol: p.Type,

View file

@ -54,7 +54,7 @@ func TestPs(t *testing.T) {
expected := []compose.ContainerSummary{
{ID: "123", Name: "123", Project: strings.ToLower(testProject), Service: "service1", State: "running", Health: "healthy", Publishers: nil},
{ID: "456", Name: "456", Project: strings.ToLower(testProject), Service: "service1", State: "running", Health: "", Publishers: []compose.PortPublisher{{URL: "localhost:80", TargetPort: 90,
{ID: "456", Name: "456", Project: strings.ToLower(testProject), Service: "service1", State: "running", Health: "", Publishers: []compose.PortPublisher{{URL: "localhost", TargetPort: 90,
PublishedPort: 80}}},
{ID: "789", Name: "789", Project: strings.ToLower(testProject), Service: "service2", State: "exited", Health: "", ExitCode: 130, Publishers: nil},
}