|
1 | 1 | package system |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
| 6 | + "text/template" |
5 | 7 |
|
6 | 8 | "github.com/docker/cli/cli" |
7 | 9 | "github.com/docker/cli/cli/command" |
@@ -44,29 +46,14 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command { |
44 | 46 | return cmd |
45 | 47 | } |
46 | 48 |
|
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 }} |
54 | 53 | Are you sure you want to continue?` |
55 | 54 |
|
56 | | - danglingImageDesc = "- all dangling images" |
57 | | - allImageDesc = `- all images without at least one container associated to them` |
58 | | -) |
59 | | - |
60 | 55 | 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)) { |
70 | 57 | return nil |
71 | 58 | } |
72 | 59 |
|
@@ -109,3 +96,26 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error { |
109 | 96 |
|
110 | 97 | return nil |
111 | 98 | } |
| 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