Skip to content

Commit b93cd31

Browse files
Merge pull request #519 from ackama:refactor/remove-always-nil-errors
PiperOrigin-RevId: 736348246
2 parents a629d30 + 1c994fd commit b93cd31

File tree

12 files changed

+49
-104
lines changed

12 files changed

+49
-104
lines changed

binary/cli/cli.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,7 @@ func multiStringToList(arg []string) []string {
462462

463463
func (f *Flags) scanRoots() ([]*scalibrfs.ScanRoot, error) {
464464
if f.RemoteImage != "" {
465-
imageOptions, err := f.scanRemoteImageOptions()
466-
if err != nil {
467-
return nil, err
468-
}
465+
imageOptions := f.scanRemoteImageOptions()
469466
fs, err := scalibrimage.NewFromRemoteName(f.RemoteImage, *imageOptions...)
470467
if err != nil {
471468
return nil, err
@@ -491,7 +488,7 @@ func (f *Flags) scanRoots() ([]*scalibrfs.ScanRoot, error) {
491488
return scanRoots, nil
492489
}
493490

494-
func (f *Flags) scanRemoteImageOptions() (*[]remote.Option, error) {
491+
func (f *Flags) scanRemoteImageOptions() *[]remote.Option {
495492
imageOptions := []remote.Option{
496493
remote.WithAuthFromKeychain(authn.DefaultKeychain),
497494
}
@@ -504,7 +501,7 @@ func (f *Flags) scanRemoteImageOptions() (*[]remote.Option, error) {
504501
},
505502
))
506503
}
507-
return &imageOptions, nil
504+
return &imageOptions
508505
}
509506

510507
// All capabilities are enabled when running SCALIBR as a binary.

binary/proto/proto.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ func ScanResultToProto(r *scalibr.ScanResult) (*spb.ScanResult, error) {
166166

167167
inventories := make([]*spb.Inventory, 0, len(r.Inventories))
168168
for _, i := range r.Inventories {
169-
p, err := inventoryToProto(i)
170-
if err != nil {
171-
return nil, err
172-
}
169+
p := inventoryToProto(i)
173170
inventories = append(inventories, p)
174171
}
175172

@@ -216,9 +213,9 @@ func pluginStatusToProto(s *plugin.Status) *spb.PluginStatus {
216213
}
217214
}
218215

219-
func inventoryToProto(i *extractor.Inventory) (*spb.Inventory, error) {
216+
func inventoryToProto(i *extractor.Inventory) *spb.Inventory {
220217
if i == nil {
221-
return nil, nil
218+
return nil
222219
}
223220
p := converter.ToPURL(i)
224221
inventoryProto := &spb.Inventory{
@@ -233,7 +230,7 @@ func inventoryToProto(i *extractor.Inventory) (*spb.Inventory, error) {
233230
LayerDetails: layerDetailsToProto(i.LayerDetails),
234231
}
235232
setProtoMetadata(i.Metadata, inventoryProto)
236-
return inventoryProto, nil
233+
return inventoryProto
237234
}
238235

239236
func setProtoMetadata(meta any, i *spb.Inventory) {
@@ -614,10 +611,7 @@ func findingToProto(f *detector.Finding) (*spb.Finding, error) {
614611
}
615612
var target *spb.TargetDetails
616613
if f.Target != nil {
617-
i, err := inventoryToProto(f.Target.Inventory)
618-
if err != nil {
619-
return nil, err
620-
}
614+
i := inventoryToProto(f.Target.Inventory)
621615
target = &spb.TargetDetails{
622616
Location: f.Target.Location,
623617
Inventory: i,

extractor/filesystem/language/golang/gobinary/gobinary.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]
139139
return []*extractor.Inventory{}, nil
140140
}
141141

142-
inventory, err := e.extractPackagesFromBuildInfo(binfo, input.Path)
143-
e.reportFileExtracted(input.Path, input.Info, err)
144-
return inventory, err
142+
inventory := e.extractPackagesFromBuildInfo(binfo, input.Path)
143+
e.reportFileExtracted(input.Path, input.Info, nil)
144+
return inventory, nil
145145
}
146146

147147
func (e Extractor) reportFileExtracted(path string, fileinfo fs.FileInfo, err error) {
@@ -159,7 +159,7 @@ func (e Extractor) reportFileExtracted(path string, fileinfo fs.FileInfo, err er
159159
})
160160
}
161161

162-
func (e *Extractor) extractPackagesFromBuildInfo(binfo *buildinfo.BuildInfo, filename string) ([]*extractor.Inventory, error) {
162+
func (e *Extractor) extractPackagesFromBuildInfo(binfo *buildinfo.BuildInfo, filename string) []*extractor.Inventory {
163163
res := []*extractor.Inventory{}
164164

165165
validatedGoVers, err := validateGoVersion(binfo.GoVersion)
@@ -190,7 +190,7 @@ func (e *Extractor) extractPackagesFromBuildInfo(binfo *buildinfo.BuildInfo, fil
190190
res = append(res, pkg)
191191
}
192192

193-
return res, nil
193+
return res
194194
}
195195

196196
func validateGoVersion(vers string) (string, error) {

extractor/standalone/containers/containerd/containerd_linux.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,7 @@ func containersFromAPI(ctx context.Context, client CtrdClient) ([]Metadata, erro
184184
for _, ns := range nss {
185185
// For each namespace returned by the API, get the containers metadata.
186186
ctx := namespaces.WithNamespace(ctx, ns)
187-
ctrs, err := containersMetadata(ctx, client, ns, defaultContainerdRootfsPrefix)
188-
if err != nil {
189-
log.Errorf("Could not get a list of containers from the containerd: %v", err)
190-
return nil, err
191-
}
187+
ctrs := containersMetadata(ctx, client, ns, defaultContainerdRootfsPrefix)
192188
// Merge all containers metadata items for all namespaces into a single list.
193189
metadata = append(metadata, ctrs...)
194190
}
@@ -205,7 +201,7 @@ func namespacesFromAPI(ctx context.Context, client CtrdClient) ([]string, error)
205201
return nss, nil
206202
}
207203

208-
func containersMetadata(ctx context.Context, client CtrdClient, namespace string, defaultAbsoluteToBundlePath string) ([]Metadata, error) {
204+
func containersMetadata(ctx context.Context, client CtrdClient, namespace string, defaultAbsoluteToBundlePath string) []Metadata {
209205
var containersMetadata []Metadata
210206

211207
taskService := client.TaskService()
@@ -226,7 +222,7 @@ func containersMetadata(ctx context.Context, client CtrdClient, namespace string
226222

227223
containersMetadata = append(containersMetadata, md)
228224
}
229-
return containersMetadata, nil
225+
return containersMetadata
230226
}
231227

232228
func taskMetadata(ctx context.Context, client CtrdClient, task *task.Process, namespace string, defaultAbsoluteToBundlePath string) (Metadata, error) {

semantic/parse.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,33 @@ func Parse(str string, ecosystem string) (Version, error) {
4646
case "Alpine":
4747
return parseAlpineVersion(str)
4848
case "ConanCenter":
49-
return parseSemverVersion(str)
49+
return parseSemverVersion(str), nil
5050
case "CRAN":
51-
return parseCRANVersion(str)
51+
return parseCRANVersion(str), nil
5252
case "crates.io":
53-
return parseSemverVersion(str)
53+
return parseSemverVersion(str), nil
5454
case "Debian":
5555
return parseDebianVersion(str)
5656
case "Go":
57-
return parseSemverVersion(str)
57+
return parseSemverVersion(str), nil
5858
case "Hex":
59-
return parseSemverVersion(str)
59+
return parseSemverVersion(str), nil
6060
case "Maven":
61-
return parseMavenVersion(str)
61+
return parseMavenVersion(str), nil
6262
case "npm":
63-
return parseSemverVersion(str)
63+
return parseSemverVersion(str), nil
6464
case "NuGet":
65-
return parseNuGetVersion(str)
65+
return parseNuGetVersion(str), nil
6666
case "Packagist":
67-
return parsePackagistVersion(str)
67+
return parsePackagistVersion(str), nil
6868
case "Pub":
69-
return parseSemverVersion(str)
69+
return parseSemverVersion(str), nil
7070
case "PyPI":
7171
return parsePyPIVersion(str)
7272
case "Red Hat":
73-
return parseRedHatVersion(str)
73+
return parseRedHatVersion(str), nil
7474
case "RubyGems":
75-
return parseRubyGemsVersion(str)
75+
return parseRubyGemsVersion(str), nil
7676
case "Ubuntu":
7777
return parseDebianVersion(str)
7878
}

semantic/version-cran.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,10 @@ func (v cranVersion) compare(w cranVersion) int {
4949
}
5050

5151
func (v cranVersion) CompareStr(str string) (int, error) {
52-
w, err := parseCRANVersion(str)
53-
54-
if err != nil {
55-
return 0, err
56-
}
57-
58-
return v.compare(w), nil
52+
return v.compare(parseCRANVersion(str)), nil
5953
}
6054

61-
func parseCRANVersion(str string) (cranVersion, error) {
55+
func parseCRANVersion(str string) cranVersion {
6256
// dashes and periods have the same weight, so we can just normalize to periods
6357
parts := strings.Split(strings.ReplaceAll(str, "-", "."), ".")
6458

@@ -70,5 +64,5 @@ func parseCRANVersion(str string) (cranVersion, error) {
7064
comps = append(comps, v)
7165
}
7266

73-
return cranVersion{comps}, nil
67+
return cranVersion{comps}
7468
}

semantic/version-maven.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,9 @@ func (mv mavenVersion) compare(w mavenVersion) (int, error) {
359359
}
360360

361361
func (mv mavenVersion) CompareStr(str string) (int, error) {
362-
mw, err := parseMavenVersion(str)
363-
364-
if err != nil {
365-
return 0, err
366-
}
367-
368-
return mv.compare(mw)
362+
return mv.compare(parseMavenVersion(str))
369363
}
370364

371-
func parseMavenVersion(str string) (mavenVersion, error) {
372-
return newMavenVersion(str), nil
365+
func parseMavenVersion(str string) mavenVersion {
366+
return newMavenVersion(str)
373367
}

semantic/version-nuget.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@ func (v nuGetVersion) compare(w nuGetVersion) int {
2929
}
3030

3131
func (v nuGetVersion) CompareStr(str string) (int, error) {
32-
w, err := parseNuGetVersion(str)
33-
34-
if err != nil {
35-
return 0, err
36-
}
37-
38-
return v.compare(w), nil
32+
return v.compare(parseNuGetVersion(str)), nil
3933
}
4034

41-
func parseNuGetVersion(str string) (nuGetVersion, error) {
42-
return nuGetVersion{parseSemverLikeVersion(str, 4)}, nil
35+
func parseNuGetVersion(str string) nuGetVersion {
36+
return nuGetVersion{parseSemverLikeVersion(str, 4)}
4337
}

semantic/version-packagist.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,17 @@ type packagistVersion struct {
128128
Components []string
129129
}
130130

131-
func parsePackagistVersion(str string) (packagistVersion, error) {
131+
func parsePackagistVersion(str string) packagistVersion {
132132
return packagistVersion{
133133
str,
134134
strings.Split(canonicalizePackagistVersion(str), "."),
135-
}, nil
135+
}
136136
}
137137

138138
func (v packagistVersion) compare(w packagistVersion) int {
139139
return comparePackagistComponents(v.Components, w.Components)
140140
}
141141

142142
func (v packagistVersion) CompareStr(str string) (int, error) {
143-
w, err := parsePackagistVersion(str)
144-
145-
if err != nil {
146-
return 0, err
147-
}
148-
149-
return v.compare(w), nil
143+
return v.compare(parsePackagistVersion(str)), nil
150144
}

semantic/version-redhat.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,7 @@ func (v redHatVersion) compare(w redHatVersion) int {
203203
}
204204

205205
func (v redHatVersion) CompareStr(str string) (int, error) {
206-
w, err := parseRedHatVersion(str)
207-
208-
if err != nil {
209-
return 0, err
210-
}
211-
212-
return v.compare(w), nil
206+
return v.compare(parseRedHatVersion(str)), nil
213207
}
214208

215209
// parseRedHatVersion parses a Red Hat version into a redHatVersion struct.
@@ -223,7 +217,7 @@ func (v redHatVersion) CompareStr(str string) (int, error) {
223217
//
224218
// When all components are present, the version is represented as "n-e:v-r.a",
225219
// though only the version is actually required.
226-
func parseRedHatVersion(str string) (redHatVersion, error) {
220+
func parseRedHatVersion(str string) redHatVersion {
227221
bf, af, hasColon := strings.Cut(str, ":")
228222

229223
if !hasColon {
@@ -247,5 +241,5 @@ func parseRedHatVersion(str string) (redHatVersion, error) {
247241
epoch = "0"
248242
}
249243

250-
return redHatVersion{epoch, version, release}, nil
244+
return redHatVersion{epoch, version, release}
251245
}

semantic/version-rubygems.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,17 @@ type rubyGemsVersion struct {
119119
Segments []string
120120
}
121121

122-
func parseRubyGemsVersion(str string) (rubyGemsVersion, error) {
122+
func parseRubyGemsVersion(str string) rubyGemsVersion {
123123
return rubyGemsVersion{
124124
str,
125125
canonicalSegments(strings.Split(canonicalizeRubyGemVersion(str), ".")),
126-
}, nil
126+
}
127127
}
128128

129129
func (v rubyGemsVersion) compare(w rubyGemsVersion) int {
130130
return compareRubyGemsComponents(v.Segments, w.Segments)
131131
}
132132

133133
func (v rubyGemsVersion) CompareStr(str string) (int, error) {
134-
w, err := parseRubyGemsVersion(str)
135-
136-
if err != nil {
137-
return 0, err
138-
}
139-
140-
return v.compare(w), nil
134+
return v.compare(parseRubyGemsVersion(str)), nil
141135
}

semantic/version-semver.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ type semverVersion struct {
100100
semverLikeVersion
101101
}
102102

103-
func parseSemverVersion(str string) (semverVersion, error) {
104-
return semverVersion{parseSemverLikeVersion(str, 3)}, nil
103+
func parseSemverVersion(str string) semverVersion {
104+
return semverVersion{parseSemverLikeVersion(str, 3)}
105105
}
106106

107107
func (v semverVersion) compare(w semverVersion) int {
@@ -113,11 +113,5 @@ func (v semverVersion) compare(w semverVersion) int {
113113
}
114114

115115
func (v semverVersion) CompareStr(str string) (int, error) {
116-
w, err := parseSemverVersion(str)
117-
118-
if err != nil {
119-
return 0, err
120-
}
121-
122-
return v.compare(w), nil
116+
return v.compare(parseSemverVersion(str)), nil
123117
}

0 commit comments

Comments
 (0)