Skip to content

Commit

Permalink
system prune: only warn about volumes if --volumes is given
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Albers <github@albersweb.de>
  • Loading branch information
albers committed Jun 29, 2017
1 parent 92a2a1d commit 849b0e9
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions cli/command/system/prune.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package system

import (
"bytes"
"fmt"
"text/template"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -44,29 +46,14 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

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

danglingImageDesc = "- all dangling images"
allImageDesc = `- all images without at least one container associated to them`
)

func runPrune(dockerCli command.Cli, options pruneOptions) error {
var message string

if options.all {
message = fmt.Sprintf(warning, allImageDesc)
} else {
message = fmt.Sprintf(warning, danglingImageDesc)
}

if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) {
return nil
}

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

return nil
}

// confirmationMessage constructs a confirmation message that depends on the cli options.
func confirmationMessage(options pruneOptions) string {
t := template.Must(template.New("confirmation message").Parse(confirmationTemplate))

warnings := []string{
"all stopped containers",
"all networks not used by at least one container",
}
if options.pruneVolumes {
warnings = append(warnings, "all volumes not used by at least one container")
}
if options.all {
warnings = append(warnings, "all images without at least one container associated to them")
} else {
warnings = append(warnings, "all dangling images")
}
warnings = append(warnings, "all build cache")

var buffer bytes.Buffer
t.Execute(&buffer, &warnings)
return buffer.String()
}

0 comments on commit 849b0e9

Please sign in to comment.