-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse-pdm-lock.go
57 lines (42 loc) · 1.2 KB
/
parse-pdm-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
package lockfile
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
)
type PdmLockPackage struct {
Name string `toml:"name"`
Version string `toml:"version"`
Groups []string `toml:"groups"`
Revision string `toml:"revision"`
}
type PdmLockFile struct {
Version string `toml:"lock-version"`
Packages []PdmLockPackage `toml:"package"`
}
const PdmEcosystem = PipEcosystem
func ParsePdmLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *PdmLockFile
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 _, pkg := range parsedLockfile.Packages {
details := PackageDetails{
Name: pkg.Name,
Version: pkg.Version,
Ecosystem: PdmEcosystem,
CompareAs: PdmEcosystem,
}
if pkg.Revision != "" {
details.Commit = pkg.Revision
}
packages = append(packages, details)
}
return packages, nil
}