Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 98187b2

Browse files
committed
down --volume to remove volume
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent b46015f commit 98187b2

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

local/compose/down.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
9898
}
9999
}
100100

101+
if options.Volumes {
102+
networks, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
103+
if err != nil {
104+
return err
105+
}
106+
for _, vol := range networks.Volumes {
107+
id := vol.Name
108+
eg.Go(func() error {
109+
resourceToRemove = true
110+
return s.removeVolume(ctx, id, w)
111+
})
112+
}
113+
}
114+
101115
if !resourceToRemove {
102116
w.Event(progress.NewEvent(projectName, progress.Done, "Warning: No resource found to remove"))
103117
}
@@ -134,6 +148,21 @@ func (s *composeService) removeImage(ctx context.Context, image string, w progre
134148
return err
135149
}
136150

151+
func (s *composeService) removeVolume(ctx context.Context, id string, w progress.Writer) error {
152+
resource := fmt.Sprintf("Volume %s", id)
153+
w.Event(progress.NewEvent(resource, progress.Working, "Removing"))
154+
err := s.apiClient.VolumeRemove(ctx, id, true)
155+
if err == nil {
156+
w.Event(progress.NewEvent(resource, progress.Done, "Removed"))
157+
return nil
158+
}
159+
if errdefs.IsNotFound(err) {
160+
w.Event(progress.NewEvent(resource, progress.Done, "Warning: No resource found to remove"))
161+
return nil
162+
}
163+
return err
164+
}
165+
137166
func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration) error {
138167
for _, container := range containers {
139168
toStop := container

local/compose/down_test.go

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

23-
"github.com/docker/docker/api/types/filters"
24-
2523
"github.com/docker/compose-cli/api/compose"
2624
"github.com/docker/compose-cli/local/mocks"
2725

2826
apitypes "github.com/docker/docker/api/types"
27+
"github.com/docker/docker/api/types/filters"
28+
"github.com/docker/docker/api/types/volume"
2929
"github.com/golang/mock/gomock"
3030
"gotest.tools/v3/assert"
3131
)
@@ -79,3 +79,24 @@ func TestDownRemoveOrphans(t *testing.T) {
7979
err := tested.Down(context.Background(), testProject, compose.DownOptions{RemoveOrphans: true})
8080
assert.NilError(t, err)
8181
}
82+
83+
func TestDownRemoveVolumes(t *testing.T) {
84+
mockCtrl := gomock.NewController(t)
85+
defer mockCtrl.Finish()
86+
api := mocks.NewMockAPIClient(mockCtrl)
87+
tested.apiClient = api
88+
89+
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
90+
[]apitypes.Container{testContainer("service1", "123")}, nil)
91+
92+
api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
93+
api.EXPECT().ContainerRemove(gomock.Any(), "123", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
94+
95+
api.EXPECT().NetworkList(gomock.Any(), apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return(nil, nil)
96+
97+
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(testProject))).Return(volume.VolumeListOKBody{Volumes: []*apitypes.Volume{{Name: "myProject_volume"}}}, nil)
98+
api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)
99+
100+
err := tested.Down(context.Background(), testProject, compose.DownOptions{Volumes: true})
101+
assert.NilError(t, err)
102+
}

local/e2e/compose/volumes_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ func TestLocalComposeVolume(t *testing.T) {
7272
})
7373

7474
t.Run("cleanup volume project", func(t *testing.T) {
75-
c.RunDockerCmd("compose", "--project-name", projectName, "down")
76-
c.RunDockerCmd("volume", "rm", projectName+"_staticVol")
75+
c.RunDockerCmd("compose", "--project-name", projectName, "down", "--volumes")
76+
res := c.RunDockerCmd("volume", "ls")
77+
assert.Assert(t, !strings.Contains(res.Stdout(), projectName+"_staticVol"))
78+
assert.Assert(t, !strings.Contains(res.Stdout(), "myvolume"))
7779
})
7880
}

0 commit comments

Comments
 (0)