-
Notifications
You must be signed in to change notification settings - Fork 336
/
parse-renv-lock.go
64 lines (48 loc) · 1.38 KB
/
parse-renv-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
package lockfile
import (
"encoding/json"
"fmt"
"path/filepath"
)
type RenvPackage struct {
Package string `json:"Package"`
Version string `json:"Version"`
Repository string `json:"Repository"`
}
type RenvLockfile struct {
Packages map[string]RenvPackage `json:"Packages"`
}
const CRANEcosystem Ecosystem = "CRAN"
type RenvLockExtractor struct{}
func (e RenvLockExtractor) ShouldExtract(path string) bool {
return filepath.Base(path) == "renv.lock"
}
func (e RenvLockExtractor) Extract(f DepFile) ([]PackageDetails, error) {
var parsedLockfile *RenvLockfile
err := json.NewDecoder(f).Decode(&parsedLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not extract from %s: %w", f.Path(), err)
}
packages := make([]PackageDetails, 0, len(parsedLockfile.Packages))
for _, pkg := range parsedLockfile.Packages {
// currently we only support CRAN
if pkg.Repository != string(CRANEcosystem) {
continue
}
packages = append(packages, PackageDetails{
Name: pkg.Package,
Version: pkg.Version,
Ecosystem: CRANEcosystem,
CompareAs: CRANEcosystem,
})
}
return packages, nil
}
var _ Extractor = RenvLockExtractor{}
//nolint:gochecknoinits
func init() {
registerExtractor("renv.lock", RenvLockExtractor{})
}
func ParseRenvLock(pathToLockfile string) ([]PackageDetails, error) {
return extractFromFile(pathToLockfile, RenvLockExtractor{})
}