package main import ( "fmt" "os" "text/tabwriter" "github.com/moby/buildkit/client" bccommon "github.com/moby/buildkit/cmd/buildctl/common" "github.com/tonistiigi/units" "github.com/urfave/cli" ) var pruneCommand = cli.Command{ Name: "prune", Usage: "clean up build cache", Action: prune, Flags: []cli.Flag{ cli.DurationFlag{ Name: "keep-duration", Usage: "Keep data newer than this limit", }, cli.Float64Flag{ Name: "keep-storage", Usage: "Keep data below this limit (in MB)", }, cli.StringSliceFlag{ Name: "filter, f", Usage: "Filter records", }, cli.BoolFlag{ Name: "all", Usage: "Include internal/frontend references", }, cli.BoolFlag{ Name: "verbose, v", Usage: "Verbose output", }, }, } func prune(clicontext *cli.Context) error { c, err := bccommon.ResolveClient(clicontext) if err != nil { return err } ch := make(chan client.UsageInfo) printed := make(chan struct{}) tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0) first := true total := int64(0) go func() { defer close(printed) for du := range ch { total += du.Size if clicontext.Bool("verbose") { printVerbose(tw, []*client.UsageInfo{&du}) } else { if first { printTableHeader(tw) first = false } printTableRow(tw, &du) tw.Flush() } } }() opts := []client.PruneOption{ client.WithFilter(clicontext.StringSlice("filter")), client.WithKeepOpt(clicontext.Duration("keep-duration"), int64(clicontext.Float64("keep-storage")*1e6)), } if clicontext.Bool("all") { opts = append(opts, client.PruneAll) } err = c.Prune(bccommon.CommandContext(clicontext), ch, opts...) close(ch) <-printed if err != nil { return err } tw = tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0) fmt.Fprintf(tw, "Total:\t%.2f\n", units.Bytes(total)) tw.Flush() return nil }