forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskusage.go
116 lines (97 loc) · 2.3 KB
/
diskusage.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"io"
"os"
"text/tabwriter"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/appcontext"
"github.com/tonistiigi/units"
"github.com/urfave/cli"
)
var diskUsageCommand = cli.Command{
Name: "du",
Usage: "disk usage",
Action: diskUsage,
Flags: []cli.Flag{
cli.StringFlag{
Name: "filter, f",
Usage: "Filter snapshot ID",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Verbose output",
},
},
}
func diskUsage(clicontext *cli.Context) error {
c, err := resolveClient(clicontext)
if err != nil {
return err
}
du, err := c.DiskUsage(appcontext.Context(), client.WithFilter(clicontext.String("filter")))
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
if clicontext.Bool("verbose") {
printVerbose(tw, du)
} else {
printTable(tw, du)
}
if clicontext.String("filter") == "" {
printSummary(tw, du)
}
return nil
}
func printKV(w io.Writer, k string, v interface{}) {
fmt.Fprintf(w, "%s:\t%v\n", k, v)
}
func printVerbose(tw *tabwriter.Writer, du []*client.UsageInfo) {
for _, di := range du {
printKV(tw, "ID", di.ID)
if di.Parent != "" {
printKV(tw, "Parent", di.Parent)
}
printKV(tw, "Created at", di.CreatedAt)
printKV(tw, "Mutable", di.Mutable)
printKV(tw, "Reclaimable", !di.InUse)
printKV(tw, "Size", fmt.Sprintf("%.2f", units.Bytes(di.Size)))
if di.Description != "" {
printKV(tw, "Description", di.Description)
}
printKV(tw, "Usage count", di.UsageCount)
if di.LastUsedAt != nil {
printKV(tw, "Last used", di.LastUsedAt)
}
fmt.Fprintf(tw, "\n")
}
tw.Flush()
}
func printTable(tw *tabwriter.Writer, du []*client.UsageInfo) {
fmt.Fprintln(tw, "ID\tRECLAIMABLE\tSIZE\tLAST ACCESSED")
for _, di := range du {
id := di.ID
if di.Mutable {
id += "*"
}
fmt.Fprintf(tw, "%s\t%v\t%.2f\t\n", id, !di.InUse, units.Bytes(di.Size))
}
tw.Flush()
}
func printSummary(tw *tabwriter.Writer, du []*client.UsageInfo) {
total := int64(0)
reclaimable := int64(0)
for _, di := range du {
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, "Reclaimable:\t%.2f\n", units.Bytes(reclaimable))
fmt.Fprintf(tw, "Total:\t%.2f\n", units.Bytes(total))
tw.Flush()
}