Merge pull request #340 from thaJeztah/fix-dont-prune-build-cache

Fix: skip build-cache prune on unsupported versions
This commit is contained in:
Vincent Demeester 2017-07-18 09:41:37 +02:00 committed by GitHub
commit 277f4b997c
3 changed files with 49 additions and 10 deletions

View File

@ -0,0 +1,15 @@
package system
import (
"github.com/docker/docker/client"
)
type fakeClient struct {
client.Client
version string
}
func (cli *fakeClient) ClientVersion() string {
return cli.version
}

View File

@ -9,21 +9,23 @@ import (
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/prune" "github.com/docker/cli/cli/command/prune"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types/versions"
units "github.com/docker/go-units" units "github.com/docker/go-units"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
type pruneOptions struct { type pruneOptions struct {
force bool force bool
all bool all bool
pruneVolumes bool pruneBuildCache bool
filter opts.FilterOpt pruneVolumes bool
filter opts.FilterOpt
} }
// NewPruneCommand creates a new cobra.Command for `docker prune` // NewPruneCommand creates a new cobra.Command for `docker prune`
func NewPruneCommand(dockerCli command.Cli) *cobra.Command { func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
options := pruneOptions{filter: opts.NewFilterOpt()} options := pruneOptions{filter: opts.NewFilterOpt(), pruneBuildCache: true}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "prune [OPTIONS]", Use: "prune [OPTIONS]",
@ -53,6 +55,9 @@ const confirmationTemplate = `WARNING! This will remove:
Are you sure you want to continue?` Are you sure you want to continue?`
func runPrune(dockerCli command.Cli, options pruneOptions) error { func runPrune(dockerCli command.Cli, options pruneOptions) error {
if versions.LessThan(dockerCli.Client().ClientVersion(), "1.31") {
options.pruneBuildCache = false
}
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) { if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) {
return nil return nil
} }
@ -86,11 +91,13 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error {
fmt.Fprintln(dockerCli.Out(), output) fmt.Fprintln(dockerCli.Out(), output)
} }
report, err := dockerCli.Client().BuildCachePrune(context.Background()) if options.pruneBuildCache {
if err != nil { report, err := dockerCli.Client().BuildCachePrune(context.Background())
return err if err != nil {
return err
}
spaceReclaimed += report.SpaceReclaimed
} }
spaceReclaimed += report.SpaceReclaimed
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed))) fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
@ -113,7 +120,9 @@ func confirmationMessage(options pruneOptions) string {
} else { } else {
warnings = append(warnings, "all dangling images") warnings = append(warnings, "all dangling images")
} }
warnings = append(warnings, "all build cache") if options.pruneBuildCache {
warnings = append(warnings, "all build cache")
}
var buffer bytes.Buffer var buffer bytes.Buffer
t.Execute(&buffer, &warnings) t.Execute(&buffer, &warnings)

View File

@ -0,0 +1,15 @@
package system
import (
"testing"
"github.com/docker/cli/cli/internal/test"
"github.com/stretchr/testify/assert"
)
func TestPrunePromptPre131(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{version: "1.30"})
cmd := NewPruneCommand(cli)
assert.NoError(t, cmd.Execute())
assert.NotContains(t, cli.OutBuffer().String(), "all build cache")
}