2017-06-08 18:17:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-30 22:54:51 +00:00
|
|
|
"fmt"
|
2017-07-25 19:11:52 +00:00
|
|
|
"io"
|
2017-06-30 22:54:51 +00:00
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
2017-06-08 18:17:44 +00:00
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
2019-03-05 09:33:15 +00:00
|
|
|
bccommon "github.com/moby/buildkit/cmd/buildctl/common"
|
2017-09-25 18:26:01 +00:00
|
|
|
"github.com/tonistiigi/units"
|
2017-06-08 18:17:44 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var diskUsageCommand = cli.Command{
|
|
|
|
Name: "du",
|
|
|
|
Usage: "disk usage",
|
|
|
|
Action: diskUsage,
|
2017-07-25 19:11:52 +00:00
|
|
|
Flags: []cli.Flag{
|
2018-07-26 00:01:37 +00:00
|
|
|
cli.StringSliceFlag{
|
2017-07-25 22:14:46 +00:00
|
|
|
Name: "filter, f",
|
2018-07-26 00:01:37 +00:00
|
|
|
Usage: "Filter records",
|
2017-07-25 22:14:46 +00:00
|
|
|
},
|
2017-07-25 19:11:52 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "verbose, v",
|
|
|
|
Usage: "Verbose output",
|
|
|
|
},
|
|
|
|
},
|
2017-06-08 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func diskUsage(clicontext *cli.Context) error {
|
2019-03-05 09:33:15 +00:00
|
|
|
c, err := bccommon.ResolveClient(clicontext)
|
2017-06-08 18:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 09:33:15 +00:00
|
|
|
du, err := c.DiskUsage(bccommon.CommandContext(clicontext), client.WithFilter(clicontext.StringSlice("filter")))
|
2017-06-08 18:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-30 22:54:51 +00:00
|
|
|
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
|
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
if clicontext.Bool("verbose") {
|
|
|
|
printVerbose(tw, du)
|
|
|
|
} else {
|
|
|
|
printTable(tw, du)
|
|
|
|
}
|
2017-06-30 22:54:51 +00:00
|
|
|
|
2018-07-27 00:53:48 +00:00
|
|
|
if len(clicontext.StringSlice("filter")) == 0 {
|
2017-07-25 22:14:46 +00:00
|
|
|
printSummary(tw, du)
|
|
|
|
}
|
2017-07-25 19:11:52 +00:00
|
|
|
|
|
|
|
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)
|
2018-07-27 00:53:48 +00:00
|
|
|
printKV(tw, "Shared", di.Shared)
|
2017-09-25 18:26:01 +00:00
|
|
|
printKV(tw, "Size", fmt.Sprintf("%.2f", units.Bytes(di.Size)))
|
2017-07-25 19:11:52 +00:00
|
|
|
if di.Description != "" {
|
|
|
|
printKV(tw, "Description", di.Description)
|
|
|
|
}
|
|
|
|
printKV(tw, "Usage count", di.UsageCount)
|
|
|
|
if di.LastUsedAt != nil {
|
|
|
|
printKV(tw, "Last used", di.LastUsedAt)
|
|
|
|
}
|
2018-07-26 19:07:52 +00:00
|
|
|
if di.RecordType != "" {
|
|
|
|
printKV(tw, "Type", di.RecordType)
|
|
|
|
}
|
2017-07-25 19:11:52 +00:00
|
|
|
|
|
|
|
fmt.Fprintf(tw, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTable(tw *tabwriter.Writer, du []*client.UsageInfo) {
|
2017-12-27 01:22:50 +00:00
|
|
|
printTableHeader(tw)
|
2017-06-30 22:54:51 +00:00
|
|
|
|
|
|
|
for _, di := range du {
|
2017-12-27 01:22:50 +00:00
|
|
|
printTableRow(tw, di)
|
2017-07-25 19:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
}
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
func printTableHeader(tw *tabwriter.Writer) {
|
|
|
|
fmt.Fprintln(tw, "ID\tRECLAIMABLE\tSIZE\tLAST ACCESSED")
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTableRow(tw *tabwriter.Writer, di *client.UsageInfo) {
|
|
|
|
id := di.ID
|
|
|
|
if di.Mutable {
|
|
|
|
id += "*"
|
|
|
|
}
|
2018-07-27 00:53:48 +00:00
|
|
|
size := fmt.Sprintf("%.2f", units.Bytes(di.Size))
|
|
|
|
if di.Shared {
|
|
|
|
size += "*"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(tw, "%-71s\t%-11v\t%s\t\n", id, !di.InUse, size)
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
func printSummary(tw *tabwriter.Writer, du []*client.UsageInfo) {
|
|
|
|
total := int64(0)
|
|
|
|
reclaimable := int64(0)
|
2018-07-27 00:53:48 +00:00
|
|
|
shared := int64(0)
|
2017-07-25 19:11:52 +00:00
|
|
|
|
|
|
|
for _, di := range du {
|
2017-06-30 22:54:51 +00:00
|
|
|
if di.Size > 0 {
|
|
|
|
total += di.Size
|
|
|
|
if !di.InUse {
|
|
|
|
reclaimable += di.Size
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 00:53:48 +00:00
|
|
|
if di.Shared {
|
|
|
|
shared += di.Size
|
|
|
|
}
|
2017-06-30 22:54:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 00:53:48 +00:00
|
|
|
if shared > 0 {
|
|
|
|
fmt.Fprintf(tw, "Shared:\t%.2f\n", units.Bytes(shared))
|
|
|
|
fmt.Fprintf(tw, "Private:\t%.2f\n", units.Bytes(total-shared))
|
|
|
|
}
|
|
|
|
|
2017-09-25 18:26:01 +00:00
|
|
|
fmt.Fprintf(tw, "Reclaimable:\t%.2f\n", units.Bytes(reclaimable))
|
|
|
|
fmt.Fprintf(tw, "Total:\t%.2f\n", units.Bytes(total))
|
2017-06-30 22:54:51 +00:00
|
|
|
tw.Flush()
|
2017-06-08 18:17:44 +00:00
|
|
|
}
|