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