|
1 | 1 | package lockfile |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "os" |
6 | | - |
7 | | - "gopkg.in/yaml.v3" |
| 4 | + "github.com/google/osv-scalibr/extractor/filesystem/language/dart/pubspec" |
8 | 5 | ) |
9 | 6 |
|
10 | | -type PubspecLockDescription struct { |
11 | | - Name string `yaml:"name"` |
12 | | - URL string `yaml:"url"` |
13 | | - Path string `yaml:"path"` |
14 | | - Ref string `yaml:"resolved-ref"` |
15 | | -} |
16 | | - |
17 | | -func (pld *PubspecLockDescription) UnmarshalYAML(value *yaml.Node) error { |
18 | | - var m struct { |
19 | | - Name string `yaml:"name"` |
20 | | - URL string `yaml:"url"` |
21 | | - Path string `yaml:"path"` |
22 | | - Ref string `yaml:"resolved-ref"` |
23 | | - } |
24 | | - |
25 | | - err := value.Decode(&m) |
26 | | - |
27 | | - if err == nil { |
28 | | - pld.Name = m.Name |
29 | | - pld.Path = m.Path |
30 | | - pld.URL = m.URL |
31 | | - pld.Ref = m.Ref |
32 | | - |
33 | | - return nil |
34 | | - } |
35 | | - |
36 | | - var str *string |
37 | | - |
38 | | - err = value.Decode(&str) |
39 | | - |
40 | | - if err != nil { |
41 | | - return fmt.Errorf("%w", err) |
42 | | - } |
43 | | - |
44 | | - pld.Path = *str |
45 | | - |
46 | | - return nil |
47 | | -} |
48 | | - |
49 | | -type PubspecLockPackage struct { |
50 | | - Source string `yaml:"source"` |
51 | | - Description PubspecLockDescription `yaml:"description"` |
52 | | - Version string `yaml:"version"` |
53 | | -} |
54 | | - |
55 | | -type PubspecLockfile struct { |
56 | | - Packages map[string]PubspecLockPackage `yaml:"packages,omitempty"` |
57 | | - Sdks map[string]string `yaml:"sdks"` |
58 | | -} |
59 | | - |
60 | 7 | const PubEcosystem Ecosystem = "Pub" |
61 | 8 |
|
62 | 9 | func ParsePubspecLock(pathToLockfile string) ([]PackageDetails, error) { |
63 | | - var parsedLockfile *PubspecLockfile |
64 | | - |
65 | | - lockfileContents, err := os.ReadFile(pathToLockfile) |
66 | | - |
67 | | - if err != nil { |
68 | | - return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err) |
69 | | - } |
70 | | - |
71 | | - err = yaml.Unmarshal(lockfileContents, &parsedLockfile) |
72 | | - |
73 | | - if err != nil { |
74 | | - return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err) |
75 | | - } |
76 | | - if parsedLockfile == nil { |
77 | | - return []PackageDetails{}, nil |
78 | | - } |
79 | | - |
80 | | - packages := make([]PackageDetails, 0, len(parsedLockfile.Packages)) |
81 | | - |
82 | | - for name, pkg := range parsedLockfile.Packages { |
83 | | - packages = append(packages, PackageDetails{ |
84 | | - Name: name, |
85 | | - Version: pkg.Version, |
86 | | - Commit: pkg.Description.Ref, |
87 | | - Ecosystem: PubEcosystem, |
88 | | - CompareAs: PubEcosystem, |
89 | | - }) |
90 | | - } |
91 | | - |
92 | | - return packages, nil |
| 10 | + return extract(pathToLockfile, pubspec.New(), PubEcosystem) |
93 | 11 | } |
0 commit comments