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
1 changed files with 40 additions and 21 deletions

View File

@ -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 {