-
Notifications
You must be signed in to change notification settings - Fork 333
/
parse-mix-lock.go
78 lines (59 loc) · 1.78 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
package lockfile
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/google/osv-scanner/internal/cachedregexp"
)
const MixEcosystem Ecosystem = "Hex"
func ParseMixLock(pathToLockfile string) ([]PackageDetails, error) {
file, err := os.Open(pathToLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not open %s: %w", pathToLockfile, err)
}
defer file.Close()
re := cachedregexp.MustCompile(`^ +"(\w+)": \{.+,$`)
scanner := bufio.NewScanner(file)
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", pathToLockfile, err)
}
return packages, nil
}