mirror of https://github.com/docker/compose.git
Filter `compose ps` output by provided compose model
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
This commit is contained in:
parent
d2a6c2c200
commit
be495ab8e6
|
@ -91,11 +91,12 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func runPs(ctx context.Context, backend api.Service, services []string, opts psOptions) error {
|
func runPs(ctx context.Context, backend api.Service, services []string, opts psOptions) error {
|
||||||
projectName, err := opts.toProjectName()
|
project, name, err := opts.projectOrName()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
containers, err := backend.Ps(ctx, projectName, api.PsOptions{
|
containers, err := backend.Ps(ctx, name, api.PsOptions{
|
||||||
|
Project: project,
|
||||||
All: opts.All,
|
All: opts.All,
|
||||||
Services: services,
|
Services: services,
|
||||||
})
|
})
|
||||||
|
|
|
@ -280,6 +280,7 @@ type ListOptions struct {
|
||||||
|
|
||||||
// PsOptions group options of the Ps API
|
// PsOptions group options of the Ps API
|
||||||
type PsOptions struct {
|
type PsOptions struct {
|
||||||
|
Project *types.Project
|
||||||
All bool
|
All bool
|
||||||
Services []string
|
Services []string
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,19 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project := options.Project
|
||||||
|
if project == nil {
|
||||||
|
project, err = s.getProjectWithResources(ctx, containers, projectName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(options.Services) == 0 {
|
||||||
|
options.Services = project.ServiceNames()
|
||||||
|
}
|
||||||
|
|
||||||
|
containers = containers.filter(isService(options.Services...))
|
||||||
summary := make([]api.ContainerSummary, len(containers))
|
summary := make([]api.ContainerSummary, len(containers))
|
||||||
eg, ctx := errgroup.WithContext(ctx)
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
for i, container := range containers {
|
for i, container := range containers {
|
||||||
|
|
|
@ -26,6 +26,7 @@ import (
|
||||||
|
|
||||||
moby "github.com/docker/docker/api/types"
|
moby "github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/api/types/volume"
|
||||||
|
|
||||||
compose "github.com/docker/compose/v2/pkg/api"
|
compose "github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/mocks"
|
"github.com/docker/compose/v2/pkg/mocks"
|
||||||
|
@ -48,6 +49,8 @@ func TestPs(t *testing.T) {
|
||||||
c2, inspect2 := containerDetails("service1", "456", "running", "", 0)
|
c2, inspect2 := containerDetails("service1", "456", "running", "", 0)
|
||||||
c2.Ports = []moby.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
|
c2.Ports = []moby.Port{{PublicPort: 80, PrivatePort: 90, IP: "localhost"}}
|
||||||
c3, inspect3 := containerDetails("service2", "789", "exited", "", 130)
|
c3, inspect3 := containerDetails("service2", "789", "exited", "", 130)
|
||||||
|
api.EXPECT().VolumeList(ctx, gomock.Any()).Return(volume.VolumeListOKBody{}, nil)
|
||||||
|
api.EXPECT().NetworkList(ctx, gomock.Any()).Return([]moby.NetworkResource{}, nil)
|
||||||
api.EXPECT().ContainerList(ctx, listOpts).Return([]moby.Container{c1, c2, c3}, nil)
|
api.EXPECT().ContainerList(ctx, listOpts).Return([]moby.Container{c1, c2, c3}, nil)
|
||||||
api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil)
|
api.EXPECT().ContainerInspect(anyCancellableContext(), "123").Return(inspect1, nil)
|
||||||
api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)
|
api.EXPECT().ContainerInspect(anyCancellableContext(), "456").Return(inspect2, nil)
|
||||||
|
|
Loading…
Reference in New Issue