-
Notifications
You must be signed in to change notification settings - Fork 336
/
parse-cargo-lock.go
48 lines (36 loc) · 1.08 KB
/
parse-cargo-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
package lockfile
import (
"fmt"
"github.com/BurntSushi/toml"
"os"
)
type CargoLockPackage struct {
Name string `toml:"name"`
Version string `toml:"version"`
}
type CargoLockFile struct {
Version int `toml:"version"`
Packages []CargoLockPackage `toml:"package"`
}
const CargoEcosystem Ecosystem = "crates.io"
func ParseCargoLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *CargoLockFile
lockfileContents, err := os.ReadFile(pathToLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
}
err = toml.Unmarshal(lockfileContents, &parsedLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err)
}
packages := make([]PackageDetails, 0, len(parsedLockfile.Packages))
for _, lockPackage := range parsedLockfile.Packages {
packages = append(packages, PackageDetails{
Name: lockPackage.Name,
Version: lockPackage.Version,
Ecosystem: CargoEcosystem,
CompareAs: CargoEcosystem,
})
}
return packages, nil
}