Skip to content

Commit

Permalink
fix: export no match / multiple match errors
Browse files Browse the repository at this point in the history
`OCIFileImage.Image` or `OCIFileImage.Index` can fail due to no
descriptors, or multiple descriptors, being selected by the provided
matcher. The functions return a single image or index only.

These no match / multiple match error cases may require specific action
in the caller. For example, when looking for an artifact associated with
a container image, which may or may not be present.

Export `ErrNoMatch` and `ErrMultipleMatches`, and use these two errors
across the Image and Index functions.

Fixes #94
  • Loading branch information
dtrudg committed Oct 7, 2024
1 parent aa3f634 commit 3b0bc23
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
8 changes: 4 additions & 4 deletions pkg/sif/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type image struct {
}

var (
errNoMatchingImage = errors.New("no image found matching criteria")
errMultipleMatchingImages = errors.New("multiple images match criteria")
ErrNoMatch = errors.New("no match found")
ErrMultipleMatches = errors.New("multiple matches found")
)

// Image returns a single Image stored in f, that is selected by the provided
Expand All @@ -42,10 +42,10 @@ func (f *OCIFileImage) Image(m match.Matcher, _ ...Option) (v1.Image, error) {
return nil, err
}
if len(matches) > 1 {
return nil, errMultipleMatchingImages
return nil, ErrMultipleMatches
}
if len(matches) == 0 {
return nil, errNoMatchingImage
return nil, ErrNoMatch
}

d, err := matches[0].Digest()
Expand Down
9 changes: 2 additions & 7 deletions pkg/sif/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ func (f *OCIFileImage) RootIndex() (v1.ImageIndex, error) {
}, nil
}

var (
errNoMatchingIndex = errors.New("no index found matching criteria")
errMultipleMatchingIndices = errors.New("multiple indices match criteria")
)

// Index returns a single ImageIndex stored in f, that is selected by the provided
// Matcher. If more than one index matches, or no index matches, an error is
// returned.
Expand All @@ -86,10 +81,10 @@ func (f *OCIFileImage) Index(m match.Matcher, _ ...Option) (v1.ImageIndex, error
return nil, err
}
if len(matches) > 1 {
return nil, errMultipleMatchingIndices
return nil, ErrMultipleMatches
}
if len(matches) == 0 {
return nil, errNoMatchingIndex
return nil, ErrNoMatch
}

d, err := matches[0].Digest()
Expand Down

0 comments on commit 3b0bc23

Please sign in to comment.