report error on `ps` when no container found for service(s)
@@ -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,7 +142,12 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO
})
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 {
var ports []string
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)
_, _ = 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 {
