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

pkg/manifests.list: add SetMediaType(), SetArtifactType() #1817

Merged
merged 1 commit into from
Jan 24, 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
42 changes: 42 additions & 0 deletions pkg/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type List interface {
Features(instanceDigest digest.Digest) ([]string, error)
SetOSFeatures(instanceDigest digest.Digest, osFeatures []string) error
OSFeatures(instanceDigest digest.Digest) ([]string, error)
SetMediaType(instanceDigest digest.Digest, mediaType string) error
MediaType(instanceDigest digest.Digest) (string, error)
SetArtifactType(instanceDigest digest.Digest, artifactType string) error
ArtifactType(instanceDigest digest.Digest) (string, error)
Serialize(mimeType string) ([]byte, error)
Instances() []digest.Digest
OCIv1() *v1.Index
Expand Down Expand Up @@ -357,6 +361,44 @@ func (l *list) OSFeatures(instanceDigest digest.Digest) ([]string, error) {
return append([]string{}, oci.Platform.OSFeatures...), nil
}

// SetMediaType sets the MediaType field in the instance with the specified digest.
func (l *list) SetMediaType(instanceDigest digest.Digest, mediaType string) error {
oci, err := l.findOCIv1(instanceDigest)
if err != nil {
return err
}
oci.MediaType = mediaType
return nil
}

// MediaType retrieves the MediaType field in the instance with the specified digest.
func (l *list) MediaType(instanceDigest digest.Digest) (string, error) {
oci, err := l.findOCIv1(instanceDigest)
if err != nil {
return "", err
}
return oci.MediaType, nil
}

// SetArtifactType sets the ArtifactType field in the instance with the specified digest.
func (l *list) SetArtifactType(instanceDigest digest.Digest, artifactType string) error {
oci, err := l.findOCIv1(instanceDigest)
if err != nil {
return err
}
oci.ArtifactType = artifactType
return nil
}

// ArtifactType retrieves the ArtifactType field in the instance with the specified digest.
func (l *list) ArtifactType(instanceDigest digest.Digest) (string, error) {
oci, err := l.findOCIv1(instanceDigest)
if err != nil {
return "", err
}
return oci.ArtifactType, nil
}

// FromBlob builds a list from an encoded manifest list or image index.
func FromBlob(manifestBytes []byte) (List, error) {
manifestType := manifest.GuessMIMEType(manifestBytes)
Expand Down
24 changes: 24 additions & 0 deletions pkg/manifests/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,27 @@ func TestSerialize(t *testing.T) {
}
}
}

func TestMediaType(t *testing.T) {
testString(t,
[]string{v1.MediaTypeImageManifest, manifest.DockerV2Schema2MediaType},
func(l List, i digest.Digest, s string) error {
return l.SetMediaType(i, s)
},
func(l List, i digest.Digest) (string, error) {
return l.MediaType(i)
},
)
}

func TestArtifactType(t *testing.T) {
testString(t,
[]string{"text/plain", "application/octet-stream"},
func(l List, i digest.Digest, s string) error {
return l.SetArtifactType(i, s)
},
func(l List, i digest.Digest) (string, error) {
return l.ArtifactType(i)
},
)
}