Przeglądaj źródła

align `compose ps` output with `docker ps`

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 2 lat temu
rodzic
commit
bc568eeb9b
3 zmienionych plików z 13 dodań i 8 usunięć
  1. 7 8
      cmd/compose/ps.go
  2. 3 0
      pkg/api/api.go
  3. 3 0
      pkg/compose/ps.go

+ 7 - 8
cmd/compose/ps.go

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

+ 3 - 0
pkg/api/api.go

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

+ 3 - 0
pkg/compose/ps.go

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