Skip to content

Commit 9311006

Browse files
authored
Merge pull request #97 from tri-adam/find-manifests
feat: add `FindManifests`
2 parents de51f71 + bba1d78 commit 9311006

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

pkg/sif/image.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ func (f *OCIFileImage) Image(m match.Matcher, _ ...Option) (v1.Image, error) {
3232
return nil, err
3333
}
3434

35-
if m == nil {
36-
m = matchAll
37-
}
38-
39-
matches, err := partial.FindImages(ri, m)
35+
matches, err := partial.FindImages(ri, matchAllIfNil(m))
4036
if err != nil {
4137
return nil, err
4238
}

pkg/sif/index.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ func (f *OCIFileImage) Index(m match.Matcher, _ ...Option) (v1.ImageIndex, error
7676
return nil, err
7777
}
7878

79-
if m == nil {
80-
m = matchAll
81-
}
82-
83-
matches, err := partial.FindIndexes(ri, m)
79+
matches, err := partial.FindIndexes(ri, matchAllIfNil(m))
8480
if err != nil {
8581
return nil, err
8682
}

pkg/sif/manifest.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2024 Sylabs Inc. All rights reserved.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
15
package sif
26

37
import (

pkg/sif/match.go

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99

1010
v1 "github.com/google/go-containerregistry/pkg/v1"
11+
"github.com/google/go-containerregistry/pkg/v1/match"
1112
)
1213

1314
var (
@@ -16,3 +17,11 @@ var (
1617
)
1718

1819
func matchAll(v1.Descriptor) bool { return true }
20+
21+
// matchAllIfNil returns m if not nil, or a Matcher that matches all descriptors otherwise.
22+
func matchAllIfNil(m match.Matcher) match.Matcher {
23+
if m != nil {
24+
return m
25+
}
26+
return matchAll
27+
}

pkg/sif/sif.go

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io"
99

1010
v1 "github.com/google/go-containerregistry/pkg/v1"
11+
"github.com/google/go-containerregistry/pkg/v1/match"
12+
"github.com/google/go-containerregistry/pkg/v1/partial"
1113
"github.com/sylabs/sif/v2/pkg/sif"
1214
)
1315

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

5456
return d.Offset(), nil
5557
}
58+
59+
// FindManifests finds the manifests stored in f that are selected by m. If m is nil, all manifests
60+
// are selected.
61+
func (f *OCIFileImage) FindManifests(m match.Matcher, _ ...Option) ([]v1.Descriptor, error) {
62+
ri, err := f.RootIndex()
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
return partial.FindManifests(ri, matchAllIfNil(m))
68+
}

pkg/sif/update.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,7 @@ func (f *OCIFileImage) RemoveManifests(matcher match.Matcher) error {
471471
return err
472472
}
473473

474-
if matcher == nil {
475-
matcher = matchAll
476-
}
477-
478-
return f.UpdateRootIndex(mutate.RemoveManifests(ri, matcher))
474+
return f.UpdateRootIndex(mutate.RemoveManifests(ri, matchAllIfNil(matcher)))
479475
}
480476

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

508-
if matcher == nil {
509-
matcher = matchAll
510-
}
511-
512504
ri, err := f.RootIndex()
513505
if err != nil {
514506
return err
515507
}
516508

517-
ri = mutate.RemoveManifests(ri, matcher)
509+
ri = mutate.RemoveManifests(ri, matchAllIfNil(matcher))
518510

519511
ri, err = appendToIndex(ri, add, ao)
520512
if err != nil {

0 commit comments

Comments
 (0)