11package filter
22
33import (
4+ "fmt"
5+
46 mmsemver "github.com/Masterminds/semver/v3"
57 bsemver "github.com/blang/semver/v4"
68
@@ -11,59 +13,80 @@ import (
1113)
1214
1315func WithPackageName (packageName string ) Predicate [catalogmetadata.Bundle ] {
14- return func (bundle * catalogmetadata.Bundle ) bool {
15- return bundle .Package == packageName
16+ return func (bundle * catalogmetadata.Bundle ) (bool , []string ) {
17+ value := bundle .Package == packageName
18+ if ! value {
19+ return false , []string {packageName }
20+ }
21+ return value , nil
1622 }
1723}
1824
1925func InMastermindsSemverRange (semverRange * mmsemver.Constraints ) Predicate [catalogmetadata.Bundle ] {
20- return func (bundle * catalogmetadata.Bundle ) bool {
26+ return func (bundle * catalogmetadata.Bundle ) ( bool , [] string ) {
2127 bVersion , err := bundle .Version ()
2228 if err != nil {
23- return false
29+ return false , [] string { err . Error ()}
2430 }
2531 // No error should occur here because the simple version was successfully parsed by blang
2632 // We are unaware of any tests cases that would cause one to fail but not the other
2733 // This will cause code coverage to drop for this line. We don't ignore the error because
2834 // there might be that one extreme edge case that might cause one to fail but not the other
2935 mVersion , err := mmsemver .NewVersion (bVersion .String ())
3036 if err != nil {
31- return false
37+ return false , []string {err .Error ()}
38+ }
39+ res := semverRange .Check (mVersion )
40+ if ! res {
41+ return false , []string {fmt .Sprintf ("no package %q matching version %q found" , bundle .Package , semverRange .String ())}
3242 }
33- return semverRange . Check ( mVersion )
43+ return true , nil
3444 }
3545}
3646
37- func InBlangSemverRange (semverRange bsemver.Range ) Predicate [catalogmetadata.Bundle ] {
38- return func (bundle * catalogmetadata.Bundle ) bool {
47+ func InBlangSemverRange (semverRange bsemver.Range , semverRangeString string ) Predicate [catalogmetadata.Bundle ] {
48+ return func (bundle * catalogmetadata.Bundle ) ( bool , [] string ) {
3949 bundleVersion , err := bundle .Version ()
4050 if err != nil {
41- return false
51+ return false , [] string { err . Error ()}
4252 }
43- return semverRange (* bundleVersion )
53+ if ! semverRange (* bundleVersion ) {
54+ return false , []string {fmt .Sprintf ("no package %q matching version %q found" , bundle .Package , semverRangeString )}
55+ }
56+ return true , nil
4457 }
4558}
4659
4760func InChannel (channelName string ) Predicate [catalogmetadata.Bundle ] {
48- return func (bundle * catalogmetadata.Bundle ) bool {
61+ return func (bundle * catalogmetadata.Bundle ) ( bool , [] string ) {
4962 for _ , ch := range bundle .InChannels {
5063 if ch .Name == channelName {
51- return true
64+ return true , nil
5265 }
5366 }
54- return false
67+ return false , [] string { fmt . Sprintf ( "no package %q found in channel %q" , bundle . Package , channelName )}
5568 }
5669}
5770
5871func WithBundleImage (bundleImage string ) Predicate [catalogmetadata.Bundle ] {
59- return func (bundle * catalogmetadata.Bundle ) bool {
60- return bundle .Image == bundleImage
72+ return func (bundle * catalogmetadata.Bundle ) (bool , []string ) {
73+ res := bundle .Image == bundleImage
74+ if ! res {
75+ return false , []string {fmt .Sprintf ("no matching bundle image %q found for package %s" , bundleImage , bundle .Package )}
76+ } else {
77+ return true , nil
78+ }
6179 }
6280}
6381
6482func WithBundleName (bundleName string ) Predicate [catalogmetadata.Bundle ] {
65- return func (bundle * catalogmetadata.Bundle ) bool {
66- return bundle .Name == bundleName
83+ return func (bundle * catalogmetadata.Bundle ) (bool , []string ) {
84+ res := bundle .Name == bundleName
85+ if ! res {
86+ return false , []string {fmt .Sprintf ("no matching bundle name %q found for package %s" , bundleName , bundle .Package )}
87+ } else {
88+ return true , nil
89+ }
6790 }
6891}
6992
@@ -87,20 +110,25 @@ func LegacySuccessor(installedBundle *ocv1alpha1.BundleMetadata) Predicate[catal
87110 return false
88111 }
89112
90- return func (candidateBundle * catalogmetadata.Bundle ) bool {
113+ return func (candidateBundle * catalogmetadata.Bundle ) ( bool , [] string ) {
91114 for _ , ch := range candidateBundle .InChannels {
92115 for _ , chEntry := range ch .Entries {
93116 if candidateBundle .Name == chEntry .Name && isSuccessor (chEntry ) {
94- return true
117+ return true , nil
95118 }
96119 }
97120 }
98- return false
121+ return false , [] string { fmt . Sprintf ( "no legacy successor found for bundle name %q" , candidateBundle . Name )}
99122 }
100123}
101124
102125func WithDeprecation (deprecated bool ) Predicate [catalogmetadata.Bundle ] {
103- return func (bundle * catalogmetadata.Bundle ) bool {
104- return bundle .HasDeprecation () == deprecated
126+ return func (bundle * catalogmetadata.Bundle ) (bool , []string ) {
127+ res := bundle .HasDeprecation () == deprecated
128+ if ! res {
129+ return false , []string {fmt .Sprintf ("no bundle %q found with desired deprecation status %t" , bundle .Name , deprecated )}
130+ } else {
131+ return true , nil
132+ }
105133 }
106134}
0 commit comments