Skip to content

Commit

Permalink
feat: adds media type detection when loading OCI descriptors
Browse files Browse the repository at this point in the history
Closes #14

Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
  • Loading branch information
jpower432 committed May 24, 2022
1 parent b5368d6 commit 0201991
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (o *BuildOptions) Run(ctx context.Context) error {
return err
}

descs, err := client.GatherDescriptors(files...)
descs, err := client.GatherDescriptors("", files...)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions registryclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// Client defines methods to publish content as artifacts
type Client interface {
// GatherDescriptors loads files to create OCI descriptors.
GatherDescriptors(...string) ([]ocispec.Descriptor, error)
// GatherDescriptors loads files to create OCI descriptors with a specific
// media type.
GatherDescriptors(string, ...string) ([]ocispec.Descriptor, error)
// GenerateConfig creates and stores a config.
// The config descriptor is returned for manifest generation.
GenerateConfig(map[string]string) (ocispec.Descriptor, error)
Expand Down
16 changes: 0 additions & 16 deletions registryclient/orasclient/file_unix.go

This file was deleted.

37 changes: 31 additions & 6 deletions registryclient/orasclient/oras.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"fmt"
"path/filepath"

"github.com/gabriel-vasile/mimetype"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/pkg/content"
"oras.land/oras-go/pkg/oras"

"github.com/uor-framework/client/registryclient"
)

const uorMediaType = "application/vnd.uor.config.v1+json"

type orasClient struct {
registryOpts content.RegistryOptions
copyOpts []oras.CopyOpt
Expand All @@ -22,9 +25,9 @@ type orasClient struct {
var _ registryclient.Client = &orasClient{}

// GatherDescriptors loads files to create OCI descriptors.
func (c *orasClient) GatherDescriptors(files ...string) ([]ocispec.Descriptor, error) {
func (c *orasClient) GatherDescriptors(mediaType string, files ...string) ([]ocispec.Descriptor, error) {
fromFile := content.NewFile("")
descs, err := loadFiles(fromFile, files...)
descs, err := loadFiles(fromFile, mediaType, files...)
if err != nil {
return nil, fmt.Errorf("unable to load files: %w", err)
}
Expand All @@ -39,6 +42,7 @@ func (c *orasClient) GenerateConfig(configAnnotations map[string]string) (ocispe
if err != nil {
return configDesc, fmt.Errorf("unable to create new manifest config: %w", err)
}
configDesc.MediaType = uorMediaType
if err := c.fileStore.Load(configDesc, config); err != nil {
return configDesc, fmt.Errorf("unable to load new manifest config: %w", err)
}
Expand Down Expand Up @@ -70,20 +74,41 @@ func (c *orasClient) Execute(ctx context.Context) error {
return nil
}

func loadFiles(store *content.File, files ...string) ([]ocispec.Descriptor, error) {
func loadFiles(store *content.File, mediaType string, files ...string) ([]ocispec.Descriptor, error) {
var descs []ocispec.Descriptor
var skipMediaTypeDetection bool
var err error

if mediaType != "" {
skipMediaTypeDetection = true
}
for _, fileRef := range files {
filename, mediaType := parseFileRef(fileRef, "")
name := filepath.Clean(filename)
name := filepath.Clean(fileRef)
if !filepath.IsAbs(name) {
// convert to slash-separated path unless it is absolute path
name = filepath.ToSlash(name)
}
desc, err := store.Add(name, mediaType, filename)

if !skipMediaTypeDetection {
mediaType, err = getDefaultMediaType(fileRef)
if err != nil {
return nil, fmt.Errorf("file %q: error dectecting media type: %v", name, err)
}
}

desc, err := store.Add(name, mediaType, fileRef)
if err != nil {
return nil, err
}
descs = append(descs, desc)
}
return descs, nil
}

func getDefaultMediaType(file string) (string, error) {
mType, err := mimetype.DetectFile(file)
if err != nil {
return "", err
}
return mType.String(), nil
}

0 comments on commit 0201991

Please sign in to comment.