-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmanifests.go
More file actions
115 lines (98 loc) · 2.98 KB
/
manifests.go
File metadata and controls
115 lines (98 loc) · 2.98 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package manifests parses dependency manifest and lockfile formats across package ecosystems.
//
// It supports 40+ ecosystems including npm, gem, pypi, cargo, maven, and more.
// Each ecosystem uses its PURL type as the identifier.
//
// Basic usage:
//
// result, err := manifests.Parse("package.json", content)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Ecosystem: %s, Kind: %s\n", result.Ecosystem, result.Kind)
// for _, dep := range result.Dependencies {
// fmt.Printf(" %s %s\n", dep.Name, dep.Version)
// }
package manifests
import (
"github.com/git-pkgs/manifests/internal/core"
"github.com/git-pkgs/purl"
)
// Re-export types from internal/core for public API.
type (
Kind = core.Kind
Scope = core.Scope
Dependency = core.Dependency
)
// Re-export constants.
const (
Manifest Kind = core.Manifest
Lockfile Kind = core.Lockfile
Supplement Kind = core.Supplement
Runtime Scope = core.Runtime
Development Scope = core.Development
Test Scope = core.Test
Build Scope = core.Build
Optional Scope = core.Optional
)
// ParseResult contains the parsed dependencies from a manifest or lockfile.
type ParseResult struct {
Ecosystem string
Kind Kind
Dependencies []Dependency
}
// Parse parses a manifest or lockfile and returns its dependencies.
func Parse(filename string, content []byte) (*ParseResult, error) {
parser, eco, kind := core.IdentifyParser(filename)
if parser == nil {
return nil, &UnknownFileError{Filename: filename}
}
deps, err := parser.Parse(filename, content)
if err != nil {
return nil, err
}
// Generate PURLs for all dependencies
for i := range deps {
version := ""
if kind == Lockfile || kind == Supplement {
version = deps[i].Version
}
deps[i].PURL = makePURL(eco, deps[i].Name, version, deps[i].RegistryURL)
}
return &ParseResult{
Ecosystem: eco,
Kind: kind,
Dependencies: deps,
}, nil
}
// makePURL creates a Package URL for a dependency.
func makePURL(ecosystem, name, version, registryURL string) string {
return purl.BuildPURLString(ecosystem, name, version, registryURL)
}
// Identify returns the ecosystem and kind for a filename without parsing.
func Identify(filename string) (ecosystem string, kind Kind, ok bool) {
_, eco, k := core.IdentifyParser(filename)
if eco == "" {
return "", "", false
}
return eco, k, true
}
// Match represents a file type match.
type Match = core.Match
// IdentifyAll returns all matching ecosystems for a filename.
func IdentifyAll(filename string) []Match {
return core.IdentifyAllParsers(filename)
}
// Ecosystems returns a list of all supported PURL ecosystem types.
func Ecosystems() []string {
return core.SupportedEcosystems()
}
// UnknownFileError is returned when a file type is not recognized.
type UnknownFileError struct {
Filename string
}
func (e *UnknownFileError) Error() string {
return "unknown manifest file: " + e.Filename
}
// ParseError is re-exported from internal/core.
type ParseError = core.ParseError