2016-09-22 17:04:34 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-22 17:04:34 -04:00
|
|
|
"fmt"
|
golangci-lint: enable perfsprint linter
cli/compose/types/types.go:568:17: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return []byte(fmt.Sprintf("%v", e.External)), nil
^
cli/command/formatter/buildcache.go:174:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
return fmt.Sprintf("%d", c.v.UsageCount)
^
cli/command/formatter/buildcache.go:178:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.InUse)
^
cli/command/formatter/buildcache.go:182:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.Shared)
^
cli/command/formatter/image.go:259:9: fmt.Sprintf can be replaced with faster strconv.FormatInt (perfsprint)
return fmt.Sprintf("%d", c.i.Containers)
^
cli/command/formatter/tabwriter/tabwriter_test.go:698:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", x), func(b *testing.B) {
^
cli/command/formatter/tabwriter/tabwriter_test.go:720:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", h), func(b *testing.B) {
^
cli/command/image/prune.go:62:31: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all))
^
cli/command/network/formatter.go:92:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.EnableIPv6)
^
cli/command/network/formatter.go:96:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.Internal)
^
cli/command/service/formatter.go:745:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
pub = fmt.Sprintf("%d", pr.pStart)
^
cli/command/service/formatter.go:750:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
tgt = fmt.Sprintf("%d", pr.tStart)
^
cli/command/service/opts.go:49:10: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
return fmt.Sprintf("%v", *i.value)
^
cli/compose/loader/loader.go:720:36: fmt.Sprint can be replaced with faster strconv.Itoa (perfsprint)
v, err := toServicePortConfigs(fmt.Sprint(value))
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 10:18:19 -05:00
|
|
|
"strconv"
|
2018-10-08 22:33:27 -04:00
|
|
|
"strings"
|
2016-09-22 17:04:34 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-22 17:04:34 -04:00
|
|
|
units "github.com/docker/go-units"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pruneOptions struct {
|
2016-12-07 17:02:13 -05:00
|
|
|
force bool
|
|
|
|
all bool
|
|
|
|
filter opts.FilterOpt
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPruneCommand returns a new cobra prune command for images
|
2017-03-30 20:21:14 -04:00
|
|
|
func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := pruneOptions{filter: opts.NewFilterOpt()}
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-10-10 11:07:32 -04:00
|
|
|
Use: "prune [OPTIONS]",
|
2016-09-22 17:04:34 -04:00
|
|
|
Short: "Remove unused images",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 18:27:44 -04:00
|
|
|
spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options)
|
2016-09-22 17:04:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if output != "" {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), output)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
|
|
|
|
return nil
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
Annotations: map[string]string{"version": "1.25"},
|
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
|
|
|
|
flags.BoolVarP(&options.all, "all", "a", false, "Remove all unused images, not just dangling ones")
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.Var(&options.filter, "filter", `Provide filter values (e.g. "until=<timestamp>")`)
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
allImageWarning = `WARNING! This will remove all images without at least one container associated to them.
|
|
|
|
Are you sure you want to continue?`
|
|
|
|
danglingWarning = `WARNING! This will remove all dangling images.
|
|
|
|
Are you sure you want to continue?`
|
|
|
|
)
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
|
2018-12-10 11:48:59 -05:00
|
|
|
pruneFilters := options.filter.Value().Clone()
|
golangci-lint: enable perfsprint linter
cli/compose/types/types.go:568:17: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return []byte(fmt.Sprintf("%v", e.External)), nil
^
cli/command/formatter/buildcache.go:174:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
return fmt.Sprintf("%d", c.v.UsageCount)
^
cli/command/formatter/buildcache.go:178:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.InUse)
^
cli/command/formatter/buildcache.go:182:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.Shared)
^
cli/command/formatter/image.go:259:9: fmt.Sprintf can be replaced with faster strconv.FormatInt (perfsprint)
return fmt.Sprintf("%d", c.i.Containers)
^
cli/command/formatter/tabwriter/tabwriter_test.go:698:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", x), func(b *testing.B) {
^
cli/command/formatter/tabwriter/tabwriter_test.go:720:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", h), func(b *testing.B) {
^
cli/command/image/prune.go:62:31: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all))
^
cli/command/network/formatter.go:92:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.EnableIPv6)
^
cli/command/network/formatter.go:96:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.Internal)
^
cli/command/service/formatter.go:745:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
pub = fmt.Sprintf("%d", pr.pStart)
^
cli/command/service/formatter.go:750:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
tgt = fmt.Sprintf("%d", pr.tStart)
^
cli/command/service/opts.go:49:10: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
return fmt.Sprintf("%v", *i.value)
^
cli/compose/loader/loader.go:720:36: fmt.Sprint can be replaced with faster strconv.Itoa (perfsprint)
v, err := toServicePortConfigs(fmt.Sprint(value))
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 10:18:19 -05:00
|
|
|
pruneFilters.Add("dangling", strconv.FormatBool(!options.all))
|
2017-02-04 12:10:05 -05:00
|
|
|
pruneFilters = command.PruneFilters(dockerCli, pruneFilters)
|
2016-11-17 00:46:37 -05:00
|
|
|
|
2016-09-22 17:04:34 -04:00
|
|
|
warning := danglingWarning
|
2017-05-15 08:45:19 -04:00
|
|
|
if options.all {
|
2016-09-22 17:04:34 -04:00
|
|
|
warning = allImageWarning
|
|
|
|
}
|
2024-02-21 10:36:17 -05:00
|
|
|
if !options.force {
|
|
|
|
if r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning); !r || err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
report, err := dockerCli.Client().ImagesPrune(ctx, pruneFilters)
|
2016-09-22 17:04:34 -04:00
|
|
|
if err != nil {
|
2017-10-12 11:44:03 -04:00
|
|
|
return 0, "", err
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(report.ImagesDeleted) > 0 {
|
2018-10-08 22:33:27 -04:00
|
|
|
var sb strings.Builder
|
|
|
|
sb.WriteString("Deleted Images:\n")
|
2016-09-22 17:04:34 -04:00
|
|
|
for _, st := range report.ImagesDeleted {
|
|
|
|
if st.Untagged != "" {
|
2018-10-08 22:33:27 -04:00
|
|
|
sb.WriteString("untagged: ")
|
|
|
|
sb.WriteString(st.Untagged)
|
|
|
|
sb.WriteByte('\n')
|
2016-09-22 17:04:34 -04:00
|
|
|
} else {
|
2018-10-08 22:33:27 -04:00
|
|
|
sb.WriteString("deleted: ")
|
|
|
|
sb.WriteString(st.Deleted)
|
|
|
|
sb.WriteByte('\n')
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 22:33:27 -04:00
|
|
|
output = sb.String()
|
2016-09-22 17:04:34 -04:00
|
|
|
spaceReclaimed = report.SpaceReclaimed
|
|
|
|
}
|
|
|
|
|
2017-10-12 11:44:03 -04:00
|
|
|
return spaceReclaimed, output, nil
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// RunPrune calls the Image Prune API
|
2016-09-22 17:04:34 -04:00
|
|
|
// This returns the amount of space reclaimed and a detailed output string
|
2023-09-09 18:27:44 -04:00
|
|
|
func RunPrune(ctx context.Context, dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint64, string, error) {
|
|
|
|
return runPrune(ctx, dockerCli, pruneOptions{force: true, all: all, filter: filter})
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|