MCPcopy
hub / github.com/docker/compose / Volumes

Method Volumes

pkg/compose/volumes.go:29–82  ·  view source on GitHub ↗
(ctx context.Context, project string, options api.VolumesOptions)

Source from the content-addressed store, hash-verified

27)
28
29func (s *composeService) Volumes(ctx context.Context, project string, options api.VolumesOptions) ([]api.VolumesSummary, error) {
30 allContainers, err := s.apiClient().ContainerList(ctx, client.ContainerListOptions{
31 Filters: projectFilter(project),
32 })
33 if err != nil {
34 return nil, err
35 }
36
37 var containers []container.Summary
38
39 if len(options.Services) > 0 {
40 // filter service containers
41 for _, c := range allContainers.Items {
42 if slices.Contains(options.Services, c.Labels[api.ServiceLabel]) {
43 containers = append(containers, c)
44 }
45 }
46 } else {
47 containers = allContainers.Items
48 }
49
50 volumesResponse, err := s.apiClient().VolumeList(ctx, client.VolumeListOptions{
51 Filters: projectFilter(project),
52 })
53 if err != nil {
54 return nil, err
55 }
56
57 projectVolumes := volumesResponse.Items
58
59 if len(options.Services) == 0 {
60 return projectVolumes, nil
61 }
62
63 var volumes []api.VolumesSummary
64
65 // create a name lookup of volumes used by containers
66 serviceVolumes := make(map[string]bool)
67
68 for _, ctr := range containers {
69 for _, mount := range ctr.Mounts {
70 serviceVolumes[mount.Name] = true
71 }
72 }
73
74 // append if volumes in this project are in serviceVolumes
75 for _, v := range projectVolumes {
76 if serviceVolumes[v.Name] {
77 volumes = append(volumes, v)
78 }
79 }
80
81 return volumes, nil
82}

Callers 1

TestVolumesFunction · 0.95

Calls 4

apiClientMethod · 0.95
projectFilterFunction · 0.85
ContainerListMethod · 0.45
VolumeListMethod · 0.45

Tested by 1

TestVolumesFunction · 0.76