Skip to content

Commit a473341

Browse files
committed
volume ls command can run without a project
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 385b3f5 commit a473341

File tree

5 files changed

+15
-35
lines changed

5 files changed

+15
-35
lines changed

cmd/compose/volumes.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,21 @@ func volumesCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Servic
5555
}
5656

5757
func runVol(ctx context.Context, dockerCli command.Cli, backend api.Service, services []string, options volumesOptions) error {
58-
project, _, err := options.projectOrName(ctx, dockerCli, services...)
58+
project, name, err := options.projectOrName(ctx, dockerCli, services...)
5959
if err != nil {
6060
return err
6161
}
6262

63-
names := project.ServiceNames()
64-
65-
if len(services) == 0 {
66-
services = names
67-
}
68-
69-
for _, service := range services {
70-
if !slices.Contains(names, service) {
71-
return fmt.Errorf("no such service: %s", service)
63+
if project != nil {
64+
names := project.ServiceNames()
65+
for _, service := range services {
66+
if !slices.Contains(names, service) {
67+
return fmt.Errorf("no such service: %s", service)
68+
}
7269
}
7370
}
7471

75-
volumes, err := backend.Volumes(ctx, project, api.VolumesOptions{
72+
volumes, err := backend.Volumes(ctx, name, api.VolumesOptions{
7673
Services: services,
7774
})
7875
if err != nil {

pkg/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type Service interface {
101101
// Generate generates a Compose Project from existing containers
102102
Generate(ctx context.Context, options GenerateOptions) (*types.Project, error)
103103
// Volumes executes the equivalent to a `docker volume ls`
104-
Volumes(ctx context.Context, project *types.Project, options VolumesOptions) ([]VolumesSummary, error)
104+
Volumes(ctx context.Context, project string, options VolumesOptions) ([]VolumesSummary, error)
105105
}
106106

107107
type VolumesOptions struct {

pkg/compose/volumes.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@ import (
2020
"context"
2121
"slices"
2222

23-
"github.com/compose-spec/compose-go/v2/types"
2423
"github.com/docker/compose/v2/pkg/api"
2524
"github.com/docker/docker/api/types/container"
2625
"github.com/docker/docker/api/types/filters"
2726
"github.com/docker/docker/api/types/volume"
2827
)
2928

30-
func (s *composeService) Volumes(ctx context.Context, project *types.Project, options api.VolumesOptions) ([]api.VolumesSummary, error) {
31-
projectName := project.Name
32-
29+
func (s *composeService) Volumes(ctx context.Context, project string, options api.VolumesOptions) ([]api.VolumesSummary, error) {
3330
allContainers, err := s.apiClient().ContainerList(ctx, container.ListOptions{
34-
Filters: filters.NewArgs(projectFilter(projectName)),
31+
Filters: filters.NewArgs(projectFilter(project)),
3532
})
3633
if err != nil {
3734
return nil, err
@@ -51,7 +48,7 @@ func (s *composeService) Volumes(ctx context.Context, project *types.Project, op
5148
}
5249

5350
volumesResponse, err := s.apiClient().VolumeList(ctx, volume.ListOptions{
54-
Filters: filters.NewArgs(projectFilter(projectName)),
51+
Filters: filters.NewArgs(projectFilter(project)),
5552
})
5653
if err != nil {
5754
return nil, err

pkg/compose/volumes_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"testing"
2222

23-
"github.com/compose-spec/compose-go/v2/types"
2423
"github.com/docker/compose/v2/pkg/api"
2524
"github.com/docker/docker/api/types/container"
2625
"github.com/docker/docker/api/types/filters"
@@ -59,7 +58,6 @@ func TestVolumes(t *testing.T) {
5958
}
6059

6160
ctx := context.Background()
62-
project := &types.Project{Name: testProject}
6361
args := filters.NewArgs(projectFilter(testProject))
6462
listOpts := container.ListOptions{Filters: args}
6563
volumeListArgs := filters.NewArgs(projectFilter(testProject))
@@ -75,14 +73,14 @@ func TestVolumes(t *testing.T) {
7573

7674
// Test without service filter - should return all project volumes
7775
volumeOptions := api.VolumesOptions{}
78-
volumes, err := tested.Volumes(ctx, project, volumeOptions)
76+
volumes, err := tested.Volumes(ctx, testProject, volumeOptions)
7977
expected := []api.VolumesSummary{vol1, vol2, vol3}
8078
assert.NilError(t, err)
8179
assert.DeepEqual(t, volumes, expected)
8280

8381
// Test with service filter - should only return volumes used by service1
8482
volumeOptions = api.VolumesOptions{Services: []string{"service1"}}
85-
volumes, err = tested.Volumes(ctx, project, volumeOptions)
83+
volumes, err = tested.Volumes(ctx, testProject, volumeOptions)
8684
expected = []api.VolumesSummary{vol1, vol2}
8785
assert.NilError(t, err)
8886
assert.DeepEqual(t, volumes, expected)

pkg/mocks/mock_docker_compose_api.go

Lines changed: 1 addition & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)