Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update recorder to lazy create file. #1379

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
}

if po.ImageRefsFile != "" {
f, err := os.OpenFile(po.ImageRefsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
innerPublisher, err = publish.NewRecorder(innerPublisher, f)
innerPublisher, err = publish.NewRecorder(innerPublisher, po.ImageRefsFile)
if err != nil {
return nil, err
}
Expand Down
24 changes: 17 additions & 7 deletions pkg/publish/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package publish
import (
"context"
"io"
"os"
"strings"

"github.com/google/go-containerregistry/pkg/name"
Expand All @@ -27,21 +28,22 @@ import (
)

// recorder wraps a publisher implementation in a layer that recordes the published
// references to an io.Writer.
// references to a file.
type recorder struct {
inner Interface
wc io.Writer
inner Interface
fileName string
wc io.Writer
}

// recorder implements Interface
var _ Interface = (*recorder)(nil)

// NewRecorder wraps the provided publish.Interface in an implementation that
// records publish results to an io.Writer.
func NewRecorder(inner Interface, wc io.Writer) (Interface, error) {
// records publish results to a file.
func NewRecorder(inner Interface, name string) (Interface, error) {
return &recorder{
inner: inner,
wc: wc,
inner: inner,
fileName: name,
}, nil
}

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

if r.wc == nil {
f, err := os.OpenFile(r.fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
r.wc = f
}

if _, err := r.wc.Write([]byte(strings.Join(references, "\n") + "\n")); err != nil {
return nil, err
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/publish/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package publish

import (
"bytes"
"context"
"os"
"path"
"strings"
"testing"

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

buf := bytes.NewBuffer(nil)
dir := t.TempDir()
file := path.Join(dir, "testfile")

recorder, err := NewRecorder(inner, buf)
recorder, err := NewRecorder(inner, file)
if err != nil {
t.Fatalf("NewRecorder() = %v", err)
}
Expand Down Expand Up @@ -82,7 +84,11 @@ func TestRecorder(t *testing.T) {
t.Errorf("recorder.Close() = %v", err)
}

refs := strings.Split(strings.TrimSpace(buf.String()), "\n")
buf, err := os.ReadFile(file)
if err != nil {
t.Fatalf("os.ReadFile() = %v", err)
}
refs := strings.Split(strings.TrimSpace(string(buf)), "\n")

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