Skip to content

Commit

Permalink
Merge pull request #97 from tri-adam/find-manifests
Browse files Browse the repository at this point in the history
feat: add `FindManifests`
  • Loading branch information
tri-adam authored Oct 15, 2024
2 parents de51f71 + bba1d78 commit 9311006
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
6 changes: 1 addition & 5 deletions pkg/sif/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ func (f *OCIFileImage) Image(m match.Matcher, _ ...Option) (v1.Image, error) {
return nil, err
}

if m == nil {
m = matchAll
}

matches, err := partial.FindImages(ri, m)
matches, err := partial.FindImages(ri, matchAllIfNil(m))
if err != nil {
return nil, err
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/sif/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ func (f *OCIFileImage) Index(m match.Matcher, _ ...Option) (v1.ImageIndex, error
return nil, err
}

if m == nil {
m = matchAll
}

matches, err := partial.FindIndexes(ri, m)
matches, err := partial.FindIndexes(ri, matchAllIfNil(m))
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sif/manifest.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Sylabs Inc. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0

package sif

import (
Expand Down
9 changes: 9 additions & 0 deletions pkg/sif/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/match"
)

var (
Expand All @@ -16,3 +17,11 @@ var (
)

func matchAll(v1.Descriptor) bool { return true }

// matchAllIfNil returns m if not nil, or a Matcher that matches all descriptors otherwise.
func matchAllIfNil(m match.Matcher) match.Matcher {
if m != nil {
return m
}
return matchAll
}
13 changes: 13 additions & 0 deletions pkg/sif/sif.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/match"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/sylabs/sif/v2/pkg/sif"
)

Expand Down Expand Up @@ -53,3 +55,14 @@ func (f *OCIFileImage) Offset(h v1.Hash) (int64, error) {

return d.Offset(), nil
}

// FindManifests finds the manifests stored in f that are selected by m. If m is nil, all manifests
// are selected.
func (f *OCIFileImage) FindManifests(m match.Matcher, _ ...Option) ([]v1.Descriptor, error) {
ri, err := f.RootIndex()
if err != nil {
return nil, err
}

return partial.FindManifests(ri, matchAllIfNil(m))
}
12 changes: 2 additions & 10 deletions pkg/sif/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,7 @@ func (f *OCIFileImage) RemoveManifests(matcher match.Matcher) error {
return err
}

if matcher == nil {
matcher = matchAll
}

return f.UpdateRootIndex(mutate.RemoveManifests(ri, matcher))
return f.UpdateRootIndex(mutate.RemoveManifests(ri, matchAllIfNil(matcher)))
}

// ReplaceImage writes img to the SIF, replacing any existing manifest that is
Expand Down Expand Up @@ -505,16 +501,12 @@ func (f *OCIFileImage) replace(add mutate.Appendable, matcher match.Matcher, opt
}
}

if matcher == nil {
matcher = matchAll
}

ri, err := f.RootIndex()
if err != nil {
return err
}

ri = mutate.RemoveManifests(ri, matcher)
ri = mutate.RemoveManifests(ri, matchAllIfNil(matcher))

ri, err = appendToIndex(ri, add, ao)
if err != nil {
Expand Down

0 comments on commit 9311006

Please sign in to comment.