Skip to content

Commit

Permalink
refactor: remove always-nil error returns from internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Mar 5, 2025
1 parent 6fb3f73 commit 829f302
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
9 changes: 3 additions & 6 deletions binary/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,7 @@ func multiStringToList(arg []string) []string {

func (f *Flags) scanRoots() ([]*scalibrfs.ScanRoot, error) {
if f.RemoteImage != "" {
imageOptions, err := f.scanRemoteImageOptions()
if err != nil {
return nil, err
}
imageOptions := f.scanRemoteImageOptions()
fs, err := scalibrimage.NewFromRemoteName(f.RemoteImage, *imageOptions...)
if err != nil {
return nil, err
Expand All @@ -504,7 +501,7 @@ func (f *Flags) scanRoots() ([]*scalibrfs.ScanRoot, error) {
return scanRoots, nil
}

func (f *Flags) scanRemoteImageOptions() (*[]remote.Option, error) {
func (f *Flags) scanRemoteImageOptions() *[]remote.Option {
imageOptions := []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
}
Expand All @@ -517,7 +514,7 @@ func (f *Flags) scanRemoteImageOptions() (*[]remote.Option, error) {
},
))
}
return &imageOptions, nil
return &imageOptions
}

// All capabilities are enabled when running SCALIBR as a binary.
Expand Down
16 changes: 5 additions & 11 deletions binary/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ func ScanResultToProto(r *scalibr.ScanResult) (*spb.ScanResult, error) {

inventories := make([]*spb.Inventory, 0, len(r.Inventories))
for _, i := range r.Inventories {
p, err := inventoryToProto(i)
if err != nil {
return nil, err
}
p := inventoryToProto(i)
inventories = append(inventories, p)
}

Expand Down Expand Up @@ -216,9 +213,9 @@ func pluginStatusToProto(s *plugin.Status) *spb.PluginStatus {
}
}

func inventoryToProto(i *extractor.Inventory) (*spb.Inventory, error) {
func inventoryToProto(i *extractor.Inventory) *spb.Inventory {
if i == nil {
return nil, nil
return nil
}
p := converter.ToPURL(i)
inventoryProto := &spb.Inventory{
Expand All @@ -233,7 +230,7 @@ func inventoryToProto(i *extractor.Inventory) (*spb.Inventory, error) {
LayerDetails: layerDetailsToProto(i.LayerDetails),
}
setProtoMetadata(i.Metadata, inventoryProto)
return inventoryProto, nil
return inventoryProto
}

func setProtoMetadata(meta any, i *spb.Inventory) {
Expand Down Expand Up @@ -600,10 +597,7 @@ func findingToProto(f *detector.Finding) (*spb.Finding, error) {
}
var target *spb.TargetDetails
if f.Target != nil {
i, err := inventoryToProto(f.Target.Inventory)
if err != nil {
return nil, err
}
i := inventoryToProto(f.Target.Inventory)
target = &spb.TargetDetails{
Location: f.Target.Location,
Inventory: i,
Expand Down
10 changes: 5 additions & 5 deletions extractor/filesystem/language/golang/gobinary/gobinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]
return []*extractor.Inventory{}, nil
}

inventory, err := e.extractPackagesFromBuildInfo(binfo, input.Path)
e.reportFileExtracted(input.Path, input.Info, err)
return inventory, err
inventory := e.extractPackagesFromBuildInfo(binfo, input.Path)
e.reportFileExtracted(input.Path, input.Info, nil)
return inventory, nil
}

func (e Extractor) reportFileExtracted(path string, fileinfo fs.FileInfo, err error) {
Expand All @@ -159,7 +159,7 @@ func (e Extractor) reportFileExtracted(path string, fileinfo fs.FileInfo, err er
})
}

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

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

return res, nil
return res
}

func validateGoVersion(vers string) (string, error) {
Expand Down
10 changes: 3 additions & 7 deletions extractor/standalone/containers/containerd/containerd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ func containersFromAPI(ctx context.Context, client CtrdClient) ([]Metadata, erro
for _, ns := range nss {
// For each namespace returned by the API, get the containers metadata.
ctx := namespaces.WithNamespace(ctx, ns)
ctrs, err := containersMetadata(ctx, client, ns, defaultContainerdRootfsPrefix)
if err != nil {
log.Errorf("Could not get a list of containers from the containerd: %v", err)
return nil, err
}
ctrs := containersMetadata(ctx, client, ns, defaultContainerdRootfsPrefix)
// Merge all containers metadata items for all namespaces into a single list.
metadata = append(metadata, ctrs...)
}
Expand All @@ -205,7 +201,7 @@ func namespacesFromAPI(ctx context.Context, client CtrdClient) ([]string, error)
return nss, nil
}

func containersMetadata(ctx context.Context, client CtrdClient, namespace string, defaultAbsoluteToBundlePath string) ([]Metadata, error) {
func containersMetadata(ctx context.Context, client CtrdClient, namespace string, defaultAbsoluteToBundlePath string) []Metadata {
var containersMetadata []Metadata

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

containersMetadata = append(containersMetadata, md)
}
return containersMetadata, nil
return containersMetadata
}

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

0 comments on commit 829f302

Please sign in to comment.