From bf0b75cee0e3602bcf81f4e3a1c58686c8643fcc Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 12 Jul 2021 10:16:04 +0200 Subject: [PATCH] report error on `ps` when no container found for service(s) Signed-off-by: Nicolas De Loof --- cmd/compose/ps.go | 61 +++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 1bbb89712..5249fcf2f 100644 --- a/cmd/compose/ps.go +++ b/cmd/compose/ps.go @@ -68,7 +68,7 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command { projectOptions: p, } cmd := &cobra.Command{ - Use: "ps", + Use: "ps [options] [SERVICE...]", Short: "List containers", PreRunE: func(cmd *cobra.Command, args []string) error { return opts.parseFilter() @@ -111,6 +111,21 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO fmt.Println(strings.Join(services, "\n")) return nil } + +SERVICES: + for _, s := range services { + for _, c := range containers { + if c.Service == s { + continue SERVICES + } + } + return fmt.Errorf("no such service: %s", s) + } + + if len(containers) == 0 { + return api.ErrNotFound + } + if opts.Quiet { for _, s := range containers { fmt.Println(s.ID) @@ -127,29 +142,33 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO }) return formatter.Print(containers, opts.Format, os.Stdout, - func(w io.Writer) { - for _, container := range containers { - var ports []string - for _, p := range container.Publishers { - if p.URL == "" { - ports = append(ports, fmt.Sprintf("%d/%s", p.TargetPort, p.Protocol)) - } else { - ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol)) - } - } - 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) - } - 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, ", ")) - } - }, + writter(containers), "NAME", "COMMAND", "SERVICE", "STATUS", "PORTS") } +func writter(containers []api.ContainerSummary) func(w io.Writer) { + return func(w io.Writer) { + for _, container := range containers { + var ports []string + for _, p := range container.Publishers { + if p.URL == "" { + ports = append(ports, fmt.Sprintf("%d/%s", p.TargetPort, p.Protocol)) + } else { + ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol)) + } + } + 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) + } + 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, ", ")) + } + } +} + func filterByStatus(containers []api.ContainerSummary, status string) []api.ContainerSummary { hasContainerWithState := map[string]struct{}{} for _, c := range containers {