forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune.go
89 lines (71 loc) · 1.93 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
package main
import (
"context"
"flag"
"fmt"
"os"
"text/tabwriter"
"github.com/containerd/containerd/namespaces"
units "github.com/docker/go-units"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
)
const pruneHelp = `Prune and clean up the build cache.`
func (cmd *pruneCommand) Name() string { return "prune" }
func (cmd *pruneCommand) Args() string { return "[OPTIONS]" }
func (cmd *pruneCommand) ShortHelp() string { return pruneHelp }
func (cmd *pruneCommand) LongHelp() string { return pruneHelp }
func (cmd *pruneCommand) Hidden() bool { return false }
func (cmd *pruneCommand) Register(fs *flag.FlagSet) {}
type pruneCommand struct{}
func (cmd *pruneCommand) Run(ctx context.Context, args []string) (err error) {
reexec()
// Create the context.
id := identity.NewID()
ctx = session.NewContext(ctx, id)
ctx = namespaces.WithNamespace(ctx, "buildkit")
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
usage, err := c.Prune(ctx)
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
if debug {
printDebug(tw, usage)
} else {
fmt.Fprintln(tw, "ID\tRECLAIMABLE\tSIZE\tDESCRIPTION")
for _, di := range usage {
id := di.ID
if di.Mutable {
id += "*"
}
desc := di.Description
if len(desc) > 50 {
desc = desc[0:50] + "..."
}
fmt.Fprintf(tw, "%s\t%t\t%s\t%s\n", id, !di.InUse, units.BytesSize(float64(di.Size_)), desc)
}
tw.Flush()
}
total := int64(0)
reclaimable := int64(0)
for _, di := range usage {
if di.Size_ > 0 {
total += di.Size_
if !di.InUse {
reclaimable += di.Size_
}
}
}
tw = tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
fmt.Fprintf(tw, "Reclaimed:\t%s\n", units.BytesSize(float64(reclaimable)))
fmt.Fprintf(tw, "Total:\t%s\n", units.BytesSize(float64(total)))
tw.Flush()
return nil
}