-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
prunehistories.go
118 lines (111 loc) · 2.82 KB
/
prunehistories.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
117
118
package main
import (
"context"
"fmt"
"io"
"text/tabwriter"
"time"
"github.com/hashicorp/go-multierror"
controlapi "github.com/moby/buildkit/api/services/control"
bccommon "github.com/moby/buildkit/cmd/buildctl/common"
"github.com/moby/buildkit/util/appcontext"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var pruneHistoriesCommand = cli.Command{
Name: "prune-histories",
Usage: "clean up build histories",
Action: pruneHistories,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "Format the output using the given Go template, e.g, '{{json .}}'",
},
},
}
func pruneHistories(clicontext *cli.Context) error {
c, err := bccommon.ResolveClient(clicontext)
if err != nil {
return err
}
ctx := appcontext.Context()
controlClient := c.ControlClient()
resp, err := controlClient.ListenBuildHistory(ctx, &controlapi.BuildHistoryRequest{
EarlyExit: true,
})
if err != nil {
return err
}
var rerr error
if format := clicontext.String("format"); format != "" {
tmpl, err := bccommon.ParseTemplate(format)
if err != nil {
return err
}
for {
ev, err := resp.Recv()
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return err
}
if ev.Record == nil || ev.Record.Pinned {
continue
}
if _, err := controlClient.UpdateBuildHistory(ctx, &controlapi.UpdateBuildHistoryRequest{
Ref: ev.Record.Ref,
Delete: true,
}); err != nil {
rerr = multierror.Append(rerr, err).ErrorOrNil()
continue
}
if err := tmpl.Execute(clicontext.App.Writer, ev); err != nil {
return err
}
if _, err = fmt.Fprintf(clicontext.App.Writer, "\n"); err != nil {
return err
}
}
return rerr
}
return pruneHistoriesWithTableOutput(ctx, clicontext.App.Writer, controlClient, resp)
}
func pruneHistoriesWithTableOutput(ctx context.Context, w io.Writer, controlClient controlapi.ControlClient,
eventReceiver controlapi.Control_ListenBuildHistoryClient) error {
tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0)
fmt.Fprintln(tw, "TYPE\tREF\tCREATED\tCOMPLETED\tGENERATION")
var rerr error
for {
ev, err := eventReceiver.Recv()
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return err
}
r := ev.Record
if r == nil || r.Pinned {
continue
}
if _, err := controlClient.UpdateBuildHistory(ctx, &controlapi.UpdateBuildHistoryRequest{
Ref: ev.Record.Ref,
Delete: true,
}); err != nil {
rerr = multierror.Append(rerr, err).ErrorOrNil()
continue
}
var (
createdAt string
completedAt string
)
if r.CreatedAt != nil {
createdAt = r.CreatedAt.Local().Format(time.RFC3339)
}
if r.CompletedAt != nil {
completedAt = r.CompletedAt.Local().Format(time.RFC3339)
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%d\n", ev.Type, r.Ref, createdAt, completedAt, r.Generation)
tw.Flush()
}
tw.Flush()
return rerr
}