Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit c4f6b1b

Browse files
committed
Add HEALTH column to compose ps
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 6215445 commit c4f6b1b

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

api/compose/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type ContainerSummary struct {
106106
Project string
107107
Service string
108108
State string
109+
Health string
109110
Publishers []PortPublisher
110111
}
111112

cli/cmd/compose/ps.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func runPs(ctx context.Context, opts psOptions) error {
8888
ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol))
8989
}
9090
}
91-
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", container.Name, container.Service, container.State, strings.Join(ports, ", "))
91+
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", container.Name, container.Service, container.State, container.Health, strings.Join(ports, ", "))
9292
}
9393
},
94-
"NAME", "SERVICE", "STATE", "PORTS")
94+
"NAME", "SERVICE", "STATE", "HEALTH", "PORTS")
9595
}

local/compose/ps.go

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
moby "github.com/docker/docker/api/types"
2525
"github.com/docker/docker/api/types/filters"
26+
"golang.org/x/sync/errgroup"
2627

2728
"github.com/docker/compose-cli/api/compose"
2829
)
@@ -37,32 +38,49 @@ func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.
3738
return nil, err
3839
}
3940

40-
var summary []compose.ContainerSummary
41-
for _, c := range containers {
42-
var publishers []compose.PortPublisher
43-
for _, p := range c.Ports {
44-
var url string
45-
if p.PublicPort != 0 {
46-
url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
41+
summary := make([]compose.ContainerSummary, len(containers))
42+
eg, ctx := errgroup.WithContext(ctx)
43+
for i, c := range containers {
44+
container := c
45+
i := i
46+
eg.Go(func() error {
47+
var publishers []compose.PortPublisher
48+
for _, p := range container.Ports {
49+
var url string
50+
if p.PublicPort != 0 {
51+
url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
52+
}
53+
publishers = append(publishers, compose.PortPublisher{
54+
URL: url,
55+
TargetPort: int(p.PrivatePort),
56+
PublishedPort: int(p.PublicPort),
57+
Protocol: p.Type,
58+
})
59+
}
60+
61+
inspect, err := s.apiClient.ContainerInspect(ctx, container.ID)
62+
if err != nil {
63+
return err
4764
}
48-
publishers = append(publishers, compose.PortPublisher{
49-
URL: url,
50-
TargetPort: int(p.PrivatePort),
51-
PublishedPort: int(p.PublicPort),
52-
Protocol: p.Type,
53-
})
54-
}
5565

56-
summary = append(summary, compose.ContainerSummary{
57-
ID: c.ID,
58-
Name: getCanonicalContainerName(c),
59-
Project: c.Labels[projectLabel],
60-
Service: c.Labels[serviceLabel],
61-
State: c.State,
62-
Publishers: publishers,
66+
var health string
67+
if inspect.State != nil && inspect.State.Health != nil {
68+
health = inspect.State.Health.Status
69+
}
70+
71+
summary[i] = compose.ContainerSummary{
72+
ID: container.ID,
73+
Name: getCanonicalContainerName(container),
74+
Project: container.Labels[projectLabel],
75+
Service: container.Labels[serviceLabel],
76+
State: container.State,
77+
Health: health,
78+
Publishers: publishers,
79+
}
80+
return nil
6381
})
6482
}
65-
return summary, nil
83+
return summary, eg.Wait()
6684
}
6785

6886
func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {

0 commit comments

Comments
 (0)