report error on ps when no container found for service(s)

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-07-12 10:16:04 +02:00
parent 396d449d36
commit bf0b75cee0
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E

View File

@ -68,7 +68,7 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
projectOptions: p, projectOptions: p,
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "ps", Use: "ps [options] [SERVICE...]",
Short: "List containers", Short: "List containers",
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.parseFilter() 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")) fmt.Println(strings.Join(services, "\n"))
return nil 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 { if opts.Quiet {
for _, s := range containers { for _, s := range containers {
fmt.Println(s.ID) fmt.Println(s.ID)
@ -127,7 +142,12 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO
}) })
return formatter.Print(containers, opts.Format, os.Stdout, return formatter.Print(containers, opts.Format, os.Stdout,
func(w io.Writer) { 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 { for _, container := range containers {
var ports []string var ports []string
for _, p := range container.Publishers { for _, p := range container.Publishers {
@ -146,8 +166,7 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO
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, strings.Join(ports, ", ")) _, _ = 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", "COMMAND", "SERVICE", "STATUS", "PORTS")
} }
func filterByStatus(containers []api.ContainerSummary, status string) []api.ContainerSummary { func filterByStatus(containers []api.ContainerSummary, status string) []api.ContainerSummary {