2018-04-19 13:07:27 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UsageInfo struct {
|
|
|
|
ID string
|
|
|
|
Mutable bool
|
|
|
|
InUse bool
|
|
|
|
Size int64
|
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
LastUsedAt *time.Time
|
|
|
|
UsageCount int
|
|
|
|
Parent string
|
|
|
|
Description string
|
2018-08-11 15:04:13 -04:00
|
|
|
RecordType UsageRecordType
|
|
|
|
Shared bool
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*UsageInfo, error) {
|
|
|
|
info := &DiskUsageInfo{}
|
|
|
|
for _, o := range opts {
|
2018-08-11 15:04:13 -04:00
|
|
|
o.SetDiskUsageOption(info)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &controlapi.DiskUsageRequest{Filter: info.Filter}
|
|
|
|
resp, err := c.controlClient().DiskUsage(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to call diskusage")
|
|
|
|
}
|
|
|
|
|
|
|
|
var du []*UsageInfo
|
|
|
|
|
|
|
|
for _, d := range resp.Record {
|
|
|
|
du = append(du, &UsageInfo{
|
|
|
|
ID: d.ID,
|
|
|
|
Mutable: d.Mutable,
|
|
|
|
InUse: d.InUse,
|
|
|
|
Size: d.Size_,
|
|
|
|
Parent: d.Parent,
|
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
|
Description: d.Description,
|
|
|
|
UsageCount: int(d.UsageCount),
|
|
|
|
LastUsedAt: d.LastUsedAt,
|
2018-08-11 15:04:13 -04:00
|
|
|
RecordType: UsageRecordType(d.RecordType),
|
|
|
|
Shared: d.Shared,
|
2018-04-19 13:07:27 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(du, func(i, j int) bool {
|
|
|
|
if du[i].Size == du[j].Size {
|
|
|
|
return du[i].ID > du[j].ID
|
|
|
|
}
|
|
|
|
return du[i].Size > du[j].Size
|
|
|
|
})
|
|
|
|
|
|
|
|
return du, nil
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
type DiskUsageOption interface {
|
|
|
|
SetDiskUsageOption(*DiskUsageInfo)
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
|
|
|
|
type DiskUsageInfo struct {
|
2018-08-11 15:04:13 -04:00
|
|
|
Filter []string
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
type UsageRecordType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
UsageRecordTypeInternal UsageRecordType = "internal"
|
|
|
|
UsageRecordTypeFrontend UsageRecordType = "frontend"
|
|
|
|
UsageRecordTypeLocalSource UsageRecordType = "source.local"
|
|
|
|
UsageRecordTypeGitCheckout UsageRecordType = "source.git.checkout"
|
|
|
|
UsageRecordTypeCacheMount UsageRecordType = "exec.cachemount"
|
|
|
|
UsageRecordTypeRegular UsageRecordType = "regular"
|
|
|
|
)
|