align compose ps output with docker ps

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2022-12-10 21:34:31 +01:00 committed by Nicolas De loof
parent a501ab3a2f
commit bc568eeb9b
3 changed files with 13 additions and 8 deletions

View File

@ -24,12 +24,14 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/docker/compose/v2/cmd/formatter" "github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/utils" "github.com/docker/compose/v2/pkg/utils"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
formatter2 "github.com/docker/cli/cli/command/formatter" formatter2 "github.com/docker/cli/cli/command/formatter"
"github.com/docker/go-units"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -142,21 +144,18 @@ SERVICES:
return formatter.Print(containers, opts.Format, os.Stdout, return formatter.Print(containers, opts.Format, os.Stdout,
writer(containers), writer(containers),
"NAME", "COMMAND", "SERVICE", "STATUS", "PORTS") "NAME", "IMAGE", "COMMAND", "SERVICE", "CREATED", "STATUS", "PORTS")
} }
func writer(containers []api.ContainerSummary) func(w io.Writer) { func writer(containers []api.ContainerSummary) func(w io.Writer) {
return func(w io.Writer) { return func(w io.Writer) {
for _, container := range containers { for _, container := range containers {
ports := displayablePorts(container) ports := displayablePorts(container)
status := container.State createdAt := time.Unix(container.Created, 0)
if status == "running" && container.Health != "" { created := units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
status = fmt.Sprintf("%s (%s)", container.State, container.Health) status := container.Status
} else if status == "exited" || status == "dead" {
status = fmt.Sprintf("%s (%d)", container.State, container.ExitCode)
}
command := formatter2.Ellipsis(container.Command, 20) command := formatter2.Ellipsis(container.Command, 20)
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", container.Name, strconv.Quote(command), container.Service, status, ports) _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", container.Name, container.Image, strconv.Quote(command), container.Service, created, status, ports)
} }
} }
} }

View File

@ -318,10 +318,13 @@ type PortPublisher struct {
type ContainerSummary struct { type ContainerSummary struct {
ID string ID string
Name string Name string
Image any
Command string Command string
Project string Project string
Service string Service string
Created int64
State string State string
Status string
Health string Health string
ExitCode int ExitCode int
Publishers PortPublishers Publishers PortPublishers

View File

@ -91,10 +91,13 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
summary[i] = api.ContainerSummary{ summary[i] = api.ContainerSummary{
ID: container.ID, ID: container.ID,
Name: getCanonicalContainerName(container), Name: getCanonicalContainerName(container),
Image: container.Image,
Project: container.Labels[api.ProjectLabel], Project: container.Labels[api.ProjectLabel],
Service: container.Labels[api.ServiceLabel], Service: container.Labels[api.ServiceLabel],
Command: container.Command, Command: container.Command,
State: container.State, State: container.State,
Status: container.Status,
Created: container.Created,
Health: health, Health: health,
ExitCode: exitCode, ExitCode: exitCode,
Publishers: publishers, Publishers: publishers,