Add HEALTH column to compose ps

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-02-01 16:53:34 +01:00
parent 6215445b8a
commit c4f6b1bd14
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 43 additions and 24 deletions

View File

@ -106,6 +106,7 @@ type ContainerSummary struct {
Project string Project string
Service string Service string
State string State string
Health string
Publishers []PortPublisher Publishers []PortPublisher
} }

View File

@ -88,8 +88,8 @@ func runPs(ctx context.Context, opts psOptions) error {
ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol)) ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol))
} }
} }
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", container.Name, container.Service, container.State, strings.Join(ports, ", ")) _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", container.Name, container.Service, container.State, container.Health, strings.Join(ports, ", "))
} }
}, },
"NAME", "SERVICE", "STATE", "PORTS") "NAME", "SERVICE", "STATE", "HEALTH", "PORTS")
} }

View File

@ -23,6 +23,7 @@ import (
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
) )
@ -37,10 +38,14 @@ func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.
return nil, err return nil, err
} }
var summary []compose.ContainerSummary summary := make([]compose.ContainerSummary, len(containers))
for _, c := range containers { eg, ctx := errgroup.WithContext(ctx)
for i, c := range containers {
container := c
i := i
eg.Go(func() error {
var publishers []compose.PortPublisher var publishers []compose.PortPublisher
for _, p := range c.Ports { for _, p := range container.Ports {
var url string var url string
if p.PublicPort != 0 { if p.PublicPort != 0 {
url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort) url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
@ -53,16 +58,29 @@ func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.
}) })
} }
summary = append(summary, compose.ContainerSummary{ inspect, err := s.apiClient.ContainerInspect(ctx, container.ID)
ID: c.ID, if err != nil {
Name: getCanonicalContainerName(c), return err
Project: c.Labels[projectLabel], }
Service: c.Labels[serviceLabel],
State: c.State, var health string
if inspect.State != nil && inspect.State.Health != nil {
health = inspect.State.Health.Status
}
summary[i] = compose.ContainerSummary{
ID: container.ID,
Name: getCanonicalContainerName(container),
Project: container.Labels[projectLabel],
Service: container.Labels[serviceLabel],
State: container.State,
Health: health,
Publishers: publishers, Publishers: publishers,
}
return nil
}) })
} }
return summary, nil return summary, eg.Wait()
} }
func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) { func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {