@@ -68,7 +68,7 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
68
68
projectOptions : p ,
69
69
}
70
70
cmd := & cobra.Command {
71
- Use : "ps" ,
71
+ Use : "ps [options] [SERVICE...] " ,
72
72
Short : "List containers" ,
73
73
PreRunE : func (cmd * cobra.Command , args []string ) error {
74
74
return opts .parseFilter ()
@@ -111,6 +111,21 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO
111
111
fmt .Println (strings .Join (services , "\n " ))
112
112
return nil
113
113
}
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
+
114
129
if opts .Quiet {
115
130
for _ , s := range containers {
116
131
fmt .Println (s .ID )
@@ -127,27 +142,31 @@ func runPs(ctx context.Context, backend api.Service, services []string, opts psO
127
142
})
128
143
129
144
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 ))
145
158
}
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 , ", " ))
148
159
}
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
+ }
151
170
}
152
171
153
172
func filterByStatus (containers []api.ContainerSummary , status string ) []api.ContainerSummary {
0 commit comments