1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
package main
import (
"context"
"fmt"
buildahcli "github.com/containers/buildah/pkg/cli"
"github.com/containers/buildah/pkg/parse"
"github.com/containers/buildah/pkg/volumes"
"github.com/containers/common/libimage"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
)
type pruneOptions struct {
force bool
all bool
}
func init() {
var (
pruneDescription = `
Cleanup intermediate images as well as build and mount cache.`
opts pruneOptions
)
pruneCommand := &cobra.Command{
Use: "prune",
Short: "Cleanup intermediate images as well as build and mount cache",
Long: pruneDescription,
RunE: func(cmd *cobra.Command, args []string) error {
return pruneCmd(cmd, args, opts)
},
Example: `buildah prune
buildah prune --force`,
}
pruneCommand.SetUsageTemplate(UsageTemplate())
flags := pruneCommand.Flags()
flags.SetInterspersed(false)
flags.BoolVarP(&opts.all, "all", "a", false, "remove all unused images")
flags.BoolVarP(&opts.force, "force", "f", false, "force removal of the image and any containers using the image")
rootCmd.AddCommand(pruneCommand)
}
func pruneCmd(c *cobra.Command, args []string, iopts pruneOptions) error {
if err := buildahcli.VerifyFlagsArgsOrder(args); err != nil {
return err
}
store, err := getStore(c)
if err != nil {
return err
}
systemContext, err := parse.SystemContextFromOptions(c)
if err != nil {
return err
}
runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext})
if err != nil {
return err
}
err = volumes.CleanCacheMount()
if err != nil {
return err
}
options := &libimage.RemoveImagesOptions{
Filters: []string{"readonly=false"},
}
if !iopts.all {
options.Filters = append(options.Filters, "dangling=true")
options.Filters = append(options.Filters, "intermediate=true")
}
options.Force = iopts.force
rmiReports, rmiErrors := runtime.RemoveImages(context.Background(), args, options)
for _, r := range rmiReports {
for _, u := range r.Untagged {
fmt.Printf("untagged: %s\n", u)
}
}
for _, r := range rmiReports {
if r.Removed {
fmt.Printf("%s\n", r.ID)
}
}
var multiE *multierror.Error
multiE = multierror.Append(multiE, rmiErrors...)
return multiE.ErrorOrNil()
}
|