mirror of https://github.com/docker/cli.git
Merge pull request #1524 from thaJeztah/fix_system_prune_output
Fix "warning" output on docker system prune
This commit is contained in:
commit
69f1727248
|
@ -3,6 +3,7 @@ package system
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"text/template"
|
||||
|
||||
"github.com/docker/cli/cli"
|
||||
|
@ -14,8 +15,9 @@ import (
|
|||
"github.com/docker/cli/cli/command/volume"
|
||||
"github.com/docker/cli/opts"
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
units "github.com/docker/go-units"
|
||||
"github.com/docker/go-units"
|
||||
"github.com/spf13/cobra"
|
||||
"vbom.ml/util/sortorder"
|
||||
)
|
||||
|
||||
type pruneOptions struct {
|
||||
|
@ -53,9 +55,15 @@ func newPruneCommand(dockerCli command.Cli) *cobra.Command {
|
|||
}
|
||||
|
||||
const confirmationTemplate = `WARNING! This will remove:
|
||||
{{- range $_, $warning := . }}
|
||||
- {{ $warning }}
|
||||
{{- range $_, $warning := .warnings }}
|
||||
- {{ $warning }}
|
||||
{{- end }}
|
||||
{{if .filters}}
|
||||
Items to be pruned will be filtered with:
|
||||
{{- range $_, $filters := .filters }}
|
||||
- {{ $filters }}
|
||||
{{- end }}
|
||||
{{end}}
|
||||
Are you sure you want to continue?`
|
||||
|
||||
func runPrune(dockerCli command.Cli, options pruneOptions) error {
|
||||
|
@ -63,7 +71,7 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error {
|
|||
if options.pruneVolumes && options.filter.Value().Contains("until") {
|
||||
return fmt.Errorf(`ERROR: The "until" filter is not supported with "--volumes"`)
|
||||
}
|
||||
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) {
|
||||
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(dockerCli, options)) {
|
||||
return nil
|
||||
}
|
||||
pruneFuncs := []func(dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint64, string, error){
|
||||
|
@ -96,7 +104,7 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error {
|
|||
}
|
||||
|
||||
// confirmationMessage constructs a confirmation message that depends on the cli options.
|
||||
func confirmationMessage(options pruneOptions) string {
|
||||
func confirmationMessage(dockerCli command.Cli, options pruneOptions) string {
|
||||
t := template.Must(template.New("confirmation message").Parse(confirmationTemplate))
|
||||
|
||||
warnings := []string{
|
||||
|
@ -118,12 +126,23 @@ func confirmationMessage(options pruneOptions) string {
|
|||
warnings = append(warnings, "all dangling build cache")
|
||||
}
|
||||
}
|
||||
if len(options.filter.String()) > 0 {
|
||||
warnings = append(warnings, "Elements to be pruned will be filtered with:")
|
||||
warnings = append(warnings, "label="+options.filter.String())
|
||||
|
||||
var filters []string
|
||||
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
|
||||
if pruneFilters.Len() > 0 {
|
||||
// TODO remove fixed list of filters, and print all filters instead,
|
||||
// because the list of filters that is supported by the engine may evolve over time.
|
||||
for _, name := range []string{"label", "label!", "until"} {
|
||||
for _, v := range pruneFilters.Get(name) {
|
||||
filters = append(filters, name+"="+v)
|
||||
}
|
||||
}
|
||||
sort.Slice(filters, func(i, j int) bool {
|
||||
return sortorder.NaturalLess(filters[i], filters[j])
|
||||
})
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
t.Execute(&buffer, &warnings)
|
||||
t.Execute(&buffer, map[string][]string{"warnings": warnings, "filters": filters})
|
||||
return buffer.String()
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package system
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/docker/cli/cli/config/configfile"
|
||||
"github.com/docker/cli/internal/test"
|
||||
"gotest.tools/assert"
|
||||
is "gotest.tools/assert/cmp"
|
||||
|
@ -11,12 +12,40 @@ import (
|
|||
func TestPrunePromptPre131DoesNotIncludeBuildCache(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{version: "1.30"})
|
||||
cmd := newPruneCommand(cli)
|
||||
cmd.SetArgs([]string{})
|
||||
assert.NilError(t, cmd.Execute())
|
||||
expected := `WARNING! This will remove:
|
||||
- all stopped containers
|
||||
- all networks not used by at least one container
|
||||
- all dangling images
|
||||
- all stopped containers
|
||||
- all networks not used by at least one container
|
||||
- all dangling images
|
||||
|
||||
Are you sure you want to continue? [y/N] `
|
||||
assert.Check(t, is.Equal(expected, cli.OutBuffer().String()))
|
||||
}
|
||||
|
||||
func TestPrunePromptFilters(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{version: "1.31"})
|
||||
cli.SetConfigFile(&configfile.ConfigFile{
|
||||
PruneFilters: []string{"label!=never=remove-me", "label=remove=me"},
|
||||
})
|
||||
cmd := newPruneCommand(cli)
|
||||
cmd.SetArgs([]string{"--filter", "until=24h", "--filter", "label=hello-world", "--filter", "label!=foo=bar", "--filter", "label=bar=baz"})
|
||||
|
||||
assert.NilError(t, cmd.Execute())
|
||||
expected := `WARNING! This will remove:
|
||||
- all stopped containers
|
||||
- all networks not used by at least one container
|
||||
- all dangling images
|
||||
- all dangling build cache
|
||||
|
||||
Items to be pruned will be filtered with:
|
||||
- label!=foo=bar
|
||||
- label!=never=remove-me
|
||||
- label=bar=baz
|
||||
- label=hello-world
|
||||
- label=remove=me
|
||||
- until=24h
|
||||
|
||||
Are you sure you want to continue? [y/N] `
|
||||
assert.Check(t, is.Equal(expected, cli.OutBuffer().String()))
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue