-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathparse.go
67 lines (53 loc) · 1.45 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package binary
import (
"debug/buildinfo"
"strings"
"golang.org/x/xerrors"
dio "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
)
var (
ErrUnrecognizedExe = xerrors.New("unrecognized executable format")
ErrNonGoBinary = xerrors.New("non go binary")
)
// convertError detects buildinfo.errUnrecognizedFormat and convert to
// ErrUnrecognizedExe and convert buildinfo.errNotGoExe to ErrNonGoBinary
func convertError(err error) error {
errText := err.Error()
if strings.HasSuffix(errText, "unrecognized file format") {
return ErrUnrecognizedExe
}
if strings.HasSuffix(errText, "not a Go executable") {
return ErrNonGoBinary
}
return err
}
type Parser struct{}
func NewParser() types.Parser {
return &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) {
info, err := buildinfo.Read(r)
if err != nil {
return nil, nil, convertError(err)
}
libs := make([]types.Library, 0, len(info.Deps))
for _, dep := range info.Deps {
// binaries with old go version may incorrectly add module in Deps
// In this case Path == "", Version == "Devel"
// we need to skip this
if dep.Path == "" {
continue
}
mod := dep
if dep.Replace != nil {
mod = dep.Replace
}
libs = append(libs, types.Library{
Name: mod.Path,
Version: mod.Version,
})
}
return libs, nil, nil
}