From 06aa829f95d12ba5f91283e80ab2b05102554400 Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Mon, 17 Jun 2024 09:23:48 +0200 Subject: [PATCH] pass id to cache Signed-off-by: Pablo Chacin --- cache.go | 8 ++++---- cache_test.go | 2 +- service.go | 22 +++++++++++----------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/cache.go b/cache.go index f06d3ab..adc1ad8 100644 --- a/cache.go +++ b/cache.go @@ -22,7 +22,7 @@ type Object struct { // Cache defines an interface for storing blobs type Cache interface { // Store stores the object and returns the metadata - Store(ctx context.Context, content io.Reader) (Object, error) + Store(ctx context.Context, id string, content io.Reader) (Object, error) } // an ObjectStore backed by a file @@ -55,8 +55,8 @@ func NewFileCache(path string) (Cache, error) { }, nil } -func (f *fileObjectStore) Store(ctx context.Context, content io.Reader) (Object, error) { - objectFile, err := os.CreateTemp(f.path, "*") +func (f *fileObjectStore) Store(ctx context.Context, id string, content io.Reader) (Object, error) { + objectFile, err := os.Create(filepath.Join(f.path, id)) if err != nil { return Object{}, fmt.Errorf("creating object %w", err) } @@ -74,7 +74,7 @@ func (f *fileObjectStore) Store(ctx context.Context, content io.Reader) (Object, checksum.Sum(buff.Bytes()) return Object{ - ID: filepath.Base(objectFile.Name()), + ID: id, Checksum: string(fmt.Sprintf("%x", checksum.Sum(nil))), URL: fmt.Sprintf("file://%s", objectFile.Name()), }, nil diff --git a/cache_test.go b/cache_test.go index 1463b3c..be11bbf 100644 --- a/cache_test.go +++ b/cache_test.go @@ -36,7 +36,7 @@ func TestFileCache(t *testing.T) { t.Fatalf("test setup %v", err) } - obj, err := cache.Store(context.TODO(), bytes.NewBuffer(tc.content)) + obj, err := cache.Store(context.TODO(), "object", bytes.NewBuffer(tc.content)) if !errors.Is(err, tc.expectErr) { t.Fatalf("expected %v got %v", tc, err) } diff --git a/service.go b/service.go index ed4429b..10eb6bf 100644 --- a/service.go +++ b/service.go @@ -118,17 +118,6 @@ func (b *buildsrv) Build(ctx context.Context, platform string, k6Constrains stri resolved[d.Name] = m.Version } - artifactBuffer := &bytes.Buffer{} - err = b.builder.Build(ctx, buildPlatform, k6Mod.Version, mods, []string{}, artifactBuffer) - if err != nil { - return Artifact{}, fmt.Errorf("building artifact %w", err) - } - - artifactObject, err := b.cache.Store(ctx, artifactBuffer) - if err != nil { - return Artifact{}, fmt.Errorf("creating object %w", err) - } - sorted := maps.Keys(resolved) sort.Strings(sorted) @@ -140,6 +129,17 @@ func (b *buildsrv) Build(ctx context.Context, platform string, k6Constrains stri } id := fmt.Sprintf("%x", hash.Sum(nil)) + artifactBuffer := &bytes.Buffer{} + err = b.builder.Build(ctx, buildPlatform, k6Mod.Version, mods, []string{}, artifactBuffer) + if err != nil { + return Artifact{}, fmt.Errorf("building artifact %w", err) + } + + artifactObject, err := b.cache.Store(ctx, id, artifactBuffer) + if err != nil { + return Artifact{}, fmt.Errorf("creating object %w", err) + } + return Artifact{ ID: id, Checksum: artifactObject.Checksum,