Skip to content

Commit

Permalink
Merge pull request #4854 from krissetto/uniform-volume-prune-output
Browse files Browse the repository at this point in the history
Uniform output of volume prune cmd with other prune cmds
  • Loading branch information
thaJeztah authored Feb 6, 2024
2 parents ce3b07c + 69e0f53 commit 57d7223
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 5 additions & 1 deletion cli/command/volume/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options)
if err != nil {
if errdefs.IsCancelled(err) {
fmt.Fprintln(dockerCli.Out(), output)
return nil
}
return err
}
if output != "" {
Expand Down Expand Up @@ -77,7 +81,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
warning = allVolumesWarning
}
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
return 0, "", nil
return 0, "", errdefs.Cancelled(errors.New("user cancelled operation"))
}

report, err := dockerCli.Client().VolumesPrune(ctx, pruneFilters)
Expand Down
18 changes: 12 additions & 6 deletions cli/command/volume/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ func TestVolumePruneSuccess(t *testing.T) {
testCases := []struct {
name string
args []string
input string
volumePruneFunc func(args filters.Args) (types.VolumesPruneReport, error)
}{
{
name: "all",
args: []string{"--all"},
name: "all",
args: []string{"--all"},
input: "y",
volumePruneFunc: func(pruneFilter filters.Args) (types.VolumesPruneReport, error) {
assert.Check(t, is.Equal([]string{"true"}, pruneFilter.Get("all")))
assert.Check(t, is.DeepEqual([]string{"true"}, pruneFilter.Get("all")))
return types.VolumesPruneReport{}, nil
},
},
Expand All @@ -91,10 +93,11 @@ func TestVolumePruneSuccess(t *testing.T) {
},
},
{
name: "label-filter",
args: []string{"--filter", "label=foobar"},
name: "label-filter",
args: []string{"--filter", "label=foobar"},
input: "y",
volumePruneFunc: func(pruneFilter filters.Args) (types.VolumesPruneReport, error) {
assert.Check(t, is.Equal([]string{"foobar"}, pruneFilter.Get("label")))
assert.Check(t, is.DeepEqual([]string{"foobar"}, pruneFilter.Get("label")))
return types.VolumesPruneReport{}, nil
},
},
Expand All @@ -104,6 +107,9 @@ func TestVolumePruneSuccess(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{volumePruneFunc: tc.volumePruneFunc})
cmd := NewPruneCommand(cli)
if tc.input != "" {
cli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader(tc.input))))
}
cmd.SetOut(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
Expand Down
2 changes: 1 addition & 1 deletion cli/command/volume/testdata/volume-prune-no.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] Total reclaimed space: 0B
Are you sure you want to continue? [y/N]

0 comments on commit 57d7223

Please sign in to comment.