Skip to content

Commit 953f11d

Browse files
committed
refactor: centralise "match if nil" logic
1 parent de51f71 commit 953f11d

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

pkg/sif/image.go

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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/update.go

Lines changed: 2 additions & 10 deletions
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)