2017-12-27 01:22:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/client"
|
2019-03-05 09:33:15 +00:00
|
|
|
bccommon "github.com/moby/buildkit/cmd/buildctl/common"
|
2017-12-27 01:22:50 +00:00
|
|
|
"github.com/tonistiigi/units"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pruneCommand = cli.Command{
|
|
|
|
Name: "prune",
|
|
|
|
Usage: "clean up build cache",
|
|
|
|
Action: prune,
|
|
|
|
Flags: []cli.Flag{
|
2018-07-31 20:14:53 +00:00
|
|
|
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)",
|
|
|
|
},
|
2018-07-26 00:20:03 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "filter, f",
|
|
|
|
Usage: "Filter records",
|
|
|
|
},
|
2018-07-26 22:31:35 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "all",
|
|
|
|
Usage: "Include internal/frontend references",
|
|
|
|
},
|
2017-12-27 01:22:50 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "verbose, v",
|
|
|
|
Usage: "Verbose output",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func prune(clicontext *cli.Context) error {
|
2019-03-05 09:33:15 +00:00
|
|
|
c, err := bccommon.ResolveClient(clicontext)
|
2017-12-27 01:22:50 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
opts := []client.PruneOption{
|
|
|
|
client.WithFilter(clicontext.StringSlice("filter")),
|
|
|
|
client.WithKeepOpt(clicontext.Duration("keep-duration"), int64(clicontext.Float64("keep-storage")*1e6)),
|
|
|
|
}
|
2018-07-26 22:31:35 +00:00
|
|
|
|
|
|
|
if clicontext.Bool("all") {
|
|
|
|
opts = append(opts, client.PruneAll)
|
|
|
|
}
|
|
|
|
|
2019-03-05 09:33:15 +00:00
|
|
|
err = c.Prune(bccommon.CommandContext(clicontext), ch, opts...)
|
2017-12-27 01:22:50 +00:00
|
|
|
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
|
|
|
|
}
|