Make system prune warning filters human-readable

The warning, printed before running `docker system prune` was printing the
filters in JSON format.

This patch attempts to make the output human readable;

- updating the code, and template to print filters individually
- reducing the indentation (which was quite deep)

Before this patch was applied;

```
docker system prune --filter until=24h --filter label=hello-world --filter label!=foo=bar --filter label=bar=baz

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all dangling build cache
        - Elements to be pruned will be filtered with:
        - label={"label":{"bar=baz":true,"hello-world":true},"label!":{"foo=bar":true},"until":{"24h":true}}
Are you sure you want to continue? [y/N]
```

With this patch applied;

```
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]
```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2018-11-17 16:53:02 +01:00
parent 4a0218bb11
commit 26e004797b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 41 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package system
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"sort"
"text/template" "text/template"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
@ -13,10 +14,10 @@ import (
"github.com/docker/cli/cli/command/network" "github.com/docker/cli/cli/command/network"
"github.com/docker/cli/cli/command/volume" "github.com/docker/cli/cli/command/volume"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/versions" "github.com/docker/docker/api/types/versions"
units "github.com/docker/go-units" "github.com/docker/go-units"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"vbom.ml/util/sortorder"
) )
type pruneOptions struct { type pruneOptions struct {
@ -54,9 +55,15 @@ func newPruneCommand(dockerCli command.Cli) *cobra.Command {
} }
const confirmationTemplate = `WARNING! This will remove: const confirmationTemplate = `WARNING! This will remove:
{{- range $_, $warning := . }} {{- range $_, $warning := .warnings }}
- {{ $warning }} - {{ $warning }}
{{- end }} {{- end }}
{{if .filters}}
Items to be pruned will be filtered with:
{{- range $_, $filters := .filters }}
- {{ $filters }}
{{- end }}
{{end}}
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 {
@ -119,17 +126,23 @@ func confirmationMessage(dockerCli command.Cli, options pruneOptions) string {
warnings = append(warnings, "all dangling build cache") warnings = append(warnings, "all dangling build cache")
} }
} }
var filters []string
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value()) pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
if pruneFilters.Len() > 0 { if pruneFilters.Len() > 0 {
f, err := filters.ToJSON(pruneFilters) // TODO remove fixed list of filters, and print all filters instead,
if err != nil { // because the list of filters that is supported by the engine may evolve over time.
f = "invalid filters" for _, name := range []string{"label", "label!", "until"} {
for _, v := range pruneFilters.Get(name) {
filters = append(filters, name+"="+v)
}
} }
warnings = append(warnings, "Elements to be pruned will be filtered with:") sort.Slice(filters, func(i, j int) bool {
warnings = append(warnings, "filter="+f) return sortorder.NaturalLess(filters[i], filters[j])
})
} }
var buffer bytes.Buffer var buffer bytes.Buffer
t.Execute(&buffer, &warnings) t.Execute(&buffer, map[string][]string{"warnings": warnings, "filters": filters})
return buffer.String() return buffer.String()
} }

View File

@ -15,15 +15,16 @@ func TestPrunePromptPre131DoesNotIncludeBuildCache(t *testing.T) {
cmd.SetArgs([]string{}) cmd.SetArgs([]string{})
assert.NilError(t, cmd.Execute()) assert.NilError(t, cmd.Execute())
expected := `WARNING! This will remove: expected := `WARNING! This will remove:
- all stopped containers - all stopped containers
- all networks not used by at least one container - all networks not used by at least one container
- all dangling images - all dangling images
Are you sure you want to continue? [y/N] ` Are you sure you want to continue? [y/N] `
assert.Check(t, is.Equal(expected, cli.OutBuffer().String())) assert.Check(t, is.Equal(expected, cli.OutBuffer().String()))
} }
func TestPrunePromptFilters(t *testing.T) { func TestPrunePromptFilters(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{version: "1.30"}) cli := test.NewFakeCli(&fakeClient{version: "1.31"})
cli.SetConfigFile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
PruneFilters: []string{"label!=never=remove-me", "label=remove=me"}, PruneFilters: []string{"label!=never=remove-me", "label=remove=me"},
}) })
@ -32,11 +33,19 @@ func TestPrunePromptFilters(t *testing.T) {
assert.NilError(t, cmd.Execute()) assert.NilError(t, cmd.Execute())
expected := `WARNING! This will remove: expected := `WARNING! This will remove:
- all stopped containers - all stopped containers
- all networks not used by at least one container - all networks not used by at least one container
- all dangling images - all dangling images
- Elements to be pruned will be filtered with: - all dangling build cache
- filter={"label":{"bar=baz":true,"hello-world":true,"remove=me":true},"label!":{"foo=bar":true,"never=remove-me":true},"until":{"24h":true}}
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] ` Are you sure you want to continue? [y/N] `
assert.Check(t, is.Equal(expected, cli.OutBuffer().String())) assert.Check(t, is.Equal(expected, cli.OutBuffer().String()))
} }