Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 2e99f80

Browse files
authored
Merge pull request #1912 from ndeloof/ps_services
2 parents 396d449 + bf0b75c commit 2e99f80

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

cmd/compose/ps.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
6868
projectOptions: p,
6969
}
7070
cmd := &cobra.Command{
71-
Use: "ps",
71+
Use: "ps [options] [SERVICE...]",
7272
Short: "List containers",
7373
PreRunE: func(cmd *cobra.Command, args []string) error {
7474
return opts.parseFilter()
@@ -111,6 +111,21 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO
111111
fmt.Println(strings.Join(services, "\n"))
112112
return nil
113113
}
114+
115+
SERVICES:
116+
for _, s := range services {
117+
for _, c := range containers {
118+
if c.Service == s {
119+
continue SERVICES
120+
}
121+
}
122+
return fmt.Errorf("no such service: %s", s)
123+
}
124+
125+
if len(containers) == 0 {
126+
return api.ErrNotFound
127+
}
128+
114129
if opts.Quiet {
115130
for _, s := range containers {
116131
fmt.Println(s.ID)
@@ -127,27 +142,31 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO
127142
})
128143

129144
return formatter.Print(containers, opts.Format, os.Stdout,
130-
func(w io.Writer) {
131-
for _, container := range containers {
132-
var ports []string
133-
for _, p := range container.Publishers {
134-
if p.URL == "" {
135-
ports = append(ports, fmt.Sprintf("%d/%s", p.TargetPort, p.Protocol))
136-
} else {
137-
ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol))
138-
}
139-
}
140-
status := container.State
141-
if status == "running" && container.Health != "" {
142-
status = fmt.Sprintf("%s (%s)", container.State, container.Health)
143-
} else if status == "exited" || status == "dead" {
144-
status = fmt.Sprintf("%s (%d)", container.State, container.ExitCode)
145+
writter(containers),
146+
"NAME", "COMMAND", "SERVICE", "STATUS", "PORTS")
147+
}
148+
149+
func writter(containers []api.ContainerSummary) func(w io.Writer) {
150+
return func(w io.Writer) {
151+
for _, container := range containers {
152+
var ports []string
153+
for _, p := range container.Publishers {
154+
if p.URL == "" {
155+
ports = append(ports, fmt.Sprintf("%d/%s", p.TargetPort, p.Protocol))
156+
} else {
157+
ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol))
145158
}
146-
command := formatter2.Ellipsis(container.Command, 20)
147-
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", container.Name, strconv.Quote(command), container.Service, status, strings.Join(ports, ", "))
148159
}
149-
},
150-
"NAME", "COMMAND", "SERVICE", "STATUS", "PORTS")
160+
status := container.State
161+
if status == "running" && container.Health != "" {
162+
status = fmt.Sprintf("%s (%s)", container.State, container.Health)
163+
} else if status == "exited" || status == "dead" {
164+
status = fmt.Sprintf("%s (%d)", container.State, container.ExitCode)
165+
}
166+
command := formatter2.Ellipsis(container.Command, 20)
167+
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", container.Name, strconv.Quote(command), container.Service, status, strings.Join(ports, ", "))
168+
}
169+
}
151170
}
152171

153172
func filterByStatus(containers []api.ContainerSummary, status string) []api.ContainerSummary {

0 commit comments

Comments
 (0)