Skip to content

Commit

Permalink
pass id to cache
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Jun 17, 2024
1 parent 0fa3442 commit 06aa829
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 11 additions & 11 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down

0 comments on commit 06aa829

Please sign in to comment.