2018-04-19 13:07:27 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2018-08-11 15:04:13 -04:00
|
|
|
"time"
|
2018-04-19 13:07:27 -04: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-08-11 15:04:13 -04:00
|
|
|
o.SetPruneOption(info)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
req := &controlapi.PruneRequest{
|
|
|
|
Filter: info.Filter,
|
|
|
|
KeepDuration: int64(info.KeepDuration),
|
|
|
|
KeepBytes: int64(info.KeepBytes),
|
|
|
|
}
|
|
|
|
if info.All {
|
|
|
|
req.All = true
|
|
|
|
}
|
2018-04-19 13:07:27 -04: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_,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
type PruneOption interface {
|
|
|
|
SetPruneOption(*PruneInfo)
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
|
|
|
|
type PruneInfo struct {
|
2018-08-11 15:04:13 -04:00
|
|
|
Filter []string
|
|
|
|
All bool
|
|
|
|
KeepDuration time.Duration
|
|
|
|
KeepBytes int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type pruneOptionFunc func(*PruneInfo)
|
|
|
|
|
|
|
|
func (f pruneOptionFunc) SetPruneOption(pi *PruneInfo) {
|
|
|
|
f(pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
var PruneAll = pruneOptionFunc(func(pi *PruneInfo) {
|
|
|
|
pi.All = true
|
|
|
|
})
|
|
|
|
|
|
|
|
func WithKeepOpt(duration time.Duration, bytes int64) PruneOption {
|
|
|
|
return pruneOptionFunc(func(pi *PruneInfo) {
|
|
|
|
pi.KeepDuration = duration
|
|
|
|
pi.KeepBytes = bytes
|
|
|
|
})
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|