Skip to content

Commit 849b0e9

Browse files
committed
system prune: only warn about volumes if --volumes is given
Signed-off-by: Harald Albers <github@albersweb.de>
1 parent 92a2a1d commit 849b0e9

1 file changed

Lines changed: 30 additions & 20 deletions

File tree

cli/command/system/prune.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package system
22

33
import (
4+
"bytes"
45
"fmt"
6+
"text/template"
57

68
"github.com/docker/cli/cli"
79
"github.com/docker/cli/cli/command"
@@ -44,29 +46,14 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
4446
return cmd
4547
}
4648

47-
const (
48-
warning = `WARNING! This will remove:
49-
- all stopped containers
50-
- all volumes not used by at least one container
51-
- all networks not used by at least one container
52-
%s
53-
- all build cache
49+
const confirmationTemplate = `WARNING! This will remove:
50+
{{- range $_, $warning := . }}
51+
- {{ $warning }}
52+
{{- end }}
5453
Are you sure you want to continue?`
5554

56-
danglingImageDesc = "- all dangling images"
57-
allImageDesc = `- all images without at least one container associated to them`
58-
)
59-
6055
func runPrune(dockerCli command.Cli, options pruneOptions) error {
61-
var message string
62-
63-
if options.all {
64-
message = fmt.Sprintf(warning, allImageDesc)
65-
} else {
66-
message = fmt.Sprintf(warning, danglingImageDesc)
67-
}
68-
69-
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
56+
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) {
7057
return nil
7158
}
7259

@@ -109,3 +96,26 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error {
10996

11097
return nil
11198
}
99+
100+
// confirmationMessage constructs a confirmation message that depends on the cli options.
101+
func confirmationMessage(options pruneOptions) string {
102+
t := template.Must(template.New("confirmation message").Parse(confirmationTemplate))
103+
104+
warnings := []string{
105+
"all stopped containers",
106+
"all networks not used by at least one container",
107+
}
108+
if options.pruneVolumes {
109+
warnings = append(warnings, "all volumes not used by at least one container")
110+
}
111+
if options.all {
112+
warnings = append(warnings, "all images without at least one container associated to them")
113+
} else {
114+
warnings = append(warnings, "all dangling images")
115+
}
116+
warnings = append(warnings, "all build cache")
117+
118+
var buffer bytes.Buffer
119+
t.Execute(&buffer, &warnings)
120+
return buffer.String()
121+
}

0 commit comments

Comments
 (0)