2017-12-27 01:22:50 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2018-07-31 20:14:53 +00:00
|
|
|
"time"
|
2017-12-27 01:22:50 +00:00
|
|
|
|
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) Prune(ctx context.Context, ch chan UsageInfo, opts ...PruneOption) error {
|
|
|
|
info := &PruneInfo{}
|
|
|
|
for _, o := range opts {
|
2018-07-26 00:20:03 +00:00
|
|
|
o.SetPruneOption(info)
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
req := &controlapi.PruneRequest{
|
|
|
|
Filter: info.Filter,
|
|
|
|
KeepDuration: int64(info.KeepDuration),
|
|
|
|
KeepBytes: int64(info.KeepBytes),
|
|
|
|
}
|
2018-07-26 22:31:35 +00:00
|
|
|
if info.All {
|
|
|
|
req.All = true
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
cl, err := c.controlClient().Prune(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to call prune")
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
d, err := cl.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ch != nil {
|
|
|
|
ch <- UsageInfo{
|
|
|
|
ID: d.ID,
|
|
|
|
Mutable: d.Mutable,
|
|
|
|
InUse: d.InUse,
|
|
|
|
Size: d.Size_,
|
2021-08-03 01:57:39 +00:00
|
|
|
Parents: d.Parents,
|
2017-12-27 01:22:50 +00:00
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
|
Description: d.Description,
|
|
|
|
UsageCount: int(d.UsageCount),
|
|
|
|
LastUsedAt: d.LastUsedAt,
|
2018-07-27 00:53:48 +00:00
|
|
|
RecordType: UsageRecordType(d.RecordType),
|
|
|
|
Shared: d.Shared,
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:20:03 +00:00
|
|
|
type PruneOption interface {
|
|
|
|
SetPruneOption(*PruneInfo)
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
|
|
|
|
type PruneInfo struct {
|
2018-07-31 20:14:53 +00:00
|
|
|
Filter []string
|
|
|
|
All bool
|
|
|
|
KeepDuration time.Duration
|
|
|
|
KeepBytes int64
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
2018-07-26 22:31:35 +00:00
|
|
|
|
|
|
|
type pruneOptionFunc func(*PruneInfo)
|
|
|
|
|
|
|
|
func (f pruneOptionFunc) SetPruneOption(pi *PruneInfo) {
|
|
|
|
f(pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
var PruneAll = pruneOptionFunc(func(pi *PruneInfo) {
|
|
|
|
pi.All = true
|
|
|
|
})
|
2018-07-31 20:14:53 +00:00
|
|
|
|
|
|
|
func WithKeepOpt(duration time.Duration, bytes int64) PruneOption {
|
|
|
|
return pruneOptionFunc(func(pi *PruneInfo) {
|
|
|
|
pi.KeepDuration = duration
|
|
|
|
pi.KeepBytes = bytes
|
|
|
|
})
|
|
|
|
}
|