-
Notifications
You must be signed in to change notification settings - Fork 362
/
parse-composer-lock.go
80 lines (64 loc) · 2 KB
/
parse-composer-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
79
80
package lockfile
import (
"encoding/json"
"fmt"
"path/filepath"
)
type ComposerPackage struct {
Name string `json:"name"`
Version string `json:"version"`
Dist struct {
Reference string `json:"reference"`
} `json:"dist"`
}
type ComposerLock struct {
Packages []ComposerPackage `json:"packages"`
PackagesDev []ComposerPackage `json:"packages-dev"`
}
const ComposerEcosystem Ecosystem = "Packagist"
type ComposerLockExtractor struct{}
func (e ComposerLockExtractor) ShouldExtract(path string) bool {
return filepath.Base(path) == "composer.lock"
}
func (e ComposerLockExtractor) Extract(f DepFile) ([]PackageDetails, error) {
var parsedLockfile *ComposerLock
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 cannot return negative numbers, but the types can't reflect that
uint64(len(parsedLockfile.Packages))+uint64(len(parsedLockfile.PackagesDev)),
)
for _, composerPackage := range parsedLockfile.Packages {
packages = append(packages, PackageDetails{
Name: composerPackage.Name,
Version: composerPackage.Version,
Commit: composerPackage.Dist.Reference,
Ecosystem: ComposerEcosystem,
CompareAs: ComposerEcosystem,
})
}
for _, composerPackage := range parsedLockfile.PackagesDev {
packages = append(packages, PackageDetails{
Name: composerPackage.Name,
Version: composerPackage.Version,
Commit: composerPackage.Dist.Reference,
Ecosystem: ComposerEcosystem,
CompareAs: ComposerEcosystem,
DepGroups: []string{"dev"},
})
}
return packages, nil
}
var _ Extractor = ComposerLockExtractor{}
//nolint:gochecknoinits
func init() {
registerExtractor("composer.lock", ComposerLockExtractor{})
}
// Deprecated: use ComposerLockExtractor.Extract instead
func ParseComposerLock(pathToLockfile string) ([]PackageDetails, error) {
return extractFromFile(pathToLockfile, ComposerLockExtractor{})
}