forked from remind101/conveyor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
76 lines (62 loc) · 1.7 KB
/
builder.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
package conveyor
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/remind101/conveyor/builder"
"github.com/remind101/pkg/reporter"
"golang.org/x/net/context"
)
// Builder is an implementation of the builder.Builder interface that adds
// timeouts and error reporting.
type Builder struct {
builder builder.Builder
// A Reporter to use to report errors.
Reporter reporter.Reporter
// Timeout controls how long to wait before canceling a build. A timeout
// of 0 means no timeout.
Timeout time.Duration
}
// NewBuilder returns a new Builder instance backed by b. It also wraps it with
// cancellation and closes the logs when the build finishes.
func NewBuilder(b builder.Builder) *Builder {
return &Builder{
builder: builder.CloseWriter(b),
Timeout: DefaultTimeout,
}
}
// Build builds the image.
func (b *Builder) Build(ctx context.Context, w io.Writer, opts builder.BuildOptions) (image string, err error) {
log.Printf("Starting build: repository=%s branch=%s sha=%s",
opts.Repository,
opts.Branch,
opts.Sha,
)
// Embed the reporter in the context.Context.
ctx = reporter.WithReporter(ctx, b.reporter())
if b.Timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, b.Timeout)
defer cancel() // Release resources.
}
reporter.AddContext(ctx, "options", opts)
defer reporter.Monitor(ctx)
defer func() {
if err != nil {
reporter.Report(ctx, err)
}
}()
image, err = b.builder.Build(ctx, w, opts)
return
}
func (b *Builder) reporter() reporter.Reporter {
if b.Reporter == nil {
return reporter.ReporterFunc(func(ctx context.Context, err error) error {
fmt.Fprintf(os.Stderr, "reporting err: %v\n", err)
return nil
})
}
return b.Reporter
}