mirror of https://github.com/docker/cli.git
Merge pull request #340 from thaJeztah/fix-dont-prune-build-cache
Fix: skip build-cache prune on unsupported versions
This commit is contained in:
commit
277f4b997c
|
@ -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
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ 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"
|
||||||
|
@ -17,13 +18,14 @@ import (
|
||||||
type pruneOptions struct {
|
type pruneOptions struct {
|
||||||
force bool
|
force bool
|
||||||
all bool
|
all bool
|
||||||
|
pruneBuildCache bool
|
||||||
pruneVolumes bool
|
pruneVolumes bool
|
||||||
filter opts.FilterOpt
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.pruneBuildCache {
|
||||||
report, err := dockerCli.Client().BuildCachePrune(context.Background())
|
report, err := dockerCli.Client().BuildCachePrune(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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")
|
||||||
}
|
}
|
||||||
|
if options.pruneBuildCache {
|
||||||
warnings = append(warnings, "all build cache")
|
warnings = append(warnings, "all build cache")
|
||||||
|
}
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
t.Execute(&buffer, &warnings)
|
t.Execute(&buffer, &warnings)
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
Loading…
Reference in New Issue