forked from moby/buildkit
/
prune.go
92 lines (80 loc) · 1.76 KB
/
prune.go
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
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/moby/buildkit/client"
"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 := 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(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
}