Skip to content

Commit 78179ff

Browse files
committed
Update recorder to lazy create file.
Currently the resolver command creates the image-refs file on initilization. This causes git to be dirty during builds. This change moves the file creation to be in the recorder itself and on the first time the Publish() method is called. This happens after the build so git is clean. This will mean that any errors on file creation will be reported after the build rather than before. Signed-off-by: Jeff Mendoza <[email protected]>
1 parent 0dcace3 commit 78179ff

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

pkg/commands/resolver.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
266266
}
267267

268268
if po.ImageRefsFile != "" {
269-
f, err := os.OpenFile(po.ImageRefsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
270-
if err != nil {
271-
return nil, err
272-
}
273-
innerPublisher, err = publish.NewRecorder(innerPublisher, f)
269+
innerPublisher, err = publish.NewRecorder(innerPublisher, po.ImageRefsFile)
274270
if err != nil {
275271
return nil, err
276272
}

pkg/publish/recorder.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package publish
1717
import (
1818
"context"
1919
"io"
20+
"os"
2021
"strings"
2122

2223
"github.com/google/go-containerregistry/pkg/name"
@@ -29,19 +30,20 @@ import (
2930
// recorder wraps a publisher implementation in a layer that recordes the published
3031
// references to an io.Writer.
3132
type recorder struct {
32-
inner Interface
33-
wc io.Writer
33+
inner Interface
34+
fileName string
35+
wc io.Writer
3436
}
3537

3638
// recorder implements Interface
3739
var _ Interface = (*recorder)(nil)
3840

3941
// NewRecorder wraps the provided publish.Interface in an implementation that
4042
// records publish results to an io.Writer.
41-
func NewRecorder(inner Interface, wc io.Writer) (Interface, error) {
43+
func NewRecorder(inner Interface, name string) (Interface, error) {
4244
return &recorder{
43-
inner: inner,
44-
wc: wc,
45+
inner: inner,
46+
fileName: name,
4547
}, nil
4648
}
4749

@@ -70,6 +72,14 @@ func (r *recorder) Publish(ctx context.Context, br build.Result, ref string) (na
7072
references = append(references, result.String())
7173
}
7274

75+
if r.wc == nil {
76+
f, err := os.OpenFile(r.fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
77+
if err != nil {
78+
return nil, err
79+
}
80+
r.wc = f
81+
}
82+
7383
if _, err := r.wc.Write([]byte(strings.Join(references, "\n") + "\n")); err != nil {
7484
return nil, err
7585
}

pkg/publish/recorder_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
package publish
1616

1717
import (
18-
"bytes"
1918
"context"
19+
"os"
20+
"path"
2021
"strings"
2122
"testing"
2223

@@ -50,9 +51,10 @@ func TestRecorder(t *testing.T) {
5051
return repo.Context().Digest(h.String()), nil
5152
}}
5253

53-
buf := bytes.NewBuffer(nil)
54+
dir := t.TempDir()
55+
file := path.Join(dir, "testfile")
5456

55-
recorder, err := NewRecorder(inner, buf)
57+
recorder, err := NewRecorder(inner, file)
5658
if err != nil {
5759
t.Fatalf("NewRecorder() = %v", err)
5860
}
@@ -82,7 +84,11 @@ func TestRecorder(t *testing.T) {
8284
t.Errorf("recorder.Close() = %v", err)
8385
}
8486

85-
refs := strings.Split(strings.TrimSpace(buf.String()), "\n")
87+
buf, err := os.ReadFile(file)
88+
if err != nil {
89+
t.Fatalf("os.ReadFile() = %v", err)
90+
}
91+
refs := strings.Split(strings.TrimSpace(string(buf)), "\n")
8692

8793
if want, got := len(refs), 5; got != want {
8894
t.Errorf("len(refs) = %d, wanted %d", got, want)

0 commit comments

Comments
 (0)