Skip to content

Commit

Permalink
Merge pull request #16 from deepfactor-io/3.5-DEEP-9655
Browse files Browse the repository at this point in the history
DEEP-9655 Add build id to package info
  • Loading branch information
namandf authored Jan 2, 2024
2 parents abffffa + d5eaafe commit 8d2a48d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
35 changes: 33 additions & 2 deletions pkg/golang/binary/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package binary

import (
"debug/buildinfo"
"debug/elf"
"fmt"
"strings"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -37,11 +39,22 @@ func NewParser() types.Parser {

// Parse scans file to try to report the Go and module versions.
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var warnings []string
var buildID string

info, err := buildinfo.Read(r)
if err != nil {
return nil, nil, convertError(err)
}

if len(info.Deps) > 0 {
// get build id
buildID, err = getBuildID(r)
if err != nil {
warnings = []string{err.Error()}
}
}

libs := make([]types.Library, 0, len(info.Deps))

for _, dep := range info.Deps {
Expand All @@ -58,10 +71,28 @@ func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency,
}

libs = append(libs, types.Library{
Name: mod.Path,
Version: mod.Version,
Name: mod.Path,
Version: mod.Version,
BuildID: buildID,
Warnings: warnings,
})
}

return libs, nil, nil
}

func getBuildID(r dio.ReadSeekerAt) (string, error) {
file, err := elf.NewFile(r)
if err != nil {
return "", err
}
defer file.Close()

for _, section := range file.Sections {
if section.Name == ".note.go.buildid" {
data, _ := section.Data()
return string(data[16:]), nil
}
}
return "", fmt.Errorf("Go BuildID not found")
}
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Library struct {
Locations Locations `json:",omitempty"`
FilePath string `json:",omitempty"` // Required to show nested jars
Warnings []string `json:",omitempty"`
BuildID string `json:",omitempty"`
}

type Libraries []Library
Expand Down

0 comments on commit 8d2a48d

Please sign in to comment.