-
Notifications
You must be signed in to change notification settings - Fork 338
/
parse-mix-lock.go
90 lines (67 loc) · 2.02 KB
/
parse-mix-lock.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package lockfile
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/osv-scanner/internal/cachedregexp"
)
const MixEcosystem Ecosystem = "Hex"
type MixLockExtractor struct{}
func (e MixLockExtractor) ShouldExtract(path string) bool {
return filepath.Base(path) == "mix.lock"
}
func (e MixLockExtractor) Extract(f DepFile) ([]PackageDetails, error) {
re := cachedregexp.MustCompile(`^ +"(\w+)": \{.+,$`)
scanner := bufio.NewScanner(f)
var packages []PackageDetails
for scanner.Scan() {
line := scanner.Text()
match := re.FindStringSubmatch(line)
if match == nil {
continue
}
// we only care about the third and fourth "rows" which are both strings,
// so we can safely split the line as if it's a set of comma-separated fields
// even though that'll actually poorly represent nested arrays & objects
fields := strings.FieldsFunc(line, func(r rune) bool {
return r == ','
})
if len(fields) < 4 {
_, _ = fmt.Fprintf(
os.Stderr,
"Found less than four fields when parsing a line that looks like a dependency in a mix.lock - please report this!\n",
)
continue
}
name := match[1]
version := strings.TrimSpace(fields[2])
commit := strings.TrimSpace(fields[3])
version = strings.TrimSuffix(strings.TrimPrefix(version, `"`), `"`)
commit = strings.TrimSuffix(strings.TrimPrefix(commit, `"`), `"`)
if strings.HasSuffix(fields[0], ":git") {
commit = version
version = ""
}
packages = append(packages, PackageDetails{
Name: name,
Version: version,
Ecosystem: MixEcosystem,
CompareAs: MixEcosystem,
Commit: commit,
})
}
if err := scanner.Err(); err != nil {
return []PackageDetails{}, fmt.Errorf("error while scanning %s: %w", f.Path(), err)
}
return packages, nil
}
var _ Extractor = MixLockExtractor{}
//nolint:gochecknoinits
func init() {
registerExtractor("mix.lock", MixLockExtractor{})
}
func ParseMixLock(pathToLockfile string) ([]PackageDetails, error) {
return extractFromFile(pathToLockfile, MixLockExtractor{})
}