Skip to content

Commit 57d7223

Browse files
authored
Merge pull request #4854 from krissetto/uniform-volume-prune-output
Uniform output of volume prune cmd with other prune cmds
2 parents ce3b07c + 69e0f53 commit 57d7223

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

cli/command/volume/prune.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
3232
RunE: func(cmd *cobra.Command, args []string) error {
3333
spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options)
3434
if err != nil {
35+
if errdefs.IsCancelled(err) {
36+
fmt.Fprintln(dockerCli.Out(), output)
37+
return nil
38+
}
3539
return err
3640
}
3741
if output != "" {
@@ -77,7 +81,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
7781
warning = allVolumesWarning
7882
}
7983
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
80-
return 0, "", nil
84+
return 0, "", errdefs.Cancelled(errors.New("user cancelled operation"))
8185
}
8286

8387
report, err := dockerCli.Client().VolumesPrune(ctx, pruneFilters)

cli/command/volume/prune_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ func TestVolumePruneSuccess(t *testing.T) {
7373
testCases := []struct {
7474
name string
7575
args []string
76+
input string
7677
volumePruneFunc func(args filters.Args) (types.VolumesPruneReport, error)
7778
}{
7879
{
79-
name: "all",
80-
args: []string{"--all"},
80+
name: "all",
81+
args: []string{"--all"},
82+
input: "y",
8183
volumePruneFunc: func(pruneFilter filters.Args) (types.VolumesPruneReport, error) {
82-
assert.Check(t, is.Equal([]string{"true"}, pruneFilter.Get("all")))
84+
assert.Check(t, is.DeepEqual([]string{"true"}, pruneFilter.Get("all")))
8385
return types.VolumesPruneReport{}, nil
8486
},
8587
},
@@ -91,10 +93,11 @@ func TestVolumePruneSuccess(t *testing.T) {
9193
},
9294
},
9395
{
94-
name: "label-filter",
95-
args: []string{"--filter", "label=foobar"},
96+
name: "label-filter",
97+
args: []string{"--filter", "label=foobar"},
98+
input: "y",
9699
volumePruneFunc: func(pruneFilter filters.Args) (types.VolumesPruneReport, error) {
97-
assert.Check(t, is.Equal([]string{"foobar"}, pruneFilter.Get("label")))
100+
assert.Check(t, is.DeepEqual([]string{"foobar"}, pruneFilter.Get("label")))
98101
return types.VolumesPruneReport{}, nil
99102
},
100103
},
@@ -104,6 +107,9 @@ func TestVolumePruneSuccess(t *testing.T) {
104107
t.Run(tc.name, func(t *testing.T) {
105108
cli := test.NewFakeCli(&fakeClient{volumePruneFunc: tc.volumePruneFunc})
106109
cmd := NewPruneCommand(cli)
110+
if tc.input != "" {
111+
cli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader(tc.input))))
112+
}
107113
cmd.SetOut(io.Discard)
108114
cmd.SetArgs(tc.args)
109115
err := cmd.Execute()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
WARNING! This will remove anonymous local volumes not used by at least one container.
2-
Are you sure you want to continue? [y/N] Total reclaimed space: 0B
2+
Are you sure you want to continue? [y/N]

0 commit comments

Comments
 (0)