-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse-poetry-lock.go
56 lines (42 loc) · 1.31 KB
/
parse-poetry-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
package lockfile
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
)
type PoetryLockPackageSource struct {
Type string `toml:"type"`
Commit string `toml:"resolved_reference"`
}
type PoetryLockPackage struct {
Name string `toml:"name"`
Version string `toml:"version"`
Source PoetryLockPackageSource `toml:"source"`
}
type PoetryLockFile struct {
Version int `toml:"version"`
Packages []PoetryLockPackage `toml:"package"`
}
const PoetryEcosystem = PipEcosystem
func ParsePoetryLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *PoetryLockFile
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,
Commit: lockPackage.Source.Commit,
Ecosystem: PoetryEcosystem,
CompareAs: PoetryEcosystem,
})
}
return packages, nil
}