Skip to content

Commit be495ab

Browse files
committed
Filter compose ps output by provided compose model
Signed-off-by: Laura Brehm <[email protected]>
1 parent d2a6c2c commit be495ab

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

cmd/compose/ps.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
9191
}
9292

9393
func runPs(ctx context.Context, backend api.Service, services []string, opts psOptions) error {
94-
projectName, err := opts.toProjectName()
94+
project, name, err := opts.projectOrName()
9595
if err != nil {
9696
return err
9797
}
98-
containers, err := backend.Ps(ctx, projectName, api.PsOptions{
98+
containers, err := backend.Ps(ctx, name, api.PsOptions{
99+
Project: project,
99100
All: opts.All,
100101
Services: services,
101102
})

pkg/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ type ListOptions struct {
280280

281281
// PsOptions group options of the Ps API
282282
type PsOptions struct {
283+
Project *types.Project
283284
All bool
284285
Services []string
285286
}

pkg/compose/ps.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
3737
return nil, err
3838
}
3939

40+
project := options.Project
41+
if project == nil {
42+
project, err = s.getProjectWithResources(ctx, containers, projectName)
43+
if err != nil {
44+
return nil, err
45+
}
46+
}
47+
48+
if len(options.Services) == 0 {
49+
options.Services = project.ServiceNames()
50+
}
51+
52+
containers = containers.filter(isService(options.Services...))
4053
summary := make([]api.ContainerSummary, len(containers))
4154
eg, ctx := errgroup.WithContext(ctx)
4255
for i, container := range containers {

pkg/compose/ps_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
moby "github.com/docker/docker/api/types"
2828
"github.com/docker/docker/api/types/filters"
29+
"github.com/docker/docker/api/types/volume"
2930

3031
compose "github.com/docker/compose/v2/pkg/api"
3132
"github.com/docker/compose/v2/pkg/mocks"
@@ -48,6 +49,8 @@ func TestPs(t *testing.T) {
4849
c2, inspect2 := containerDetails("service1", "456", "running", "", 0)
4950
c2.Ports = []moby.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
5051
c3, inspect3 := containerDetails("service2", "789", "exited", "", 130)
52+
api.EXPECT().VolumeList(ctx, gomock.Any()).Return(volume.VolumeListOKBody{}, nil)
53+
api.EXPECT().NetworkList(ctx, gomock.Any()).Return([]moby.NetworkResource{}, nil)
5154
api.EXPECT().ContainerList(ctx, listOpts).Return([]moby.Container{c1, c2, c3}, nil)
5255
api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil)
5356
api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)

0 commit comments

Comments
 (0)