From 05b510a447141ffd0db70a9c4d113a8fead5f802 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 22 Jun 2021 11:21:27 +0200 Subject: [PATCH] compose ps to include container command Signed-off-by: Nicolas De Loof --- cmd/compose/ps.go | 7 +++++-- local/e2e/compose/compose_test.go | 8 ++++---- local/e2e/compose/restart_test.go | 4 ++-- local/e2e/compose/start_stop_test.go | 4 ++-- pkg/api/api.go | 1 + pkg/compose/ps.go | 4 +++- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 28ca0a747..1bbb89712 100644 --- a/cmd/compose/ps.go +++ b/cmd/compose/ps.go @@ -22,8 +22,10 @@ import ( "io" "os" "sort" + "strconv" "strings" + formatter2 "github.com/docker/cli/cli/command/formatter" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -141,10 +143,11 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO } else if status == "exited" || status == "dead" { status = fmt.Sprintf("%s (%d)", container.State, container.ExitCode) } - _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", container.Name, container.Service, status, strings.Join(ports, ", ")) + 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, strings.Join(ports, ", ")) } }, - "NAME", "SERVICE", "STATUS", "PORTS") + "NAME", "COMMAND", "SERVICE", "STATUS", "PORTS") } func filterByStatus(containers []api.ContainerSummary, status string) []api.ContainerSummary { diff --git a/local/e2e/compose/compose_test.go b/local/e2e/compose/compose_test.go index e90db4f3b..b2fb64c41 100644 --- a/local/e2e/compose/compose_test.go +++ b/local/e2e/compose/compose_test.go @@ -102,13 +102,13 @@ func TestLocalComposeUp(t *testing.T) { t.Run("check healthcheck output", func(t *testing.T) { c.WaitForCmdResult(c.NewDockerCmd("compose", "-p", projectName, "ps", "--format", "json"), - StdoutContains(`"Name":"compose-e2e-demo_web_1","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`), + StdoutContains(`"Name":"compose-e2e-demo_web_1","Command":"/dispatcher","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`), 5*time.Second, 1*time.Second) res := c.RunDockerCmd("compose", "-p", projectName, "ps") - res.Assert(t, icmd.Expected{Out: `NAME SERVICE STATUS PORTS`}) - res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_web_1 web running (healthy) 0.0.0.0:90->80/tcp`}) - res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_db_1 db running 5432/tcp`}) + res.Assert(t, icmd.Expected{Out: `NAME COMMAND SERVICE STATUS PORTS`}) + res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_web_1 "/dispatcher" web running (healthy) 0.0.0.0:90->80/tcp, :::90->80/tcp`}) + res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_db_1 "docker-entrypoint.s…" db running 5432/tcp`}) }) t.Run("images", func(t *testing.T) { diff --git a/local/e2e/compose/restart_test.go b/local/e2e/compose/restart_test.go index 902541bf4..14fb9e8e0 100644 --- a/local/e2e/compose/restart_test.go +++ b/local/e2e/compose/restart_test.go @@ -34,8 +34,8 @@ func TestRestart(t *testing.T) { getServiceRegx := func(service string, status string) string { // match output with random spaces like: - // e2e-start-stop_db_1 db running - return fmt.Sprintf("%s_%s_1\\s+%s\\s+%s", projectName, service, service, status) + // e2e-start-stop_db_1 "echo hello" db running + return fmt.Sprintf("%s_%s_1.+%s\\s+%s", projectName, service, service, status) } t.Run("Up a project", func(t *testing.T) { diff --git a/local/e2e/compose/start_stop_test.go b/local/e2e/compose/start_stop_test.go index 854f3f1c7..875f620c6 100644 --- a/local/e2e/compose/start_stop_test.go +++ b/local/e2e/compose/start_stop_test.go @@ -39,8 +39,8 @@ func TestStartStop(t *testing.T) { getServiceRegx := func(service string, status string) string { // match output with random spaces like: - // e2e-start-stop_db_1 db running - return fmt.Sprintf("%s_%s_1\\s+%s\\s+%s", projectName, service, service, status) + // e2e-start-stop_db_1 "echo hello" db running + return fmt.Sprintf("%s_%s_1.+%s\\s+%s", projectName, service, service, status) } t.Run("Up a project", func(t *testing.T) { diff --git a/pkg/api/api.go b/pkg/api/api.go index 8fc5619de..ebed72b57 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -285,6 +285,7 @@ type PortPublisher struct { type ContainerSummary struct { ID string Name string + Command string Project string Service string State string diff --git a/pkg/compose/ps.go b/pkg/compose/ps.go index 1f8c590cd..c293e28e3 100644 --- a/pkg/compose/ps.go +++ b/pkg/compose/ps.go @@ -21,8 +21,9 @@ import ( "fmt" "sort" - "github.com/docker/compose-cli/pkg/api" "golang.org/x/sync/errgroup" + + "github.com/docker/compose-cli/pkg/api" ) func (s *composeService) Ps(ctx context.Context, projectName string, options api.PsOptions) ([]api.ContainerSummary, error) { @@ -83,6 +84,7 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api Name: getCanonicalContainerName(container), Project: container.Labels[api.ProjectLabel], Service: container.Labels[api.ServiceLabel], + Command: container.Command, State: container.State, Health: health, ExitCode: exitCode,